Skip to content

Set up a SQL Server destination

The Replicator Service writes to a SQL Server or Azure SQL database through an ODBC driver on the Stelo VM. It creates the destination tables and their indexes, loads baselines and snapshots (with SQL Server bulk copy by default), and applies incremental changes with SQL.

Supported configurations

Supported
SQL Server SQL Server, and Azure SQL Database (see Azure SQL Database). For other cloud services, ask Stelo Support.
Driver on the Stelo VM Microsoft ODBC Driver 18 for SQL Server (recommended) or 17, or the SQDR bundled SQL Server driver (installed and licensed with SQDR). See Databases.
Snapshot replication Yes. With the Microsoft driver, baselines and snapshots load the data with SQL Server bulk copy (Use native-loader function, if available).
Incremental replication Yes, from any source with a Capture Agent.
Apply methods Batch, Merge and Sequence (see Choosing the apply method).
Authentication Remote Credentials (a SQL Server login), or Use Integrated Security (Windows authentication as the account the Replicator Service runs under, on Windows).
Encryption SSL/TLS with Encrypt and TrustServerCertificate. Driver 18 encrypts by default. See Connection strings.

Docker

The SQDR container includes Microsoft ODBC Drivers 17 and 18 for SQL Server (see ODBC drivers in the container).

Prepare the database

The database administrator creates the login and user the Replicator Service connects as, gives it the permissions for the SQL it runs (see What the Replicator Service runs on the database), and sets two database options. For Azure SQL Database, see Azure SQL Database.

Case-sensitive collation

For a destination of incremental groups, give the database a case-sensitive collation (with CS in its name), for example SQL_Latin1_General_CP1_CS_AS. With a case-insensitive collation, two source key values that differ only in case are the same row at the destination. See Keep destination keys the same as the replication key.

Choose the collation when you create the database:

CREATE DATABASE [mydb] COLLATE SQL_Latin1_General_CP1_CS_AS;

To check it:

SELECT collation_name FROM sys.databases WHERE name = 'mydb';

To change the collation of an existing database, see Collation and row versions.

Create the login and user

Run as a member of the sysadmin server role (or a login that can create logins and users):

USE [master];
CREATE LOGIN [sqdruser] WITH PASSWORD = 'mypassword';

USE [mydb];
CREATE USER [sqdruser] FOR LOGIN [sqdruser] WITH DEFAULT_SCHEMA = [dbo];
GRANT CREATE TABLE TO [sqdruser];
GRANT ALTER, SELECT, INSERT, UPDATE, DELETE, REFERENCES ON SCHEMA::[dbo] TO [sqdruser];

Replace sqdruser, mypassword, mydb and the schema dbo with yours. Repeat the USE part for every database that subscriptions write to.

For Windows authentication instead of a SQL Server login, see Integrated Security. For what each permission is for, database roles instead of the grants, and the extra grant for Merge In Place, see Permissions.

Snapshot isolation

Stelo recommends turning on snapshot isolation and read committed snapshot for the destination database:

ALTER DATABASE [mydb] SET ALLOW_SNAPSHOT_ISOLATION ON;
ALTER DATABASE [mydb] SET READ_COMMITTED_SNAPSHOT ON;

READ_COMMITTED_SNAPSHOT can be changed only when no other connection uses the database. To check both settings:

SELECT name, snapshot_isolation_state_desc, is_read_committed_snapshot_on
  FROM sys.databases WHERE name = 'mydb';

What read committed snapshot changes: see Collation and row versions.

tempdb and AUTO_CLOSE

  • tempdb holds the row versions of read committed snapshot and, for Merge groups, the merge tables of up to Maximum Subscriptions tables at a time. Size it for the largest batches.
  • Turn off AUTO_CLOSE for the destination database, and for every SQL Server database SQDR uses (the control database and sources too). A database with AUTO_CLOSE on shuts down when its last connection closes and starts up again on the next one, which makes replication slow and can fail connections with "Database 'name' is being recovered. Waiting until recovery is finished."; the Windows Application event log then shows many "Starting up database 'name'" messages. To list the databases with AUTO_CLOSE on, and turn it off:

    SELECT name FROM sys.databases WHERE is_auto_close_on = 1;
    ALTER DATABASE [mydb] SET AUTO_CLOSE OFF;
    

For faster bulk copy baselines with the BULK_LOGGED recovery model, see Recovery model.

Network

The Stelo VM must reach the SQL Server port, 1433 by default. A named instance can use another port, or the SQL Server Browser on UDP 1434 to find it.

Create the destination

