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

Post a Comment

1 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.
  1. Thanks for the post!
    My friend was looking for such kind of information for his work related to virtual data rooms comparison.

    ReplyDelete