How do I connect Microsoft SQL Server to Salesforce?

Use the Windows Salesforce ODBC driver.

  • Install and license the Salesforce ODBC driver on the machine where SQL Server is installed.

    Note If you want to use the linked server in a clustered environment, you need to install the ODBC driver on each node in the cluster.

    For installation instructions, refer to the Salesforce ODBC driver documentation.

  • Before you can use the Salesforce ODBC driver to connect SQL Server to Salesforce, you need to configure an ODBC data source. An ODBC data source stores the connection details for the target database (in this case, Salesforce) and the ODBC driver that is required to connect to it (in this case, the Salesforce ODBC driver).

    Note If you want to use the linked server in a clustered environment, you need to configure the data source on each node in the cluster.

    You can configure a user ODBC data source, which is only available to the user who creates it, or a system ODBC data source, which is available to all users on the machine. You configure ODBC data sources in ODBC Data Source Administrator, which is included with Windows.

    There are two versions of ODBC Data Source Administrator. The version of ODBC Data Source Administrator that you need to run depends on whether you have a 32-bit or a 64-bit version of SQL Server. To find out which version of SQL Server you have, connect to your SQL Server instance, and then run this SQL statement:

    select SERVERPROPERTY('edition')
    

    If you have the 64-bit version of SQL Server and want to use a linked server with the Salesforce ODBC driver, you need to run the 64-bit version of ODBC Data Source Administrator. To do this, in the Windows Run dialog box, type:

    odbcad32.exe

    If you have the 32-bit version of SQL Server or want to use SSIS with the Salesforce ODBC driver, you need to run the 32-bit version of ODBC Data Source Administrator. To do this, in the Windows Run dialog box, type:

    %windir%\syswow64\odbcad32.exe

    Use ODBC Data Source Administrator to create your Salesforce ODBC driver data source:

    1. Do one of the following:
      • To create a user data source, in the User DSN tab, choose Add.

        Important This will only succeed if the SQL Server instance is running under the same user account as the one used to create the data source. Otherwise, you must create a System ODBC data source instead.

        –Or–

      • To create a system data source, choose the System DSN tab, and then choose Add.
    2. In the Create New Data Source dialog box, choose Easysoft ODBC-Salesforce Driver, and then choose Finish.
    3. Complete the DSN Setup dialog box:
      Setting Value
      DSN Salesforce
      User Name The name of your Salesforce user. For example, myuser@mydomain.com.
      Password The password for your Salesforce user.
      Token The security token for your Salesforce user, if required.

      To find out whether you need to supply a security token, choose the Test button. If the connection attempt fails with an error which contains LOGIN_MUST_USE_SECURITY_TOKEN, you need to supply one.

      Salesforce emails the security token to the email address associated with your Salesforce user account. If you have not received a security token, you can regenerate it. Salesforce will then email the new security token to you. To regenerate your security token, log in to Salesforce and then choose Setup from the user menu. Search for "security token" in the Quick Find box. Choose Reset Security Token in the Reset Security Token page. When you receive the token in your email client, copy it and then paste it into the Token field.

    4. Use the Test button to verify that you can successfully connect to Salesforce.

    You can now connect SQL Server to Salesforce.

    Example: Retrieve Salesforce data by using a linked server

    1. In Microsoft SQL Server Management Studio, connect to the SQL Server instance you want to create the linked server against.

      You need to log on with an account that is a member of the SQL Server sysadmin fixed server role to create a linked server.

    2. Right-click Server Objects. From the pop-up menu choose New > Linked Server.
    3. In the Linked server box, enter "Salesforce".

      (If you want to call the Linked server "Salesforce," you will have to enclose this name in double quotes in your SQL statements. For example, SELECT * FROM "SALESFORCE.COM"...Account.)

    4. From the Provider list, choose Microsoft OLE DB Provider for ODBC drivers.
    5. In the Data source box, enter the name of your Salesforce ODBC data source, and then choose OK.

      SQL Server verifies the linked server by testing the connection.

      • If you get the error "Specified driver could not be loaded due to system error 126: The specified module could not be found," choose Yes when prompted whether to keep the linked server. You need to restart your SQL Server instance before you can use the linked server. If SQL Server was already running when you installed the Salesforce ODBC driver, it will not have the latest version of the System Path environment variable. The Salesforce ODBC driver Setup program adds entries for the driver to the System Path. Restarting the instance makes these changes available to SQL Server, allowing it to load the Salesforce ODBC driver.
      • If you made a mistake when specifying the Salesforce ODBC data source, you get the error "Data source name not found and no default driver specified." If you get this error, choose No when prompted whether to keep the linked server and edit the value in the Data source box.
    6. You can query your Salesforce data either by using a:
      • Four part table name in a distributed query.

        A four part table name has the format:

        server_name.[database_name].[schema_name].table_name.

        Even though with Salesforce there is no database or schema, the Easysoft ODBC-Salesforce Driver returns a "dummy" value for both identifiers, because some ODBC applications expect there to be a database and a schema. The database that the driver returns is SF. The schema that the driver returns is DBO. Include these identifiers in your SQL statements. For example:

        SELECT * FROM SALESFORCE.SF.DBO.Account
        

        The capitalisation of the table name must be the same as it is in Salesforce. For example, the following query is invalid:

        SELECT * FROM SALESFORCE.SF.DBO.ACCOUNT
        

        To check the capitalisation of the Salesforce tables (objects), run:

        EXEC sp_tables_ex @table_server = 'SALESFORCE'
        
      • Pass-through query in an OPENQUERY function. For example:
        SELECT * FROM OPENQUERY(SALESFORCE, 'SELECT * FROM ACCOUNT')
        

        SQL Server sends pass-through queries as uninterpreted query strings to the Salesforce ODBC driver. This means that SQL Server does not apply any kind of logic to the query or try to estimate what that query will do.

    Example: Retrieve Salesforce data by using OPENDATASOURCE and OPENROWSET

    The OPENDATASOURCE and OPENROWSET functions enable you to work with Salesforce data in SQL Server without configuring a linked server. There are some security implications associated with their use and these functions are therefore not enabled by default.

    1. In Microsoft SQL Server Management Studio, run these commands to enable the OPENDATASOURCE and OPENROWSET functions:
      EXEC sp_configure 'show advanced options', 1
      RECONFIGURE
      GO
      EXEC sp_configure 'ad hoc distributed queries', 1
      RECONFIGURE
      GO
    2. Run:
      SELECT * FROM OPENDATASOURCE('MSDASQL', 'DSN=MY_SALESFORCE_ODBC_DATA_SOURCE;') .SF.DBO.Account

      –Or–

      SELECT * FROM OPENROWSET('MSDASQL', 'DSN=MY_SALESFORCE_ODBC_DATA_SOURCE;', 'select * from Account;')

    Example: Back up a Salesforce table in SQL Server

    This command creates a copy of the Account table, it assumes the linked server is named SALESFORCE.

    IF OBJECT_ID('dbo.Account', 'U') IS NOT NULL DROP TABLE dbo.Account;
    SELECT * INTO Account FROM OPENQUERY(SALESFORCE,'SELECT * FROM Account')
    

    Example: Integrate Salesforce with SQL Server by using SQL Server Integration Services (SSIS)

    These instructions assume that you have Microsoft Visual Studio and SQL Server Data Tools for Visual Studio installed.

    1. Create a .csv file named product.csv with the following contents:
      Name,Description,Family
      Easysoft ODBC-Salesforce Driver,"ODBC driver for Salesforce, Force.com, Database.com",Easysoft Data Access
      Easysoft ODBC-SQL Server Driver,"ODBC driver for SQL Server, SQL Azure",Easysoft Data Access
    2. In Visual Studio, create a new Integration Services Project.
    3. Drag a Data Flow Task from the Toolbox to the Control Flow tab.
    4. Choose the Data Flow tab.
    5. Drag a Flat File Source from the Toolbox to the Data Flow tab, and then press Return.

      Flat File Source is under the Other Sources list.

    6. In the Flat File Source Editor, choose New.
    7. In the Flat File Connection Manager Editor dialog box, browse for your .csv file.
    8. In the Text qualifier box, enter ".
    9. Choose Preview, and then choose OK to exit the dialog boxes.
    10. Drag an ODBC Destination from the Toolbox to the Data Flow tab, and then press Return.

      ODBC Destination is in the Other Destinations list.

    11. Select the Flat File Source. Drag the blue arrow over to the ODBC Destination.
    12. Select the ODBC Destination, and then press Return.
    13. In the ODBC Destination dialog box, choose New.
    14. In the Configure ODBC Connection Manager dialog box, choose New.
    15. In the Connection Manager dialog box, choose your Salesforce ODBC data source, and then use the OK button to return to the ODBC Source dialog box.
    16. In the Name of the table or the view list, choose Product2.
    17. Choose Mappings.
    18. Map the Name, Description and Family columns in the Input list to the same columns in the Destination list.

      Your Input and Destination columns should look like this:

      Input Column Destination Column
      Name Name
      <ignore> ProductCode
      Description Description
      <ignore> IsActive
      <ignore> CreatedDate
      <ignore> CreatedById
      <ignore> LastModifiedDate
      <ignore> LastModifiedById
      <ignore> SystemModstamp
      Family Family
    19. Choose the Start button to insert the records from the .csv file into Salesforce.

    Further information