Install the driver, then create the destination as described in Create a destination, with these choices:

  1. Install the ODBC driver on the Stelo VM: Microsoft ODBC Driver 18 for SQL Server (recommended) or 17, from Microsoft. To use the SQDR bundled SQL Server driver instead, see SQDR bundled SQL Server driver.

    Docker

    The SQDR container includes Microsoft ODBC Drivers 17 and 18 for SQL Server.

  2. Name: choose the DBMS Type SQLServer.

  3. Connection: choose ODBC Driver and the driver, then the Template SQL Server, and fill in Server and Database, with Encrypt and Trusted_Connection as needed. Driver 18 checks the server's certificate when it encrypts: install the certificate authority's certificate on the Stelo VM, or add TrustServerCertificate=Yes (see Move to ODBC Driver 18). For Azure SQL Database, see Azure SQL Database.

    For named instances, ports and Always On listeners, see Connection string.

  4. Authentication: Remote Credentials with the login and password, or Use Integrated Security. Click Verify.

  5. Tick Advanced to check the defaults: on the Default step, Database and Object Schema (dbo by default) are where new subscriptions create their tables. See Defaults.

Test the connection. Verify tests the connection, not the permissions, which are first used when a subscription runs. When you create subscriptions, you can test them first with a snapshot subscription of a small table: it creates the table, empties it and loads it with bulk copy. Its replication events show each step and any SQL Server error, for example "CREATE TABLE permission denied in database".

Connection string

With the Microsoft ODBC Driver 17 or 18 for SQL Server, the template is Server=MYHOST;Database=MyDatabase;Encrypt=No;Trusted_Connection=Yes (Driver 18):

Keyword What it does
Server The server: (local) or . for a local default instance, .\SQLEXPRESS for a local named instance, MYHOST, MYHOST\SQLEXPRESS for a named instance, MYHOST,1533 for a port other than 1433.
Database The default database. Subscriptions can still use other databases.
Trusted_Connection=Yes Windows authentication, as the Replicator Service's account. With Use Integrated Security the Replicator Service adds it if it's missing; remove it for a SQL Server login.
Encrypt Yes encrypts the connection and checks the server's certificate; No or Optional doesn't encrypt. ODBC Driver 18 encrypts by default (Yes), so a connection that worked with Driver 17 can fail with a certificate error: install the certificate authority's certificate on the SQDR server, add TrustServerCertificate=Yes, or set Encrypt=No. The Driver 18 template sets Encrypt=No.
TrustServerCertificate=Yes Encrypts without checking the server's certificate: for a certificate from an authority the SQDR server doesn't know, or when you connect with a name or IP address that isn't in the certificate.
MultiSubnetFailover=Yes For an Always On availability group listener or a failover cluster instance: the connection reaches the active node quickly after a failover.

The SQDR bundled SQL Server driver uses HostName=localhost\SQLEXPRESS;EnableQuotedIdentifiers=1; for destinations with this driver, choose Insert using ODBC in subscriptions.

To move an existing source or destination to Driver 18, create a new one with it, because the driver of an existing one can't be changed. See Move to ODBC Driver 18.

