Easysoft Blog

Wed, 25 July 2018

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.com ODBC Driver to connect your application to Salesforce.com, you need to configure an ODBC data source. An ODBC data source stores the connection details for the target database (e.g. Salesforce.com) and the ODBC driver that is required to connect to it (e.g. the Salesforce.com ODBC driver).

If you have the 64-bit version of SQL Server, you need to run 64-bit version of ODBC 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 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 Administrator. To do this, in the Windows Run dialog box, type:

%windir%\syswow64\odbcad32.exe

To create a Salesforce.com ODBC Driver data source:

  1. In the ODBC Administrator, choose the System DSN tab, and then choose Add.

    To run the 32-bit version of ODBC Administrator, in the Windows Run dialog box, enter:

    %windir%\syswow64\odbcad32.exe
  2. In the Create New Data Source dialog box, choose Easysoft Salesforce SOQL ODBC Driver, and then choose Finish.
  3. Complete the Easysoft Salesforce SOQL ODBC Driver DSN Setup dialog box:
    Setting Value
    DSN Salesforce.com
    User Name The name of your Salesforce.com user. For example, myuser@mydomain.com.
    Password The password for your Salesforce.com user.
    Token The security token for your Salesforce.com 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.com emails the security token to the email address associated with your Salesforce.com user account. If you have not received a security token, you can regenerate it. Salesforce.com will then email the new security token to you. To regenerate your security token, log in to Salesforce.com and then choose Setup from the user menu. Search for "security token" in the Quick Find box. Click 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.com.

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, type "Salesforce".

    (If you want to call the Linked server "Salesforce.com", 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, type the name of your Salesforce.com ODBC data source, and then choose OK.

    SQL Server verifies the linked server by testing the connection.

  6. You can query your Salesforce.com 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')