Keep the first edit and undo the second
Follow a temporary table through a savepoint, rollback, and commit.
What you are trying to do
A ticket starts at 5 points. You accept an estimate of 8, then try a second estimate before deciding which change to keep.
Work through it
- Open the SQL in the walkthrough and find the two UPDATE steps.
- Compare the rows before and after each write. Find the point where the second estimate replaces the first.
- Follow the rollback and read the first SELECT. Then follow the commit and read the second SELECT.
- In a disposable SQL Server database, run the script and compare the two results.
Check your work
Both reads show 8 points. The read after the savepoint rollback shows one open transaction. The read after COMMIT shows zero.
Practice SQL
Open in SQL FlowCREATE TABLE #TicketEstimate (TicketId int, Points int);
INSERT INTO #TicketEstimate VALUES (101, 5);
BEGIN TRANSACTION;
UPDATE #TicketEstimate SET Points = 8 WHERE TicketId = 101;
SAVE TRANSACTION FirstEstimate;
UPDATE #TicketEstimate SET Points = 12 WHERE TicketId = 101;
ROLLBACK TRANSACTION FirstEstimate;
SELECT Points, @@TRANCOUNT AS OpenCount FROM #TicketEstimate;
COMMIT TRANSACTION;
SELECT Points, @@TRANCOUNT AS OpenCount FROM #TicketEstimate;
DROP TABLE #TicketEstimate;
Knowledge check
Commands in this lesson
Next lesson Expand a bill of materials without looping