Master SQL Server From Scratch

Clear, interactive, and structured SQL Server lessons designed for beginners and developers.

COURSE SYLLABUS
1. Introduction to SQL Server
Introduction to SQL Server SQL Server Installation SQL Server Fundamentals
2. Data Types
Data Types Numeric Data Types Character & String Data Types Date & Time Data Types Other SQL Server Data Types
3. Database & Tables
Database & Tables Introduction Creating a Database Creating Tables Table Structure Altering Tables Dropping Tables
4. Constraints
Constraints Introduction PRIMARY KEY FOREIGN KEY UNIQUE Constraint NOT NULL & DEFAULT CHECK Constraint
5. SQL Basics
SQL Basics Introduction SQL Commands SQL Command Categories SELECT Statement INSERT, UPDATE & DELETE
6. CRUD Operations
CRUD Operations Introduction INSERT Operation SELECT Operation UPDATE Operation DELETE Operation
7. SQL Queries
Queries Introduction WHERE Clause ORDER BY Clause DISTINCT LIKE Operator IN, BETWEEN & IS NULL
8. Joins
Joins Introduction INNER JOIN LEFT JOIN RIGHT JOIN FULL OUTER JOIN CROSS JOIN
9. SQL Functions
SQL Functions Introduction String Functions Numeric Functions Date & Time Functions Conversion Functions NULL Functions
10. Grouping & Aggregate Functions
GROUP BY Aggregate Functions COUNT() SUM() AVG() MIN() & MAX()
11. Subqueries
Subqueries Introduction Single-Row Subquery Multiple-Row Subquery Correlated Subquery Subquery with DML
12. Views
SQL Views Introduction Creating a View Updating a View Altering a View Dropping a View
13. Stored Procedures
Stored Procedures Introduction Creating a Stored Procedure Input Parameters Output Parameters Executing & Modifying Stored Procedures
14. Triggers
Triggers Introduction AFTER INSERT Trigger AFTER UPDATE Trigger AFTER DELETE Trigger INSTEAD OF Trigger
15. Transactions
Transactions Introduction COMMIT ROLLBACK SAVEPOINT Transaction with TRY...CATCH
16. Indexes
Indexes Introduction Clustered Index Non-Clustered Index Creating & Dropping Indexes Index Performance
17. Common Table Expressions
CTE Introduction Simple CTE CTE with JOIN CTE with Aggregate Functions Recursive CTE
18. Window Functions
Window Functions Introduction ROW_NUMBER() RANK() and DENSE_RANK() LEAD() and LAG() PARTITION BY
19. Advanced SQL
Advanced SQL Introduction Temporary Tables Table Variables Dynamic SQL Error Handling Performance Optimization

SAVEPOINT

Learn how SAVEPOINT is used to create a specific point inside a SQL Server transaction and roll back to that point.

What is a SAVEPOINT?

A SAVEPOINT is a named point created inside a transaction.

It allows us to roll back only part of a transaction instead of rolling back the entire transaction.

In SQL Server, the command SAVE TRANSACTION is used to create a savepoint.

Why Do We Use SAVEPOINT?

  • To create a specific point inside a transaction.
  • To roll back only part of a transaction.
  • To avoid rolling back the complete transaction.
  • To provide more control over transaction operations.

SAVEPOINT Syntax

BEGIN TRANSACTION;

SAVE TRANSACTION SavePointName;

-- SQL statements

ROLLBACK TRANSACTION SavePointName;

COMMIT;

Creating a SAVEPOINT

We can create a savepoint using the SAVE TRANSACTION statement.

BEGIN TRANSACTION;

SAVE TRANSACTION StudentSavePoint;

Here, StudentSavePoint is the name of the savepoint.

Simple SAVEPOINT Example

BEGIN TRANSACTION;

INSERT INTO Students
(
    StudentId,
    StudentName,
    City,
    Age
)
VALUES
(
    401,
    'Rahul',
    'Pune',
    22
);

SAVE TRANSACTION StudentSavePoint;

UPDATE Students
SET City = 'Mumbai'
WHERE StudentId = 401;

ROLLBACK TRANSACTION StudentSavePoint;

COMMIT;

In this example, the INSERT operation remains part of the transaction, while the UPDATE performed after the savepoint is rolled back.

ROLLBACK to a SAVEPOINT

To roll back to a specific savepoint, use ROLLBACK TRANSACTION followed by the savepoint name.

ROLLBACK TRANSACTION StudentSavePoint;

This rolls back the transaction to the specified savepoint rather than rolling back the entire transaction.

Multiple SAVEPOINTS

Multiple savepoints can be created during a transaction.

BEGIN TRANSACTION;

INSERT INTO Students
(
    StudentId,
    StudentName,
    City,
    Age
)
VALUES
(
    402,
    'Priya',
    'Pune',
    21
);

SAVE TRANSACTION Point1;

UPDATE Students
SET Age = 22
WHERE StudentId = 402;

SAVE TRANSACTION Point2;

UPDATE Students
SET City = 'Mumbai'
WHERE StudentId = 402;

ROLLBACK TRANSACTION Point2;

COMMIT;

Here, the transaction contains more than one savepoint, allowing more precise control over the transaction.

SAVEPOINT vs ROLLBACK

SAVEPOINT ROLLBACK
Creates a point inside a transaction. Undoes transaction changes.
Allows partial rollback. Can roll back the entire transaction or to a specified savepoint.
Created using SAVE TRANSACTION. Performed using ROLLBACK TRANSACTION.

Real-World Example

Consider an online order where several operations are performed in sequence:

  1. Create an order.
  2. Add order items.
  3. Update product stock.
  4. Process additional operations.

A savepoint can be created after an important stage. If a later operation fails, the transaction can be rolled back to that savepoint instead of starting everything again.

Important Points

  • SQL Server uses SAVE TRANSACTION to create a savepoint.
  • A savepoint must be created inside a transaction.
  • A transaction can contain multiple savepoints.
  • ROLLBACK TRANSACTION followed by a savepoint name rolls back to that point.
  • A savepoint provides more control than rolling back the complete transaction.
  • COMMIT is used when the final transaction changes should be permanently saved.

Summary

A SAVEPOINT is a named point inside a transaction. It allows us to roll back a portion of the transaction without rolling back everything.

SQL Server creates savepoints using SAVE TRANSACTION and rolls back to them using ROLLBACK TRANSACTION.