More about preparing a SQL Server destination¶
Azure SQL Database¶
-
Create a contained database user in the destination database instead of a login, with the same permissions:
CREATE USER [sqdruser] WITH PASSWORD = 'mypassword'; GRANT CREATE TABLE TO [sqdruser]; GRANT ALTER, SELECT, INSERT, UPDATE, DELETE, REFERENCES ON SCHEMA::[dbo] TO [sqdruser]; -
Allow the public IP address of the Stelo VM (where SQDR runs, usually with Stelo Capture) in the server's firewall rules. That's the address Azure sees, not the server's own: behind NAT, a firewall or a cloud gateway, it's the gateway's public egress IP. To find it, ask your network team, or open a site that shows your public IP from the server.
- Azure SQL Database turns snapshot isolation and read committed snapshot on by default; check them with the query in Snapshot isolation. The recovery model can't be changed.
To connect to it, see Azure SQL Database connection.
Integrated Security¶
With Use Integrated Security, the Replicator Service connects as the Windows account its service runs under, set in
the SQDR Configuration Tool (see Change the service
account). Create the login for that account instead of a
SQL Server login, for example CREATE LOGIN [MYDOMAIN\sqdrsvc] FROM WINDOWS;, and the user for that login. On a new
installation the service runs as Local System, which connects to a SQL Server on another computer as the Stelo VM's
computer account: CREATE LOGIN [MYDOMAIN\MYSERVER$] FROM WINDOWS; (where MYSERVER is the Stelo VM's name). Repeat
the USE part for every database that subscriptions write to.
Permissions¶
| Permission | Why it's needed |
|---|---|
CREATE TABLE (database) |
To create tables. SQL Server also requires ALTER on the schema the table goes into. |
ALTER on the schema |
To create, alter and drop the tables in the schema, and everything that needs ALTER on a table: TRUNCATE TABLE, CREATE INDEX, DROP INDEX, ALTER INDEX, primary and foreign keys, sp_rename, and bulk copy into a table with constraints or triggers. |
SELECT on the schema |
UPDATE and DELETE statements whose WHERE clause reads the key columns, and the MERGE. |
INSERT, UPDATE, DELETE on the schema |
Loading and applying changes. Bulk copy needs INSERT. |
REFERENCES on the schema |
Only with Replicate Foreign Keys: to create foreign keys between the tables. |
Instead of the grants, you can make the user a member of the db_ddladmin, db_datareader and db_datawriter
roles, which give these permissions on every schema of the database.
The merge tables of Merge groups are global temporary tables in tempdb, which every user can create, so they need
no permission. With Merge In Place, or a schema in the Decoration, grant ALTER on that schema too.
Collation and row versions¶
Changing the database's collation later (ALTER DATABASE ... COLLATE) doesn't convert existing columns: existing tables
keep their old collation. See Microsoft: Set or change the database
collation.
tempdb may have another collation: the Replicator Service gives the key columns of its ## merge tables the
destination database's collation, so the MERGE compares keys the same way as the destination table.
With read committed snapshot, queries on the destination tables use row versions instead of shared locks: they don't
block the Replicator Service while it applies changes, and it doesn't block them. SQL Server keeps the row versions in
tempdb, which needs room for them.
Temporal tables¶
SQDR has no special support for SQL Server temporal (system-versioned) tables, but a subscription can fill one that you create yourself:
-
Create the destination table (for example with a first baseline), then turn it into a temporal table: give it a primary key, add the period columns and turn on system versioning with a history table. For example:
-- 1. A temporal table needs a primary key ALTER TABLE dbo.MYTABLE ADD CONSTRAINT PK_MYTABLE PRIMARY KEY CLUSTERED (MYKEY); -- 2. The period columns ALTER TABLE dbo.MYTABLE ADD BeginDate datetime2 GENERATED ALWAYS AS ROW START NOT NULL DEFAULT SYSUTCDATETIME(), EndDate datetime2 GENERATED ALWAYS AS ROW END NOT NULL DEFAULT CAST('9999-12-31 23:59:59.9999999' AS datetime2), PERIOD FOR SYSTEM_TIME (BeginDate, EndDate); -- 3. Turn on system versioning, with a history table ALTER TABLE dbo.MYTABLE SET (SYSTEM_VERSIONING = ON (HISTORY_TABLE = dbo.MYTABLE_History)); -
Set the subscription's Destination DDL Options to Use existing table (Use existing table for baseline), so that SQDR doesn't drop and re-create the table. The period columns aren't source columns, so they stay out of the subscription; SQL Server fills them.
- SQL Server doesn't truncate a system-versioned table: choose Delete existing data before replication in the Baseline Replication Options. The deleted rows go to the history table.
Recovery model¶
Bulk copy baselines can be faster with the BULK_LOGGED recovery model, which logs bulk operations minimally:
ALTER DATABASE MYDB SET RECOVERY BULK_LOGGED
Check the effect on your backups first: a log backup that contains bulk-logged operations can't be restored to a
point in time within it. Switch back with SET RECOVERY FULL if you need that.