Skip to main content

Microsoft SQL Server (MSSQL)

Airbyte's certified MSSQL connector offers the following features:

  • Multiple methods of keeping your data fresh, including Change Data Capture (CDC) using the binlog.
  • Incremental as well as Full Refresh sync modes, providing flexibility in how data is delivered to your destination.
  • Reliable replication at any table size with checkpointing and chunking of database reads.

Features

FeatureSupportedNotes
Full Refresh SyncYes
Incremental Sync - AppendYes
Replicate Incremental DeletesYes
CDC (Change Data Capture)Yes
SSL SupportYes
SSH Tunnel ConnectionYes
NamespacesYesEnabled by default

The MSSQL source does not alter the schema present in your database. Depending on the destination connected to this source, however, the schema may be altered. See the destination's documentation for more details.

Getting Started

Requirements

  1. MSSQL Server Azure SQL Database, Azure Synapse Analytics, Azure SQL Managed Instance, SQL Server 2019, SQL Server 2017, SQL Server 2016, SQL Server 2014, SQL Server 2012, PDW 2008R2 AU34.
  2. Create a dedicated read-only Airbyte user with access to all tables needed for replication
  3. If you want to use CDC, please see the relevant section below for further setup requirements

1. Make sure your database is accessible from the machine running Airbyte

This is dependent on your networking setup. The easiest way to verify if Airbyte is able to connect to your MSSQL instance is via the check connection tool in the UI.

This step is optional but highly recommended to allow for better permission control and auditing. Alternatively, you can use Airbyte with an existing user in your database.

3. Your database user should now be ready for use with Airbyte!

Airbyte Cloud

On Airbyte Cloud, only secured connections to your MSSQL instance are supported in source configuration. You may either configure your connection using one of the supported SSL Methods or by using an SSH Tunnel.

Change Data Capture (CDC)

We use SQL Server's change data capture feature with transaction logs to capture row-level INSERT, UPDATE and DELETE operations that occur on CDC-enabled tables.

Some extra setup requiring at least db_owner permissions on the database(s) you intend to sync from will be required (detailed below).

Please read the CDC docs for an overview of how Airbyte approaches CDC.

Should I use CDC for MSSQL?

  • If you need a record of deletions and can accept the limitations posted below, CDC is the way to go!
  • If your data set is small and/or you just want a snapshot of your table in the destination, consider using Full Refresh replication for your table instead of CDC.
  • If the limitations below prevent you from using CDC and your goal is to maintain a snapshot of your table in the destination, consider using non-CDC incremental and occasionally reset the data and re-sync.
  • If your table has a primary key but doesn't have a reasonable cursor field for incremental syncing (i.e. updated_at), CDC allows you to sync your table incrementally.

CDC Limitations

  • Make sure to read our CDC docs to see limitations that impact all databases using CDC replication.
  • hierarchyid and sql_variant types are not processed in CDC migration type (not supported by Debezium). For more details please check this ticket
  • CDC is only available for SQL Server 2016 Service Pack 1 (SP1) and later.
  • db_owner (or higher) permissions are required to perform the neccessary setup for CDC.
  • On Linux, CDC is not supported on versions earlier than SQL Server 2017 CU18 (SQL Server 2019 is supported).
  • Change data capture cannot be enabled on tables with a clustered columnstore index. (It can be enabled on tables with a non-clustered columnstore index).
  • The SQL Server CDC feature processes changes that occur in user-created tables only. You cannot enable CDC on the SQL Server master database.
  • Using variables with partition switching on databases or tables with change data capture (CDC) is not supported for the ALTER TABLE ... SWITCH TO ... PARTITION ... statement
  • Our CDC implementation uses at least once delivery for all change records.
  • Read more on CDC limitations in the Microsoft docs.

Setting up CDC for MSSQL

1. Enable CDC on database and tables

