Salesforce SOQL from SQL Server

The Salesforce ODBC driver extends the number of applications that you can use Salesforce SOQL from. In this blog, we describe how to run SOQL statements from SQL Server.

To get started:

Before you can use the Salesforce ODBC driver to connect your application 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).

If you have the 64-bit version of SQL Server, you need to run 64-bit version of ODBC Data Source Administrator (which you use to create a data source). To do this, open Administrative Tools in Control Panel, and then open Data Sources ODBC. On Windows Server 2003 and earlier, the Control Panel applet that launches ODBC Data Source Administrator is labelled Data Sources. On Windows 8 and later, the Control Panel applet is labelled ODBC Data Sources (64-bit).

If you have the 32-bit version of SQL Server, you need to run 32-bit version of ODBC Data Source Administrator. To do this, in the Windows Run dialog box, enter:

%windir%\syswow64\odbcad32.exe

To create a Salesforce ODBC driver data source:

  1. In the ODBC Data Source Administrator, choose the System DSN tab, and then choose Add.
  2. In the Create New Data Source dialog box, choose Easysoft Salesforce SOQL ODBC 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.

Using Salesforce SOQL from SQL Server

Using the Salesforce ODBC driver to access Salesforce data from SQL 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"...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.

  6. You can query your Salesforce data by using a pass-through SQL Query. For example:
    SELECT * FROM OPENQUERY(SALESFORCE,'SELECT Account.Name, (SELECT Contact.LastName FROM Account.Contacts) FROM Account')
    EXEC ('SELECT Account.Name, (SELECT Contact.LastName FROM Account.Contacts) FROM Account' ) at SALESFORCE
    SELECT * FROM OPENROWSET('MSDASQL', 'DSN=MY_SALESFORCE_ODBC_DATA_SOURCE;', 'SELECT Account.Name, (SELECT Contact.LastName FROM Account.Contacts) FROM Account')