INSERT INTO Table1(group, account)
OUTPUT inserted.Id, B.title, B.amount
INTO Table2(id2, title, amount)
SELECT A.*,
B.title,
B.amount,
B.id2
FROM Table1 AS A
LEFT OUTER JOIN
(SELECT title,
amount,
id2
FROM Table2) AS B
ON A.id = B.id2
i'm stuck with this..i have two join tables and what i want is to copy the same set of data from table1 to itself and copy the new id of the newly copied data from table1 to table2 column id2 by using OUTPUT clause.
but now with the query above i cant get through the column that i needed..how can i insert column B.title & B.amount to table2 ?
If table 1 and table 2 have a 1:1 relationship, and no foreign key exists between the two then you could do this in a single statement:
Example on SQL Fiddle
Realistically though, if your tables are related they should have a foreign key, and in most cases they won't be 1:1, rather 1:n.
In which case you would still need to use
MERGEto caputre both the new ID and the old ID, but you would then need to capture this mapping in a temporary table before performing a second insert to Table2:Example on SQL Fiddle