Category: SQL Server DBA | Author: Chreddy Sure | Published On: 04 Jun 2026

Case Study: Implementing Historical Data Replication with Change Tracking in Microsoft SQL Server

Client Industry: Healthcare

Technology Stack: SQL Server, Transactional Replication, T-SQL

Project Type: Database Replication & Data Synchronization

Duration: 4 Weeks

Outcome: Near Real-Time Historical Data Replication

Key Benefits Delivered

  • Implemented near real-time data replication between SQL Server environments.
  • Enabled efficient delta-based data consumption for downstream applications.
  • Preserved historical records by preventing source-side deletions from propagating to the archive database.
  • Eliminated the need for triggers, avoiding additional overhead on the source system.
  • Maintained timestamp integrity even during replication snapshot reinitialization.
  • Delivered a stable and scalable solution that has been running successfully in production for several years.

Background

A client required a solution to replicate data between two Microsoft SQL Server environments while supporting incremental data consumption by downstream applications.

The source environment (Server X) contained a database (D1) with a table (T1). The target environment (Server Y) needed to maintain a synchronized copy of this table for reporting and application access.

Business Requirements

The client requirements were as follows:

  • Replicate data from Server X to Server Y.
  • Applications and users should perform all read operations against Server Y.
  • Support incremental (delta) data retrieval instead of full-table reads.
  • Preserve historical records in the target database even when records are deleted from the source system.
  • Minimize performance impact on the source environment.

Technical Challenge

The source table (T1) did not contain any audit or timestamp columns that could be used to identify newly inserted or modified records.

However, downstream applications required the following metadata columns:

crdate - Timestamp indicating when a record was initially inserted into Server Y

altdate -Timestamp indicating when a record was last modified in Server Y

Applications would use these columns to retrieve only newly inserted or modified records, significantly reducing data transfer and processing overhead.

An additional challenge was that the target database had to function as a historical repository. Therefore:

  • INSERT and UPDATE operations must be replicated.
  • DELETE operations occurring on Server X must not be reflected on Server Y.
  • Records removed from the source system must remain available in the target environment for historical and reporting purposes.

Solution Design

After evaluating multiple approaches, database triggers were ruled out due to concerns around:

  • Performance overhead
  • Increased transaction latency
  • Maintenance complexity
  • Potential impact on source system workloads

Instead, SQL Server Transactional Replication was selected as the foundation of the solution.

Initial Replication Configuration:

The following implementation steps were performed:

Step 1: Configure Transactional Replication

Transactional Replication was established between:

  • Publisher: Server X
  • Subscriber: Server Y

A snapshot was generated and applied to initialize the subscriber database.

Step 2: Add Audit Columns

Immediately after snapshot initialization, the following columns were added to the subscriber table:

crdate DATETIME NOT NULL DEFAULT(GETDATE())
altdate DATETIME NOT NULL DEFAULT(GETDATE())

These columns existed only on Server Y and were not part of the source schema.

Step 3: Prevent DELETE Replication

By default, Transactional Replication propagates DELETE operations to subscribers.

To satisfy the archival requirement, the generated replication stored procedure (sp_MSdel_*) on the subscriber was modified so that DELETE statements were ignored.

As a result:

  • INSERT operations continued to replicate.
  • UPDATE operations continued to replicate.
  • DELETE operations were effectively suppressed.

This allowed Server Y to retain historical records indefinitely.

Snapshot Reinitialization Challenge

A secondary challenge emerged whenever a Snapshot Agent execution was required.

During snapshot reinitialization:

  • Subscriber tables were dropped and recreated.
  • Existing data on Server Y was removed.
  • All records were reinserted as new rows.

Consequently, all records received new crdate and altdate values, causing downstream applications to incorrectly interpret the entire dataset as newly inserted data.

Snapshot Preservation Strategy

To preserve historical timestamps during snapshot refreshes, an additional process was developed.

Step 1: Backup Subscriber Database

A full backup of the subscriber database was taken before snapshot execution.

Step 2: Restore Backup to Temporary Database

The backup was restored to a temporary database (for example, D1_Backup) on Server Y.

Step 3: Execute Snapshot Job

The replication snapshot was generated and applied normally.

Step 4: Recreate Audit Columns

After snapshot completion, the crdate and altdate columns were recreated with default datetime values.

Step 5: Reconcile Historical Data and Timestamps

A custom reconciliation process was implemented after snapshot initialization.

The process compared data between:

  • Current subscriber database
  • Backup database restored before snapshot execution

The comparison was performed using SQL Server TABLEDIFF utility to identify records that existed in the backup database but were missing from the newly initialized subscriber database.

The reconciliation process performed the following actions:

  • Restored historical crdate and altdate values for records that existed before the snapshot.
  • Identified records that were missing after snapshot initialization.
  • Reinserted the missing records from the backup database into the current subscriber database.
  • Allowed genuinely new records introduced by the snapshot to retain their current datetime values.

