Reputation: 23
How can we insert record in two tables in same stored Procedure in SQL Server. I need just inserted ID field from first table to insert it as reference to second table. As in multi user environment we will have concurrent inserts.
Upvotes: 0
Views: 349
Reputation: 28900
begin tran
Declare @tbl table (id int)
insert into t11
output inserted.* into @tbl
select 1
insert into t2
select * from @tbl
commit
Upvotes: 1
Reputation: 121922
BEGIN TRAN
DECLARE @id INT
INSERT INTO tbl1
VALUES (..)
SET @id = SCOPE_IDENTITY()
INSERT INTO tbl2
VALUES (@id)
COMMIT TRAN
Upvotes: 3