MS SQL Server provides some built-in stored procedures to enable CDC.

  • To enable CDC, a SQL Server administrator with the necessary privileges (db_owner or sysadmin) must first run a query to enable CDC at the database level.

    USE {database name}
    GO
    EXEC sys.sp_cdc_enable_db
    GO
  • The administrator must then enable CDC for each table that you want to capture. Here's an example:

    USE {database name}
    GO

    EXEC sys.sp_cdc_enable_table
    @source_schema = N'{schema name}',
    @source_name = N'{table name}',
    @role_name = N'{role name}', [1]
    @filegroup_name = N'{filegroup name}', [2]
    @supports_net_changes = 0 [3]
    GO
    • [1] Specifies a role which will gain SELECT permission on the captured columns of the source table. We suggest putting a value here so you can use this role in the next step but you can also set the value of @rolename to NULL to allow only _sysadmin and db_owner to have access. Be sure that the credentials used to connect to the source in Airbyte align with this role so that Airbyte can access the cdc tables.
    • [2] Specifies the filegroup where SQL Server places the change table. We recommend creating a separate filegroup for CDC but you can leave this parameter out to use the default filegroup.
    • [3] If 0, only the support functions to query for all changes are generated. If 1, the functions that are needed to query for net changes are also generated. If supports_net_changes is set to 1, index_name must be specified, or the source table must have a defined primary key.
  • (For more details on parameters, see the Microsoft doc page for this stored procedure).

  • If you have many tables to enable CDC on and would like to avoid having to run this query one-by-one for every table, this script might help!

For further detail, see the Microsoft docs on enabling and disabling CDC.

2. Enable snapshot isolation

  • When a sync runs for the first time using CDC, Airbyte performs an initial consistent snapshot of your database. To avoid acquiring table locks, Airbyte uses snapshot isolation, allowing simultaneous writes by other database clients. This must be enabled on the database like so:

    ALTER DATABASE {database name}
    SET ALLOW_SNAPSHOT_ISOLATION ON;

3. Create a user and grant appropriate permissions

  • Rather than use sysadmin or db_owner credentials, we recommend creating a new user with the relevant CDC access for use with Airbyte. First let's create the login and user and add to the db_datareader role:

    USE {database name};
    CREATE LOGIN {user name}
    WITH PASSWORD = '{password}';
    CREATE USER {user name} FOR LOGIN {user name};
    EXEC sp_addrolemember 'db_datareader', '{user name}';
    • Add the user to the role specified earlier when enabling cdc on the table(s):

      EXEC sp_addrolemember '{role name}', '{user name}';
    • This should be enough access, but if you run into problems, try also directly granting the user SELECT access on the cdc schema:

      USE {database name};
      GRANT SELECT ON SCHEMA :: [cdc] TO {user name};
    • If feasible, granting this user 'VIEW SERVER STATE' permissions will allow Airbyte to check whether or not the SQL Server Agent is running. This is preferred as it ensures syncs will fail if the CDC tables are not being updated by the Agent in the source database.

      USE master;
      GRANT VIEW SERVER STATE TO {user name};

4. Extend the retention period of CDC data

  • In SQL Server, by default, only three days of data are retained in the change tables. Unless you are running very frequent syncs, we suggest increasing this retention so that in case of a failure in sync or if the sync is paused, there is still some bandwidth to start from the last point in incremental sync.

  • These settings can be changed using the stored procedure sys.sp_cdc_change_job as below:

    -- we recommend 14400 minutes (10 days) as retention period
    EXEC sp_cdc_change_job @job_type='cleanup', @retention = {minutes}
  • After making this change, a restart of the cleanup job is required:

  EXEC sys.sp_cdc_stop_job @job_type = 'cleanup';

EXEC sys.sp_cdc_start_job @job_type = 'cleanup';

5. Ensure the SQL Server Agent is running

  • MSSQL uses the SQL Server Agent

    to run the jobs necessary

    for CDC. It is therefore vital that the Agent is operational in order for to CDC to work effectively. You can check

    the status of the SQL Server Agent as follows:

  EXEC xp_servicecontrol 'QueryState', N'SQLServerAGENT';
  • If you see something other than 'Running.' please follow

    the Microsoft docs

    to start the service.

Connection to MSSQL via an SSH Tunnel

Airbyte has the ability to connect to a MSSQL instance via an SSH Tunnel. The reason you might want to do this because it is not possible (or against security policy) to connect to the database directly (e.g. it does not have a public IP address).

When using an SSH tunnel, you are configuring Airbyte to connect to an intermediate server (a.k.a. a bastion sever) that does have direct access to the database. Airbyte connects to the bastion and then asks the bastion to connect directly to the server.

