1

I have 3 tables, Table A,B,C

Table B has a foreign key of Table A Table C has a foreign key key of Table B

Now I'm planning to insert more than one record in table A then I need to capture inserted primary key values to refer to the Table B insertion then I need to capture the Table B inserted identity values for Table C reference insertion etc..

I have tried

MERGE 
INTO A
USING
(
 SLECT A.*,B.*,C.*
 FROM A INNER JOIN B ON A.ID =B.ID
 INNER JOIN C ON C.ID =B.ID
) AS D
ON 1 = 0
WHEN NOT MATCHED THEN
INSERT
(
 VALUE1,VALUE2,VALUE3
)
VALUES
(
 A.VALUE1,A.VALUE2,A.VALUE3
)

OUTPUT INSERTED A.VALUE1,
B.VALUE2,B.VALUE3
INTO B
(
VALUE1,VALUE2,VALUE3
)

OUTPUT INSERTED B.VALUE1,
C.VALUE2,C.VALUE3
INTO C
(
VALUE1,VALUE2,VALUE3
)

Here I'm trying to insert the same table values but issue here is table A,B,C has same column names so I cant select all tables in using statement in Merger.

Is there any way to insert as per my requirement?

Shakeer Mirza
  • 5,054
  • 2
  • 18
  • 41
Domnic
  • 3,817
  • 9
  • 40
  • 60
  • Possible duplicate of [Fastest way to perform nested bulk inserts with scope\_identity() usage?](http://stackoverflow.com/questions/34826450/fastest-way-to-perform-nested-bulk-inserts-with-scope-identity-usage) – Zohar Peled Jan 27 '17 at 12:33

0 Answers0