Modifying data safely
UPDATE
Change a targeted row in a transaction without changing the shared dataset.
Lesson 46 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. Change a targeted row in a transaction without changing the shared dataset. 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;
UPDATE dbo.Products SET ListPrice = ListPrice + 1.00
WHERE ProductID = 1;
SELECT ProductID, ListPrice FROM dbo.Products WHERE ProductID = 1;
ROLLBACK TRANSACTION;Read it step by step
- WHERE ProductID = 1 targets one product key.
- The SELECT displays the temporary new price while the transaction is open.
- ROLLBACK restores the original price for later lessons.
Check the result
| ProductID | ListPrice |
|---|---|
| 1 | 40.00 |
The displayed price is temporary; the unmodified product still costs 39.00 afterward.
Practice
Temporarily set ProductID 2 IsActive to 0, inspect it, and roll back.
Check your answer and expected result
Reveal answer
BEGIN TRANSACTION;
UPDATE dbo.Products SET IsActive = 0 WHERE ProductID = 2;
SELECT ProductID, IsActive FROM dbo.Products WHERE ProductID = 2;
ROLLBACK TRANSACTION;| ProductID | IsActive |
|---|---|
| 2 | 0 |
The flag is 0 only inside the transaction; rollback restores it to 1.
Your progress
Reveal the check answer or reach the end to complete this lesson.