Using this feature requires additional configuration, when creating the source. We will talk through what each piece of configuration means.

  1. Configure all fields for the source as you normally would, except SSH Tunnel Method.

  2. SSH Tunnel Method defaults to No Tunnel (meaning a direct connection). If you want to use an

    SSH Tunnel choose SSH Key Authentication or Password Authentication.

    1. Choose Key Authentication if you will be using an RSA private key as your secret for

      establishing the SSH Tunnel (see below for more information on generating this key).

    2. Choose Password Authentication if you will be using a password as your secret for establishing

      the SSH Tunnel.

  3. SSH Tunnel Jump Server Host refers to the intermediate (bastion) server that Airbyte will connect to. This should

    be a hostname or an IP Address.

  4. SSH Connection Port is the port on the bastion server with which to make the SSH connection. The default port for

    SSH connections is 22, so unless you have explicitly changed something, go with the default.

  5. SSH Login Username is the username that Airbyte should use when connection to the bastion server. This is NOT the

    MSSQL username.

  6. If you are using Password Authentication, then SSH Login Username should be set to the

    password of the User from the previous step. If you are using SSH Key Authentication leave this

    blank. Again, this is not the MSSQL password, but the password for the OS-user that Airbyte is

    using to perform commands on the bastion.

  7. If you are using SSH Key Authentication, then SSH Private Key should be set to the RSA

    private Key that you are using to create the SSH connection. This should be the full contents of

    the key file starting with -----BEGIN RSA PRIVATE KEY----- and ending

    with -----END RSA PRIVATE KEY-----.

Generating an SSH Key Pair

The connector expects an RSA key in PEM format. To generate this key:

ssh-keygen -t rsa -m PEM -f myuser_rsa

This produces the private key in pem format, and the public key remains in the standard format used by the authorized_keys file on your bastion host. The public key should be added to your bastion host to whichever user you want to use with Airbyte. The private key is provided via copy-and-paste to the Airbyte connector configuration screen, so it may log in to the bastion.

Data type mapping

MSSQL data types are mapped to the following data types when synchronizing data. You can check the test values examples here. If you can't find the data type you are looking for or have any problems feel free to add a new test!

MSSQL TypeResulting TypeNotes
bigintnumber
binarystring
bitboolean
charstring
datedate
datetimetimestamp
datetime2timestamp
datetimeoffsettimestamp with timezone
decimalnumber
intnumber
floatnumber
geographystring
geometrystring
moneynumber
numericnumber
ntextstring
nvarcharstring
nvarchar(max)string
realnumber
smalldatetimetimestamp
smallintnumber
smallmoneynumber
sql_variantstring
uniqueidentifierstring
textstring
timetime
tinyintnumber
varbinarystring
varcharstring
varchar(max) COLLATE Latin1_General_100_CI_AI_SC_UTF8string
xmlstring

If you do not see a type in this list, assume that it is coerced into a string. We are happy to take feedback on preferred mappings.

Upgrading from 0.4.17 and older versions to 0.4.18 and newer versions

There is a backwards incompatible spec change between Microsoft SQL Source connector versions 0.4.17 and 0.4.18. As part of that spec change replication_method configuration parameter was changed to object from string.

In Microsoft SQL source connector versions 0.4.17 and older, replication_method configuration parameter was saved in the configuration database as follows:

"replication_method": "STANDARD"

Starting with version 0.4.18, replication_method configuration parameter is saved as follows:

"replication_method": {
"method": "STANDARD"
}

After upgrading Microsoft SQL Source connector from 0.4.17 or older version to 0.4.18 or newer version you need to fix source configurations in the actor table in Airbyte database. To do so, you need to run two SQL queries. Follow the instructions in Airbyte documentation to run SQL queries on Airbyte database.

If you have connections with Microsoft SQL Source using Standard replication method, run this SQL:

update public.actor set configuration =jsonb_set(configuration, '{replication_method}', '{"method": "STANDARD"}', true)
WHERE actor_definition_id ='b5ea17b1-f170-46dc-bc31-cc744ca984c1' AND (configuration->>'replication_method' = 'STANDARD');

If you have connections with Microsoft SQL Source using Logicai Replication (CDC) method, run this SQL:

update public.actor set configuration =jsonb_set(configuration, '{replication_method}', '{"method": "CDC"}', true)
WHERE actor_definition_id ='b5ea17b1-f170-46dc-bc31-cc744ca984c1' AND (configuration->>'replication_method' = 'CDC');

Changelog

