Connecting to Any ODBC Data Source as a Linked Server



You can use the TDS Remoting feature of the SQL Gateway to set up a linked server for any CData ODBC data source. Either use the UI in SQL Server Management Studio or call stored procedures to create the linked server. You can then work with your ODBC data source just as you would a linked SQL Server instance.

Want to know why you should use TDS remoting to create a linked server? Learn more.

Configure the TDS Remoting Service

The SQL Gateway runs a service that listens for requests from clients in SQL Server's TDS protocol. The service can be configured in the UI for the SQL Gateway: See the setup guide in the SQL Gateway overview.

Create a Linked Server for the ODBC Data Source

After you have configured and started the TDS remoting service, create the linked server and connect. You can use the UI in SQL Server Management Studio or call stored procedures.

Create a Linked Server from the UI

Follow the steps below to create a linked server from the Object Explorer.

  1. Open SQL Server Management Studio and connect to an instance of SQL Server.
  2. In the Object Explorer, expand the node for the SQL Server database. In the Server Objects node, right-click Linked Servers and click New Linked Server. The New Linked Server dialog is displayed.
  3. In the General section, click the Other Data Source option and enter the following information after naming the linked server:
    • Provider: Select the SQL Server Native Client Provider that corresponds to your version of SQL Server. For example, SQL Server Native Client 11.0.
    • Data Source: Enter the host and port the TDS remoting service is running on, separated by a comma.

      Note that a value of "localhost" in this input refers to the machine where SQL Server is running so be careful when creating a linked server in Management Studio when not running on the same machine as SQL Server.

    • Catalog: Enter the DSN for the ODBC data source you wish to connect to.
  4. In the Security section, select the option to have the connection "made using this security context" and enter the username and password of a user you created in the Users tab of the SQL Gateway.

Create a Linked Server Programmatically

In addition to using the SQL Server Management Studio UI to create a linked server, you can use stored procedures. The following inputs are required:

  • server: The linked server name.
  • provider: Enter "SQLNCLI", for the SQL Server Native Client Provider.
  • datasrc: The host and port the service is running on, separated by a comma.

    Note that a value of "localhost" in the datasrc input refers to the machine where SQL Server is running, so be careful when creating a linked server in Management Studio when not running on the same machine as SQL Server.

  • catalog: Enter the system DSN configured for the service.
  • srvproduct: Enter the product name of the data source; this can be an arbitrary value, such as "CData SQL Gateway" or an empty string.
Follow the steps below to create the linked server and configure authentication to the SQL Gateway:
  1. Call sp_addlinkedserver to create the linked server:

    EXEC sp_addlinkedserver @server='SALESFORCE',
      @provider='SQLNCLI',
      @dataclass=lazyload data-src='<MachineIPAddress>,1433',
      @catalog='CData Salesforce Sys',
      @srvproduct ='';
    GO
  2. Call the sp_addlinkedsrvlogin stored procedure to allow SQL Server users to connect with the credentials of an authorized user of the TDS remoting service. Note that the credentials you use to connect to the service must exist as credentials in the SQL Gateway application.

    EXEC sp_addlinkedsrvlogin @rmtsrvname='SALESFORCE',
      @rmtuser='admin',
      @rmtpassword='test',
      @useself='FALSE',
      @locallogin=NULL;
    GO

Connect from SQL Server Management Studio

SQL Server Management Studio uses the SQL Server Client OLE DB provider, which requires the ODBC driver to be used inprocess. You must enable the 'Allow inprocess' option for the SQL Server Native Client Provider in Management Studio to query the linked server from SQL Server Management Studio. To do this, open the properties for the provider you are using under Server Objects -> Linked Servers -> Providers. Check the "allow inprocess" option and save the changes.

Execute Queries

You can now execute queries to the linked server from any tool that can connect to SQL Server. Set the table name accordingly:

SELECT * FROM [LINKED_SERVER].[CATALOG].[SCHEMA].[TABLE_NAME]