Read our blogs, tips and tutorials
Try our exercises or test your skills
Watch our tutorial videos or shorts
Take a self-paced course
Read our recent newsletters
License our courseware
Book expert consultancy
Buy our publications
Get help in using our site
544 attributed reviews in the last 3 years
Refreshingly small course sizes
Outstandingly good courseware
Whizzy online classrooms
Wise Owl trainers only (no freelancers)
Almost no cancellations
We have genuine integrity
We invoice after training
Review 30+ years of Wise Owl
View our top 100 clients
Search our website
We also send out useful tips in a monthly email newsletter ...
Written by Andrew Gould
Normally, when you rollback a transaction SQL Server will undo everything that has happened since the transaction began. You can, however, provide yourself with a way to rollback only part of a transaction using something called a savepoint.
You can create a savepoint at any stage after beginning a transaction, as shown in the code below:
BEGIN TRAN Tran1
INSERT INTO tblTest(TestName)
VALUES ('A')
SAVE TRAN Savepoint
INSERT INTO tblTest(TestName)
VALUES ('B')
ROLLBACK TRAN Savepoint
INSERT INTO tblTest(TestName)
VALUES ('C')
COMMIT TRAN Tran1
Once a savepoint has been created you can choose to rollback to that point at any stage up until you commit or rollback the entire transaction. In the code above, anything that is done between SAVE TRAN and ROLLBACK TRAN will be rolled back, while anything that occurs before or after these lines will be committed.
If we examine the contents of tblTest after executing the code we would see the following values:
The second value that was added to the table doesn't appear because we rolled back this section of the transaction.
If you begin a transaction in one procedure and then call another procedure the transaction remains open. You can create a savepoint within the called procedure to allow you to rollback any actions that are performed within it.
The code below shows a simple procedure to add a new film to our database. Within this procedure we call another one to assign the correct director to the film.
CREATE PROC spAddFilm(
@Title VARCHAR(MAX)
,@ReleaseDate DATETIME
,@Director VARCHAR(MAX))
AS
BEGIN
DECLARE @FilmID INT
DECLARE @DirectorID INT
BEGIN TRAN AddFilm
--Add a new record to the Film table
INSERT INTO tblFilm(FilmName, FilmReleaseDate)
VALUES (@Title, @ReleaseDate)
--Store the Id of the film just added
SET @FilmID = @@IDENTITY
--Call stored procedure to get the DirectorID
EXEC @DirectorID = spAssignDirector @Director
--Assign the DirectorID to the film added earlier
UPDATE tblFilm
SET FilmDirectorID = @DirectorID
WHERE FilmID = @FilmID
COMMIT TRAN AddFilm
END
The procedure that is called in the middle of this one is used to retrieve the Id number of the film's director. The code for this is shown below:
CREATE PROC spAssignDirector(
@Director VARCHAR(MAX))
AS
BEGIN
DECLARE @ID INT
--Create a savepoint
SAVE TRAN AddDirector
--Insert the new director name into the table
INSERT INTO tblDirector(DirectorName)
VALUES (@DirectorName)
--If there is more than one director with the same name,
--Rollback to the savepoint
IF (SELECT COUNT(*) FROM tblDirector WHERE DirectorName=@Director) > 1
ROLLBACK TRAN AddDirector
--Store the ID of the director
SELECT @ID = DirectorID
FROM tblDirector
WHERE DirectorName = @Director
--Return the DirectorID to the calling procedure
RETURN @ID
END
This procedure creates a savepoint before it attempts to modify any data. It then inserts a new record into the director table and checks to see if there is more than one director with the same name. If that's true then the transaction is rolled back to the savepoint created at the start of this procedure, without affecting any actions that have taken place in the calling procedure.
This is a fairly inefficient way to achieve the result we want! We're using this method purely to demonstrate how savepoints work in stored procedures.
You can learn more about this topic on the following Wise Owl courses:
Kingsmoor House
Railway Street
GLOSSOP
SK13 2AA
Landmark Offices
99 Bishopsgate
LONDON
EC2M 3XD
Holiday Inn
25 Aytoun Street
MANCHESTER
M1 3AE
© Wise Owl Business Solutions Ltd 2024. All Rights Reserved.