Imagine an application that manages employee salaries, financial transactions, customer account balances, or approval records. An administrator updates an employee's salary, and the application continues working normally. Months later, someone asks who changed the salary, what the previous value was, and whether the stored history can be trusted.
Traditional audit tables can record these changes, but they introduce an important trust problem. If someone has sufficient database permissions, they may also be able to modify or delete the audit records. A history table alone does not guarantee that the history itself has remained unchanged.
SQL Server Ledger addresses this problem by combining relational tables with cryptographic hashing. It records changes to data and provides a mechanism to verify whether the stored history has been tampered with.
Ledger is particularly useful for applications that manage important records where organizations need stronger evidence of data integrity. It does not replace application authorization, database security, or regular auditing. Instead, it adds a tamper-evidence layer that helps organizations detect unauthorized changes.
This article explains how SQL Server Ledger works, how to create an updatable ledger table, how to inspect historical changes, and how to verify database integrity.
What Is SQL Server Ledger?
SQL Server Ledger is a database feature that uses cryptographic hashing to make unauthorized changes to ledger data detectable.
It was introduced in SQL Server 2022 and is also available in Azure SQL Database and Azure SQL Managed Instance.
The feature is based on two important ideas:
- Recording changes to data in a way that preserves historical information.
- Cryptographically linking database transactions so that changes to previously recorded information can be detected during verification.
Ledger uses a blockchain-inspired structure, but it does not require an external blockchain network or cryptocurrency. The ledger is maintained by the database engine.
When transactions modify ledger tables, SQL Server records transaction information and generates cryptographic hashes. Transactions are organized into blocks, and each block is connected to the previous block through hashing. This creates a chain that can be verified later.
The important distinction is that Ledger provides tamper evidence rather than making every possible form of tampering physically impossible. Someone with sufficient access to database files or infrastructure could potentially modify underlying data outside normal database operations. However, if trusted database digests have been stored separately, subsequent verification can detect inconsistencies.
Understanding the Two Types of Ledger Tables
SQL Server provides two types of ledger tables: updatable ledger tables and append-only ledger tables.
Choosing between them depends on whether your application needs to modify existing records or only insert new records.
Updatable ledger tables
An updatable ledger table supports INSERT, UPDATE, and DELETE operations while preserving historical versions of modified records.
Whenever a row is updated or deleted, SQL Server automatically stores the previous version in a corresponding history table.
For example, consider an employee record:
| Employee ID | Employee Name | Salary |
|---|
| 101 | Rahul | 50000 |
If the salary changes from 50000 to 60000, the current ledger table contains the updated value. The previous version is automatically preserved in the history table.
This allows the application to investigate how the record changed over time without implementing a separate history-writing process for every update.
Updatable ledger tables are suitable for:
- Employee salary and designation records.
- Customer account information.
- Financial and billing records.
- Inventory adjustments.
- Business approval and compliance records.
Append-only ledger tables
Append-only ledger tables allow INSERT operations but prevent UPDATE and DELETE operations through normal database operations.
They are useful when every record represents an event that should remain part of the permanent history.
For example, a security monitoring application might record login attempts, permission changes, or access events. Once an event has been inserted, the application should not be able to modify or remove it.
Append-only ledger tables are suitable for:
- Security audit events.
- Payment event records.
- Access control history.
- Compliance event logs.
- Application activity records.
Unlike updatable ledger tables, append-only ledger tables do not require a separate history table because existing rows cannot be updated or deleted.
Creating an Updatable Ledger Table
Let's create a practical example using an employee salary management application.
The application needs to store employee information and preserve every previous salary value whenever a salary changes.
The following example creates a regular database and an updatable ledger table.
CREATE DATABASE EmployeeLedgerDB;
GO
USE EmployeeLedgerDB;
GO
CREATE TABLE Employees
(
EmployeeId INT NOT NULL PRIMARY KEY,
EmployeeName NVARCHAR(100) NOT NULL,
Department NVARCHAR(100) NOT NULL,
Salary DECIMAL(12, 2) NOT NULL,
LedgerStartTransactionId BIGINT
GENERATED ALWAYS AS TRANSACTION_ID START HIDDEN NOT NULL,
LedgerStartSequenceNumber BIGINT
GENERATED ALWAYS AS SEQUENCE_NUMBER START HIDDEN NOT NULL,
LedgerEndTransactionId BIGINT
GENERATED ALWAYS AS TRANSACTION_ID END HIDDEN NOT NULL,
LedgerEndSequenceNumber BIGINT
GENERATED ALWAYS AS SEQUENCE_NUMBER END HIDDEN NOT NULL
)
WITH
(
SYSTEM_VERSIONING = ON,
LEDGER = ON
);
GO
This table contains the business columns along with four system-generated ledger columns.
The generated columns maintain transaction and sequence information for row versions:
- TRANSACTION_ID START identifies the transaction that created the row version.
- SEQUENCE_NUMBER START identifies the operation sequence within that transaction.
- TRANSACTION_ID END identifies the transaction that ended the row version.
- SEQUENCE_NUMBER END identifies the operation sequence that ended the row version.
The HIDDEN property prevents these system-generated columns from appearing in ordinary SELECT * results. They remain available when explicitly requested.
The SYSTEM_VERSIONING and LEDGER options enable the updatable ledger functionality. SQL Server automatically creates the corresponding history table and a ledger view.
The history table stores earlier versions of employee records, while the ledger view combines information from the current and historical records to provide a change history.
One important design consideration is that ledger functionality cannot simply be removed after a table has been created as a ledger table. Choose the table type and schema carefully before deploying it to production.
Inserting and Updating Employee Records
Once the table is created, normal application operations can insert and update employee information.
Insert an employee:
INSERT INTO Employees
(
EmployeeId,
EmployeeName,
Department,
Salary
)
VALUES
(
101,
'Rahul Sharma',
'Engineering',
50000
);
Now update the employee's salary:
UPDATE Employees
SET Salary = 60000
WHERE EmployeeId = 101;
The current Employees table now contains the latest salary.
SELECT
EmployeeId,
EmployeeName,
Department,
Salary
FROM Employees;
The result is:
| EmployeeId | EmployeeName | Department | Salary |
|---|
| 101 | Rahul Sharma | Engineering | 60000.00 |
The earlier salary value is not lost. SQL Server automatically moves the previous row version into the history table as part of the update.
This is one of the most useful characteristics of updatable ledger tables. Developers do not need to write separate INSERT statements into an audit table every time an employee record changes.
Understanding the Ledger View
SQL Server automatically creates a ledger view for each ledger table.
The view combines the current table and its history table and adds metadata describing the operations performed on the records.
For the Employees table, SQL Server creates a corresponding ledger view. Its default name follows the table name with a Ledger suffix.
You can inspect the available views using:
SELECT
name,
type_desc
FROM sys.views
WHERE name LIKE '%Ledger';
To examine the employee change history, query the generated view:
SELECT *
FROM dbo.MSSQL_LedgerView_Employees;
The exact generated name can depend on the schema and ledger view configuration. Use the name created for your table if it differs from this example.
The ledger view includes the original table columns and additional metadata columns:
- ledger_transaction_id identifies the transaction responsible for the row operation.
- ledger_sequence_number identifies the operation's position within the transaction.
- ledger_operation_type identifies whether the operation inserted or deleted a row version.
- ledger_operation_type_desc provides a readable operation description.
For an update, the ledger view contains two logical operations: one representing the removal of the previous version and another representing the insertion of the new version.
This means an update is represented as a DELETE operation followed by an INSERT operation in the ledger view. It does not mean that the database has lost the original record.
You can use this information to reconstruct the sequence of changes.
For example:
SELECT
EmployeeId,
EmployeeName,
Department,
Salary,
ledger_transaction_id,
ledger_sequence_number,
ledger_operation_type_desc
FROM dbo.MSSQL_LedgerView_Employees
ORDER BY
ledger_transaction_id,
ledger_sequence_number;
This query provides a chronological view of the employee's recorded changes based on ledger transaction metadata.
For application reporting, you can filter by EmployeeId to investigate the history of an individual employee.
Creating an Append-Only Ledger Table
Now consider a different requirement.
A security application records employee access events. Each event should be permanent, and users should not be able to update or delete an existing event through normal SQL operations.
An append-only ledger table is appropriate for this scenario.
CREATE TABLE EmployeeAccessEvents
(
EventId BIGINT IDENTITY(1,1) NOT NULL PRIMARY KEY,
EmployeeId INT NOT NULL,
EventType NVARCHAR(50) NOT NULL,
EventDescription NVARCHAR(500) NOT NULL,
EventTime DATETIME2 NOT NULL
DEFAULT SYSUTCDATETIME()
)
WITH
(
LEDGER = ON
(
APPEND_ONLY = ON
)
);
GO
SQL Server automatically adds the required generated ledger columns when they are not explicitly defined.
You can insert events normally:
INSERT INTO EmployeeAccessEvents
(
EmployeeId,
EventType,
EventDescription
)
VALUES
(
101,
'LOGIN',
'Employee successfully logged in'
);
Another event can be inserted when the employee accesses a protected resource:
INSERT INTO EmployeeAccessEvents
(
EmployeeId,
EventType,
EventDescription
)
VALUES
(
101,
'RESOURCE_ACCESS',
'Employee accessed the payroll dashboard'
);
However, attempts to update or delete existing records are rejected by the database engine.
For example:
DELETE FROM EmployeeAccessEvents
WHERE EventId = 1;
This operation fails because the table is append-only.
This behavior is useful for event records that must remain available for later investigation. However, it also means that data retention and storage requirements must be considered before choosing this table type.
Understanding Database Digests
Recording historical changes is only part of the Ledger feature. The next important step is protecting the integrity information so that it can be verified later.
SQL Server generates database digests containing cryptographic hash information about ledger blocks.
A digest represents the state of the database ledger at a particular point in time. It is important to store these digests outside the database because an attacker who can modify both the database and its digest information could attempt to hide changes.
SQL Server supports storing database digests in external trusted storage, including Azure Confidential Ledger and supported immutable Azure Blob Storage configurations.
You can manually generate a database ledger digest using:
EXEC sys.sp_generate_database_ledger_digest;
The procedure returns digest information that can be stored in an external trusted location.
For production environments, digest management should be automated and monitored. Generating a digest without preserving it in a separately protected location does not provide the same protection as maintaining an independent verification reference.
The external digest is important because it provides a trusted point of comparison when the database is verified later.
Verifying Ledger Data
Suppose an organization wants to verify whether its employee salary history has remained unchanged.
The verification process recalculates the relevant cryptographic hashes and compares them with previously generated database digests.
You can verify the ledger using the following stored procedure:
EXEC sys.sp_verify_database_ledger;
This procedure verifies the database ledger using the available verification information.
For verification against an externally stored digest, use the appropriate verification procedure and supply the trusted digest information.
For example, the following procedure accepts digest information in JSON format:
EXEC sys.sp_verify_database_ledger
N'
[
{
"database_name": "EmployeeLedgerDB",
"block_id": 0,
"hash": "0x..."
}
]';
The hash shown here is a placeholder. In a real environment, use the complete digest generated by SQL Server and stored in your trusted location.
If verification succeeds, the database ledger is consistent with the supplied digest information.
If verification fails, SQL Server reports inconsistencies that require investigation.
A failed verification should be treated as a security and data-integrity incident. Preserve the verification output, investigate the affected records and database environment, and follow your organization's incident response process.
Inspecting Ledger Transactions and Blocks
SQL Server provides system catalog views that help administrators inspect ledger transactions and blocks.
The sys.database_ledger_transactions view contains information about transactions recorded in the database ledger.
SELECT *
FROM sys.database_ledger_transactions
ORDER BY transaction_id DESC;
The sys.database_ledger_blocks view contains information about ledger blocks, including their hashes and links to previous blocks.
SELECT *
FROM sys.database_ledger_blocks
ORDER BY block_id DESC;
These views are useful when investigating the database's ledger structure and understanding how transactions are organized.
Access to ledger metadata is controlled through permissions, including VIEW LEDGER CONTENT. Grant this permission only to users and administrative roles that need to inspect ledger information.
For production monitoring, administrators can use these catalog views alongside digest management and verification procedures to build an operational process for checking ledger integrity.
Important Considerations Before Using Ledger in Production
Ledger provides stronger integrity evidence, but it introduces design and operational considerations that should be understood before adopting it.
Ledger does not replace authorization
Ledger does not decide which users are allowed to modify employee salaries or customer records.
Applications should still use proper authentication, role-based permissions, least-privilege database access, and approval workflows.
For example, an employee should not receive permission to update salary information simply because the table is protected by Ledger. Ledger records changes and helps detect tampering; authorization controls who can perform the changes.
Protect database digests separately
A database digest should not be stored only in the same environment that it is intended to protect.
If an attacker can modify the ledger data and the trusted digest, verification may no longer provide reliable evidence.
Use an independent storage location with appropriate access controls and retention policies. Automate digest generation, storage, and monitoring so that missing digests do not go unnoticed.
Plan for additional storage
Updatable ledger tables retain previous row versions in their history tables. Append-only tables also retain inserted records permanently through normal table operations.
This increases storage requirements over time.
Estimate how quickly records will be inserted or modified, how much history the application will generate, and how that history will affect backups, restores, indexing, and reporting.
Ledger history is not designed for ordinary deletion-based retention cleanup. Deleting older data from append-only ledger tables or from the history table of an updatable ledger table is not supported.
Review feature limitations
Ledger tables have specific limitations involving supported data types, temporal table behavior, and other database features.
For example, types such as XML, sql_variant, FILESTREAM, and vector are listed among the unsupported data types for ledger tables. Some operations available for ordinary tables, such as TRUNCATE TABLE, are also not supported.
Review the documented limitations for the SQL Server or Azure SQL platform you are using before migrating an existing application.
Plan migration carefully
Existing regular tables cannot simply be converted into ledger tables with a single ALTER TABLE statement.
A migration generally requires creating a ledger table, copying the existing data, and validating the migrated records.
The migration process should also consider how historical information will be represented. If the original application has an audit table, determine whether that information needs to be preserved separately or incorporated into the migration process.
Test the migration, application behavior, permissions, and backup strategy before changing production tables.
Conclusion
SQL Server Ledger provides a practical way to make changes to important database records detectable. Updatable ledger tables preserve earlier row versions when records are modified or deleted, while append-only ledger tables protect event records from normal update and delete operations.
The ledger view makes it easier to inspect recorded changes, and database digests provide an independent reference for verifying the integrity of the ledger.
For applications managing financial information, employee records, customer accounts, security events, or compliance data, Ledger can add an important layer of trust.
The strongest implementation combines ledger tables with carefully designed permissions, separately protected database digests, automated verification, storage planning, and a documented incident response process. That combination makes it possible not only to maintain historical records but also to obtain evidence when the integrity of those records is questioned.
Join the conversation! Your thoughts help the community grow.