Can anyone tell me how to do repeated multiple inserts on two tables with primary key, foreign key Here\'s what I\'ve done. This is a very snippet of what needs to be done.
Use the OUTPUT clause
DECLARE @IDS TABLE (id INT)
INSERT INTO [dbo].[Table1]
([UserID]
,[FirstName])
OUTPUT inserted.id INTO @IDS
SELECT 'User1' AS [UserID]
,'FirstName'
FROM [dbo].[StatusTable]
INSERT INTO [dbo].[Table2]
([AccountID],[Status])
SELECT Id, 'S' FROM @IDS
Try a set based approach instead of this single row at a time logic. Load the first table, and then you can reference the first table and the data table in the insert to the second table, if you have something that makes each row unique.
you can use a select statement instead of a value list:
insert into table
select rows from othertable (or multiple tables...it's a select statement as complicated as you wish)
Pseudo coded:
Insert into table2 (datacolumns)
select table1.id, datacolumn
from statustable s
inner join table1 t
on (whatever makes these rows unique)