SQL SAVEPOINT

Roll back part of a transaction without discarding earlier successful work.

What & Why

A savepoint is a named position inside a transaction that ROLLBACK TO SAVEPOINT can restore. It supports controlled recovery during multi-step operations while keeping valid earlier changes pending.

See How It Works

BUSINESS QUESTION

Keep a reviewed campaign status change while undoing a later optional adjustment.

idnamechannelspendstart_dateend_datestatustarget_segment
1Spring Launchgoogle_ads55000.002024-01-152024-03-31activesmb
2Retention Webinaremail45000.002024-02-102024-04-15activeenterprise
3Finance Retargetinglinkedin50000.002024-03-122024-05-31activeenterprise
4Enterprise Searchgoogle_ads60000.002024-04-012024-06-30activeenterprise
EXAMPLE QUERY
BEGIN;
UPDATE marketing.campaigns SET status = 'paused' WHERE id = 1;
SAVEPOINT campaign_paused;
UPDATE marketing.campaigns SET spend = spend * 1.10 WHERE id = 1;
ROLLBACK TO SAVEPOINT campaign_paused;
COMMIT;
SAVEPOINT — partially undo campaign 1 changes
stepcampaign_idstatusspend
BEGIN1active55000.00
SAVEPOINT campaign_paused1paused55000.00
UPDATE after savepoint1paused60500.00
ROLLBACK TO campaign_paused1paused55000.00
COMMIT1paused55000.00

ROLLBACK TO removes only the 10% spend increase made after the savepoint; the earlier status change for campaign ID 1 remains and is committed.

This lesson's practice is part of Pro.

Advanced business practice

Sign up free to try it on a real business scenario