sach
sach

Reputation: 23

Insert record in two tables with identity field value from first table

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

Answers (2)

TheGameiswar
TheGameiswar

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

Devart
Devart

Reputation: 121922

BEGIN TRAN

    DECLARE @id INT

    INSERT INTO tbl1
    VALUES (..)

    SET @id = SCOPE_IDENTITY()

    INSERT INTO tbl2
    VALUES (@id)

COMMIT TRAN

Upvotes: 3

Related Questions