Is it possible to insert into two tables at the same time? SQL Server Interview Question

SQL Server Interview Question: Is it possible to insert into two tables at the same time?

In one statement: No.
In one transaction: Yes
BEGIN TRANSACTION
   DECLARE @DataID int;
   INSERT INTO DataTable (Column1 ...) VALUES (....);
   SELECT @DataID = scope_identity();
   INSERT INTO LinkTable VALUES (@ObjectID, @DataID);
COMMIT

Click here for more than 300 Sql server Interview questions

Comments

  1. Thanks for the post!
    My friend was looking for such kind of information for his work related to virtual data rooms comparison.

    ReplyDelete

Archive

Contact Form

Send