OUPUT for insert from table variable gives: “The multi-part identifier ”k.CustomerName“ could not be bound.”?

I think the approach you are taking is complicating the scenario.

I think the approach you are taking is complicating the scenario. Firstly, the OUTPUT clause is not going to work the way you need it to work here, because you can only use columns inserted, and you want to use columns from the source table, but ones that does not get inserted. (CustomerName as an example).

There are two ways I suggest you can go about this: First Approach. Change you query. Use OUTPUT, but output an id field.

Then, after your first insert into ITEMS, join your new table with your source table. NOW you will have access to all fields and still a way to identify which records should be inserted. Second approach.

Drop your temp tables. Use a simple insert statement with a WHERE _ NOT IN clause. This takes away the complexity and still achieves the goal of not inserting duplicates, just on a closer level.

If not, your database structure can solve this, by adding a customerID to the Items table. If this is not the case, you will have to look at using a staging table with the same seed as the items tabel, where you add the customer id column. That would enable you to keep the link between customer and item just for the session and you can use that table as a base to insert into all the other tables.

– Charl Lamprecht Dec 20 at 9:20.

Use: INSERT INTO dbo. Items OUTPUT INSERTED. CustomerName, INSERTED.

CustomerNr, INSERTED. PkItemId INTO @ItemIds (CustomerName, CustomerNr, pkGenericValueId) SELECT 2, 1, null, null, 1, null FROM @TempA k; INSERT INTO dbo. DefinitionToItem OUTPUT INSERTED.

CustomerName, INSERTED. CustomerNr, INSERTED. PkDefinitionToItemId INTO @DefinitionToItemIds SELECT 1, 0, @DefinitionId, gvd.

PkItemId FROM @ItemIds gvd; You might need to change the column names, because they need to come from the table being inserted into rather than the column they were sourced from.

It didn't work. I need to insert those value for every customer and need @ItemsIds to contain for each customer the id of the value I inserted. But the customer name and number should not go into the Items and DefinitionToItem tables.

– Matthijs Wessels Dec 20 at 5:11 1 @MatthijsWessels - You can't get values with output that you don't insert to the target table. If you were to use SQL Server 2008 you could use merge where it is possible. Stackoverflow.Com/questions/5365629/… – Mikael Eriksson Dec 20 at 6:40.

Your table #AllCustomers does not contain field CustomerName in this query SELECT k. CustomerName , k. CustomerNr FROM #AllCustomers k.

You're right, I will correct it. But I still get the same error – Matthijs Wessels Dec 20 at 4:56 Add the column aliases to the subquery SELECT 2, 1, null, null, 1, null FROM @TempA k; – Oleg Dok Dec 20 at 4:59 I changed it to: 2 AS VALUETYPE, 1 AS Version, null AS IValue, null AS SValue, 1 AS BValue, null AS DValue, but no luck – Matthijs Wessels Dec 20 at 5:08 Sorry for misinformation - you cannot use table aliases except "inserted" or "deleted" for OUTPUT clause used in INSERT statement – Oleg Dok Dec 20 at 5:17.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions