Modifying data safely
INSERT
Insert a row in a transaction and undo it after inspection.
Lesson 45 of 84
Reading: about 2 min
Lesson + practice: about 15 min
Difficulty: Intermediate
On this page4 sections
This lesson uses DDI_SQL_Training dataset v1.0.0 on SQL Server 2019 or later. Insert a row in a transaction and undo it after inspection. Run changing examples only on a disposable training instance. The example uses a session-local object or rolls back its changes.
Run the example
BEGIN TRANSACTION;
INSERT INTO dbo.Categories (CategoryID, CategoryName)
VALUES (99, N'Temporary lesson category');
SELECT CategoryID, CategoryName FROM dbo.Categories WHERE CategoryID = 99;
ROLLBACK TRANSACTION;Read it step by step
- The column list names the two required values explicitly.
- The SELECT shows the inserted row while the transaction is open.
- ROLLBACK removes the row before the session ends, leaving v1.0.0 unchanged.
Check the result
| CategoryID | CategoryName |
|---|---|
| 99 | Temporary lesson category |
The result is visible inside the transaction only; CategoryID 99 is absent after rollback.
Practice
Insert a temporary CategoryID 98, inspect it, and roll back.
Check your answer and expected result
Reveal answer
BEGIN TRANSACTION;
INSERT INTO dbo.Categories (CategoryID, CategoryName)
VALUES (98, N'Practice category');
SELECT CategoryID, CategoryName FROM dbo.Categories WHERE CategoryID = 98;
ROLLBACK TRANSACTION;| CategoryID | CategoryName |
|---|---|
| 98 | Practice category |
The row appears before rollback and is absent afterward.
Your progress
Reveal the check answer or reach the end to complete this lesson.