DECLARE @People TABLE (first_name VARCHAR(50), age int);
BEGIN TRAN
INSERT INTO @People VALUES ('John', 25);
INSERT INTO @People VALUES ('Daniel', 30);
ROLLBACK
SELECT * FROM @People;
Answer:
The records inserted into a table variable will not be affected by the transaction rollback.
Reference: Brent Ozar
If you like this SQL interview question, you may also like the below interview question and answers.
Originally published at blog.rajanand.org.
DECLARE @my_age INT = 20;
BEGIN TRAN
SET @my_age += 30;
ROLLBACK;
SELECT @my_age;
Answer:
Even though the transaction is rolled back, the value set to the variable remains same.
Changes to variables aren’t affected by the rollback of a transaction.
Reference: Microsoft docs & Brent Ozar
If you like this SQL interview question, you may also like the below interview question and answers.
Originally published at blog.rajanand.org.