0

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.

James Z
  • 12,104
  • 10
  • 27
  • 43
sach
  • 23
  • 2

2 Answers2

3
BEGIN TRAN

    DECLARE @id INT

    INSERT INTO tbl1
    VALUES (..)

    SET @id = SCOPE_IDENTITY()

    INSERT INTO tbl2
    VALUES (@id)

COMMIT TRAN
Devart
  • 115,199
  • 22
  • 161
  • 180
1
begin tran
Declare @tbl table (id int)

insert into t11
output inserted.* into @tbl
select 1


insert into t2
select * from @tbl
commit
TheGameiswar
  • 26,582
  • 7
  • 53
  • 87