Logic Applied

  • Existing records retained their historical crdate and altdate values from the backup database.
  • Records identified as missing through the TABLEDIFF comparison were reinserted from the backup database.
  • Newly introduced records retained the current datetime values assigned during snapshot initialization.
  • Historical data integrity was preserved across snapshot refresh operations.

This ensured that downstream applications continued to receive only genuine incremental changes.

Results

The implemented solution successfully achieved all business objectives:

  • Reliable near real-time replication using SQL Server Transactional Replication.
  • Incremental data consumption through crdate and altdate tracking.
  • Historical data retention despite source-side deletions.
  • Elimination of trigger-based overhead on the source system.
  • Preservation of change history during snapshot reinitialization events.
  • Stable production operation over an extended period without reported issues.

Conclusion

By combining SQL Server Transactional Replication with custom subscriber-side enhancements, a robust historical replication framework was established. The solution enabled efficient change tracking, preserved historical records, and supported downstream delta-processing requirements while avoiding the performance concerns associated with trigger-based implementations.

Read More
Category: PLSQL, SQL Development, SQL Server DBA | Author: Chreddy Sure | Published On: 27 May 2026
When we are working with MS SQL Server databases, we are getting the following error in some of the SQL Server instances. Msg 823, Level 24, State 2, Line 2 The operating system returned error 21(The device is not ready.) to SQL Server during a read at offset 0x00000000056000 in file 'E:\SQLDEV22\databasefiles\crystalspidersinst_0700.mdf'. Additional messages in the SQL Server error log and operating system error log may provide more detail. This is a severe system-level error condition that threatens database integrity and must be corrected immediately. Complete a full database consistency check (DBCC CHECKDB). This error can be caused by many factors; for more information, see SQL Server Books Online. This is one of the major issue at Microsoft SQL Server storage-level. Quick Remedy: Restart the SQL Server service The error means SQL Server is trying to read the MDF file from disk E: but Windows unable to access the disk/device properly. The reasons are as follows: The drive may be disconnected External HDD/SSD failure Disk has corruption VM mounted disk issue Storage controller issue Bad sectors Antivirus or any other software locked the file Let's start step by step troubleshoot: Step 1 — Check the Windows Disk Open Command Prompt as Administrator, run the following command to check and repair the bad sectors. I am assuming it is E: drive has database files. /> chkdsk E: /f /r Step 2 — Run DBCC CHECKDB command If the database is online, then run the following command. Replace the database name with your actual database name. DBCC CHECKDB ('YourDatabaseName') WITH NO_INFOMSGS, ALL_ERRORMSGS; If the database is in SUSPECT state, then run the following commands. ALTER DATABASE YourDatabaseName SET EMERGENCY; ALTER DATABASE YourDatabaseName SET SINGLE_USER; DBCC CHECKDB ('YourDatabaseName', REPAIR_ALLOW_DATA_LOSS); ALTER DATABASE YourDatabaseName SET MULTI_USER; Thank you
Read More
Category: SQL Development, SQL Server DBA | Author: Chreddy Sure | Published On: 25 May 2026
While connecting to SQL Server, users may sometimes encounter the following error: “A network-related or instance-specific error occurred while establishing a connection to SQL Server...” This issue usually happens due to incorrect server details, SQL Server service issues, network problems, or permission-related errors. In this article, we will look at common SQL Server connection errors and their possible solutions. Error 1: A Network-Related or Instance-Specific Error Occurred Error Message: “A network-related or instance-specific error occurred while establishing a connection to SQL Server...” Possible Reasons: 1. Incorrect Server Name Make sure the SQL Server name or instance name entered in the connection string or SSMS is correct. 2. SQL Server Service Not Running The SQL Server Database Engine service may not be in the RUNNING state. Solution Open SQL Server Configuration Manager or Services Check the status of: SQL Server SQL Server Browser Start the services if they are stopped 3. Network Connectivity Issues There may be a network-related issue between the client machine and the SQL Server. Solution Verify network connectivity Ping the server Check firewall settings Ensure the SQL Server port is open Error 2: Login Failed for User Error Message “Login failed for user 'peter'. (Framework Microsoft SqlClient Data Provider). Error Number: 18456” Reason This error generally occurs due to insufficient permissions or invalid login credentials. Solution Contact the Database Administrator (DBA) and ensure: The user account has proper access permissions SQL authentication is enabled (if required) The login is mapped to the required database Error 3: SSL Certificate Trust Issue Error Message: “A connection was successfully established with the server, but then an error occurred during the login process. (provider: SSL Provider, error: 0 - The certificate chain was issued by an authority that is not trusted.) (Framework Microsoft SqlClient Data Provider)” Reason: This issue occurs because the SSL certificate used by SQL Server is not trusted by the client machine. Solution: You can temporarily disable encrypted connections in SQL Server Management Studio (SSMS). Steps to Fix Open SQL Server Management Studio (SSMS) In the login window, click Options Go to the Connection Properties tab Uncheck the option: Encrypt Connection Click Connect
Read More
1