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,209
  • 10
  • 24
  • 44
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
  • 119,203
  • 23
  • 166
  • 186
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
  • 27,855
  • 8
  • 56
  • 94