Azure SQL Database

  • Copy the ODBC connection string from the Azure portal (the database's Connection strings), without the user and password, for example Server=tcp:myserver.database.windows.net,1433;Database=mydatabase;Encrypt=Yes;TrustServerCertificate=No;Connection Timeout=30. Always include Database.
  • With the SQDR bundled SQL Server driver: HostName=myserver.database.windows.net;EnableQuotedIdentifiers=1;Database=mydatabase.
  • For a destination, set Object Schema to dbo (or your schema) rather than the user ID user@account (see Defaults).

Group settings for SQL Server

When you create an incremental group to this destination, choose the DML Replication method (see Apply):

Merge and Merge In Place

  • Merge loads each table's changes into a ##SQDRWT_<id> table in tempdb, for up to Maximum Subscriptions tables at a time, so size tempdb for it (see tempdb and AUTO_CLOSE).
  • Merge In Place (off by default; on in SQL Server groups created by SQDR 6.x, see Limitations) puts the merge tables next to the destination tables instead, which needs ALTER on their schema (see Permissions).

Check the destination

After the first baseline of the incremental group (for snapshots, after the first run):

  1. Group status. On the Groups page, the group shows Actively monitoring and applying change data (see Group status). If a member failed, its replication events show the SQL Server error, for example "CREATE TABLE permission denied in database". To see the user's permissions in the database:

    EXECUTE AS USER = 'sqdruser';
    SELECT * FROM fn_my_permissions(NULL, 'DATABASE');
    SELECT * FROM fn_my_permissions('dbo', 'SCHEMA');
    REVERT;
    
  2. Rows at the destination. Compare a table's row count with the source:

    SELECT COUNT(*) FROM [mydb].[dbo].[mytable];
    
  3. Changes. Change a few rows at the source; they're applied with the group's next poll (the Poll Interval; see Scheduling). Query the rows again to see them.

What the Replicator Service runs on the database

The permissions in Prepare the database follow from these statements:

When Statements
A subscription is saved or a baseline starts (depending on its Destination DDL Options) DROP TABLE and CREATE TABLE for the destination table, named with its database and schema.
Before a baseline or snapshot copies the data TRUNCATE TABLE, or, with Delete existing data before replication, DELETE TOP (n) FROM ... in batches (see Baseline Replication Options).
During the copy With Use native-loader function, if available (the default), SQL Server bulk copy through the ODBC driver's bulk copy functions, with the TABLOCK hint for the slices of a parallel baseline. With Insert using ODBC, INSERT with ODBC parameter arrays.
Around the copy CREATE INDEX and DROP INDEX ... ON for the index on the replication key (suffix IR) and the source indexes you replicate; ALTER TABLE ... ADD CONSTRAINT ... PRIMARY KEY and DROP CONSTRAINT for Allow UPSERT members and replicated primary keys; ALTER TABLE ... WITH NOCHECK ADD CONSTRAINT ... FOREIGN KEY followed by NOCHECK CONSTRAINT for replicated foreign keys. See The key at the destination.
With Allow disable/enable indexes on the group ALTER INDEX ... DISABLE before the copy and ALTER INDEX ... REBUILD after it, for the table's non-unique, non-clustered indexes, including yours. The Replicator Service finds the indexes left disabled in sys.indexes. See Disabling indexes during a baseline.
Applying changes with Batch or Sequence INSERT, UPDATE ... WHERE <key> and DELETE ... WHERE <key>, one row at a time.
Applying changes with Merge A global temporary table ##SQDRWT_<id> in tempdb, created (or emptied with TRUNCATE TABLE) and loaded with a multi-row INSERT for each table, then one MERGE. Its character key columns get the destination database's collation. See How Merge works.
With Merge In Place A permanent merge table next to the destination table (or in the schema of the Decoration), created, emptied with TRUNCATE TABLE and dropped like a destination table.
ALTER processing ALTER TABLE ... ADD, ALTER COLUMN and DROP COLUMN, and sp_rename (in the destination database) to rename a column. ADD Copy loads a work table (#SQDRCP_<id>, or a regular table next to the destination table when the group allows checkpoint/restart) and updates the destination table from it.

Data types

The Replicator Service maps the source columns to SQL Server types; see Data type mapping and its Per-database notes. For SQL Server:

  • Deprecated types. text, ntext, image and datetime aren't used unless Enable deprecated types is on: text maps to varchar(max) and timestamps to datetime2 (see Options that change the mapping).
  • Timestamps into datetime. A datetime column (with Enable deprecated types, or in a table you created) accepts only years from 1753, and no time of 24:00:00. A Db2 TIMESTAMP value outside that range fails with "The conversion of char to datetime resulted in a value out of range." Use datetime2 (the default), map the column to a character type (see Data type mapping), or convert the value with a derived column.
  • LOBs. varchar(max), nvarchar(max) and varbinary(max) are treated as LOBs (see LOB and long columns).
  • Unicode. Promote CHAR types to wide-CHAR equivalent types creates nchar and nvarchar columns.
  • Special types. money, smallmoney and sql_variant are used only for source columns of the same type. geometry, geography and hierarchyid keep their types (see Spatial and hierarchyid columns and SQL Server sql_variant).
  • Oracle ROWID. An Oracle source's row identifier is created as a character column with the case-sensitive collation Latin1_General_CS_AS (see RRN and ROWID columns).
  • Keys. Key columns are created NOT NULL when forceNonNullPK says so, since SQL Server doesn't allow a primary key on nullable columns.

Limitations

  • Identity columns. The Replicator Service doesn't create identity columns (see What isn't copied) and inserts the source's values. In a table you create yourself, an identity column on a replicated column rejects those values in INSERT statements, and bulk copy replaces them with values SQL Server generates.
  • Triggers and constraints during bulk copy. Bulk copy doesn't fire the destination table's triggers or check its CHECK and foreign key constraints. A trigger that fails while changes are applied causes a polling error (see Group polling error caused by a trigger on the destination table).
  • Replicated foreign keys are created WITH NOCHECK and disabled (NOCHECK CONSTRAINT), so they don't check the rows.
  • Truncate. SQL Server doesn't truncate a table that a foreign key references; use Delete existing data before replication for such tables.
  • Indexes. Indexes are created non-clustered unless the useCluster service property is on. Allow disable/enable indexes leaves a clustered index alone.

Groups created by SQDR 6.x

Merge In Place is on in SQL Server groups created by SQDR 6.x (the setting is kept when you upgrade), so their merge tables are permanent tables in the destination database rather than ## tables in tempdb, and need ALTER on their schema.

Troubleshooting

Problems and messages, with their causes and fixes, are in Troubleshooting › SQL Server.