VersionDatePull RequestSubject
4.0.72024-04-0336772Adopt latest CDK.
4.0.62024-03-2536333Deprecate Dbz state iterator.
4.0.52024-03-2136364Allow up to 40 minutes of wait for the an initial CDC record.
4.0.42024-03-2036325[Refactor] : Remove mssql initial source operations .
4.0.32024-03-1936263Fix a failure seen in CDC with tables containing default values.
4.0.22024-03-0635792Initial sync will now send record count in state message.
4.0.12024-03-1236011Read correctly null values of columns with default value in CDC.
4.0.02024-03-0635873Terabyte-sized tables support, reliability improvements, bug fixes.
3.7.72024-03-0635816Fix query that was failing on a case sensitive server.
3.7.62024-03-0435721Fix tests
3.7.52024-02-2935739Allow configuring the queue size used for cdc events.
3.7.42024-02-2635566Add config to throw an error on invalid CDC position.
3.7.32024-02-2335596Fix a logger issue
3.7.22024-02-2135368Change query syntax to make it compatible with Azure SQL Managed Instance.
3.7.12024-02-2035405Change query syntax to make it compatible with Azure Synapse.
3.7.02024-01-3033311Source mssql with checkpointing initial sync.
3.6.12024-01-2634573Adopt CDK v0.16.0.
3.6.02024-01-1033700Remove CDC config options for data_to_sync and snapshot isolation.
3.5.12024-01-0533510Test-only changes.
3.5.02023-12-1933071Fix SSL configuration parameters
3.4.12024-01-0233755Encode binary to base64 format
3.4.02023-12-1933481Remove LEGACY state flag
3.3.22023-12-1433505Using the released CDK.
3.3.12023-12-1233225extracting MsSql specific files out of the CDK.
3.3.02023-12-1233018Migrate to Per-stream/Global states and away from Legacy states
3.2.12023-12-1133330Parse DatetimeOffset fields with the correct format when used as cursor
3.2.02023-12-0733225CDC : Enable compression of schema history blob in state.
3.1.02023-11-2832882Enforce SSL on Airbyte Cloud.
3.0.22023-11-2732573Format Datetime and Datetime2 datatypes to 6-digit microsecond precision
3.0.12023-11-2232656Adopt java CDK version 0.5.0.
3.0.02023-11-0731531Remapped date, smalldatetime, datetime2, time, and datetimeoffset datatype to their correct Airbyte types
2.0.42023-11-06#32193Adopt java CDK version 0.4.1.
2.0.32023-10-3132024Upgrade to Debezium version 2.4.0.
2.0.22023-10-3031960Adopt java CDK version 0.2.0.
2.0.12023-08-2429821Set replication_method display_type to radio, update titles and descriptions, and make CDC the default choice
2.0.02023-08-2229493Set a default cursor for Cdc mode
1.1.12023-07-2428545Support Read Committed snapshot isolation level
1.1.02023-06-2627737License Update: Elv2
1.0.192023-06-2027212Fix silent exception swallowing in StreamingJdbcDatabase
1.0.182023-06-1427335Remove noisy debug logs
1.0.172023-05-2526473CDC : Limit queue size
1.0.162023-05-0125740Disable index logging
1.0.152023-04-2625401CDC : Upgrade Debezium to version 2.2.0
1.0.142023-04-1925345Logging : Log database indexes per stream
1.0.132023-04-1924582CDC : refactor for performance improvement
1.0.122023-04-1725220Logging changes : Log additional metadata & clean up noisy logs
1.0.112023-04-1124656CDC minor refactor
1.0.102023-04-0624820Fix data loss bug during an initial failed non-CDC incremental sync
1.0.92023-04-0424833Fix Debezium retry policy configuration
1.0.82023-03-2824166Fix InterruptedException bug during Debezium shutdown
1.0.72023-03-2724529Preparing the connector for CDC checkpointing
1.0.62023-03-2220760Removed redundant date-time datatypes formatting
1.0.52023-03-2124207Fix incorrect schema change warning in CDC mode
1.0.42023-03-2124147Fix error with CDC checkpointing
1.0.32023-03-1524082Fixed NPE during cursor values validation
1.0.22023-03-1423908Log warning on null cursor values
1.0.12023-03-1023939For network isolation, source connector accepts a list of hosts it is allowed to connect
1.0.02023-03-0623112Upgrade Debezium version to 2.1.2
0.4.292023-02-2416798Add event_serial_no to cdc metadata
0.4.282023-01-1821348Fix error introduced in 18959 in which option initial_waiting_seconds was removed
0.4.272022-12-1420436Consolidate date/time values mapping for JDBC sources
0.4.262022-12-1218959CDC : Don't timeout if snapshot is not complete.
0.4.252022-11-0418732Upgrade debezium version to 1.9.6
0.4.242022-10-2518383Better SSH error handling + messages
0.4.232022-10-2118263Fixes bug introduced in 15833 and adds better error messaging for SSH tunnel in Destinations
0.4.222022-10-1918087Better error messaging for configuration errors (SSH configs, choosing an invalid cursor)
0.4.212022-10-1718041Fixes bug introduced 2022-09-12 with SshTunnel, handles iterator exception properly
2022-10-1315535Update incremental query to avoid data missing when new data is inserted at the same time as a sync starts under non-CDC incremental mode
0.4.202022-09-1415668Wrap logs in AirbyteLogMessage
0.4.192022-09-0516002Added ability to specify schemas for discovery during setting connector up
0.4.182022-09-0314910Standardize spec for CDC replication. Replace the replication_method enum with a config object with a method enum field.
0.4.172022-09-0116261Emit state messages more frequently
0.4.162022-08-1814356DB Sources: only show a table can sync incrementally if at least one column can be used as a cursor field
0.4.152022-08-1115538Allow additional properties in db stream state
0.4.142022-08-1015430fixed a bug on handling special character on database name
0.4.132022-08-0415268Added [] enclosing to escape special character in the database name
0.4.122022-08-0214801Fix multiple log bindings
0.4.112022-07-2214714Clarified error message when invalid cursor column selected
0.4.102022-07-1414574Removed additionalProperties:false from JDBC source connectors
0.4.92022-07-0514379Aligned Normal and CDC migration + added some fixes for datatypes processing
0.4.82022-06-2414121Omit using 'USE' keyword on Azure SQL with CDC
0.4.52022-06-2314077Use the new state management
0.4.32022-06-1713887Increase version to include changes from 13854
0.4.22022-06-0613435Adjust JDBC fetch size based on max memory and max row size
0.4.12022-05-2513419Correct enum for Standard method.
0.4.02022-05-2512759 13168For CDC, Add option to ignore existing data and only sync new changes from the database.
0.3.222022-04-2912480Query tables with adaptive fetch size to optimize JDBC memory consumption
0.3.212022-04-1111729Bump mina-sshd from 2.7.0 to 2.8.0
0.3.192022-03-3111495Adds Support to Chinese MSSQL Server Agent
0.3.182022-03-2911010Adds JDBC Params
0.3.172022-02-2110242Fixed cursor for old connectors that use non-microsecond format. Now connectors work with both formats
0.3.162022-02-1810242Updated timestamp transformation with microseconds
0.3.152022-02-1410256Add -XX:+ExitOnOutOfMemoryError JVM option
0.3.142022-01-249554Allow handling of java sql date in CDC
0.3.132022-01-079094Added support for missed data types
0.3.122021-12-309206Update connector fields title/description
0.3.112021-12-248958Add support for JdbcType.ARRAY
0.3.102021-12-018371Fixed incorrect handling "\n" in ssh key
0.3.92021-11-097748Improve support for binary and varbinary data types
0.3.82021-10-267386Fixed data type (smalldatetime, smallmoney) conversion from mssql source
0.3.72021-09-306585Improved SSH Tunnel key generation steps
0.3.62021-09-176318Added option to connect to DB via SSH
0.3.42021-08-134699Added json config validator
0.3.32021-07-054689Add CDC support
0.3.22021-06-093179Add AIRBYTE_ENTRYPOINT for Kubernetes support
0.3.12021-06-083893Enable SSL connection
0.3.02021-04-212990Support namespaces
0.2.32021-03-282600Add NCHAR and NVCHAR support to DB and cursor type casting
0.2.22021-03-262460Destination supports destination sync mode
0.2.12021-03-182488Sources support primary keys
0.2.02021-03-092238Protocol allows future/unknown properties
0.1.112021-02-021887Migrate AbstractJdbcSource to use iterators
0.1.102021-01-251746Fix NPE in State Decorator
0.1.92021-01-191724Fix JdbcSource handling of tables with same names in different schemas
0.1.92021-01-141655Fix JdbcSource OOM
0.1.82021-01-131588Handle invalid numeric values in JDBC source
0.1.62020-12-091172Support incremental sync
0.1.52020-11-301038Change JDBC sources to discover more than standard schemas
0.1.42020-11-301046Add connectors using an index YAML file