0
insert Table1
OUTPUT inserted.ID, s.[Id] 
INTO @Inserted
select 
s.[Field1],
s.[Field2],
from 
@TableVariable s

why is s.[Id] marked by red line ? Is it non-correct syntax to save value in @Inserted table variable ?

4
  • You can run the query and it will throw an error to help you troubleshoot. You can double click the error in SSMS and it will point out in the window where the error is. However, the syntax for this query is overall wrong. Google how to write an insert into and google syntax for using output/output with a join. Commented Jun 7, 2016 at 15:42
  • 4
    If you want to create some kind of mapping, there's a method involving MERGE. Commented Jun 7, 2016 at 16:13
  • I found an answer as well as example here stackoverflow.com/a/1786653/5173754 Commented Jun 7, 2016 at 16:58
  • 1
    @DotNetGoose - edit your question to clearly show the design of the tables and variables involved. Without that, it's difficult to provide an answer that has clarity. Commented Jun 7, 2016 at 17:04

1 Answer 1

2

s is not valid
at that point you only have inserted

if you want the fields then try

OUTPUT inserted.ID, inserted.[Field1], inserted.[Field2] 

instead of line 2 in your query. You can find more here:

OUTPUT Clause (Transact-SQL)

8
  • but how can i map new ID on an old ID ? Commented Jun 7, 2016 at 16:43
  • New ID, old ID? You are inserting two fields [Field1] and [Field2]. Answer is only as good as the question. . Commented Jun 7, 2016 at 16:45
  • one of them is a new id an another one is an old id Commented Jun 7, 2016 at 16:48
  • it has Identity property, so its an id Commented Jun 7, 2016 at 16:50
  • @ypercubeᵀᴹ i called it "old" then that is old :) Commented Jun 7, 2016 at 16:58

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.