Salesforce SOQL from Oracle

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 Oracle by using our ODBC driver with Oracle's Database Gateway for ODBC (DG4ODBC).

DG4ODBC comes as part of Oracle 12c / Oracle 11g, at no additional cost, and supports both these versions of Oracle and OracleŽ 10g.

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 DG4ODBC, 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 DG4ODBC, 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 Oracle

Using the Salesforce ODBC driver to access Salesforce data from Oracle Heterogenous Services:

  1. Create a DG4ODBC init file. To do this, change to the %ORACLE_HOME%\hs\admin directory. Create a copy of the file initdg4odbc.ora. Name the new file initsoql.ora.

    Note In these instructions, replace %ORACLE_HOME% with the location of your Oracle HOME directory. For example, C:\oraclexe\app\oracle\product\11.2.0\server.

  2. Ensure these parameters and values are present in your init file:
    HS_FDS_CONNECT_INFO = my_salesforce_odbc_dsn;UID=my_salesforce_user_name;DOMAIN=my_salesforce_user_domain

    Replace my_salesforce_odbc_dsn with the name of a Salesforce.com ODBC driver data source that connects to the target Salesforce.com server. Replace my_salesforce_user_name with the user portion of your Salesforce.com user name. Replace my_salesforce_user_domain with the domain portion of your Salesforce.com user name. For example:

    HS_FDS_CONNECT_INFO = "my_salesforce_dsn;UID=myuser;DOMAIN=mydomain"
  3. Add an entry to %ORACLE_HOME%\network\admin\listener.ora that creates a SID_NAME for DG4ODBC. For example:
    SID_LIST_LISTENER =
     (SID_LIST =
       (SID_DESC=
         (SID_NAME=soql)
         (ORACLE_HOME=%ORACLE_HOME%)
         (PROGRAM=dg4odbc)
       )
     )
  4. Add a DG4ODBC entry to %ORACLE_HOME%\network\admin\tnsnames.ora that specifies the SID_NAME created in the previous step. For example:
    SOQL =
      (DESCRIPTION =
        (ADDRESS = (PROTOCOL = TCP)(HOST = oracle_host)(PORT = 1521))
        (CONNECT_DATA =
          (SID = soql)
        )
        (HS = OK)
      )

    Replace oracle_host with the host name of your Oracle machine.

  5. Start (or restart) the Oracle Listener:
    cd %ORACLE_HOME%\bin
    lsnrctl stop
    lsnrctl start
  6. Connect to your Oracle database in SQL*Plus.
  7. In SQL*Plus, create a database link for the target Salesforce.com server. For example:
    CREATE PUBLIC DATABASE LINK soqllink CONNECT TO
    "my_salesforce_user" IDENTIFIED by "my_salesforce_password" USING 'soql';

    Replace my_salesforce_user and my_salesforce_password with a valid username and password for the target Salesforce.com server.

To use SOQL from Oracle, you need to use DBMS_HS_PASSTHROUGH, which lets you send a pass-through statement to Salesforce. For example:

DECLARE 
   c BINARY_INTEGER; 
   nr BINARY_INTEGER; 
   val1 nvarchar2(50);
   val2 nvarchar2(50); 
BEGIN
   c := DBMS_HS_PASSTHROUGH.OPEN_CURSOR@soqllink; 
   DBMS_HS_PASSTHROUGH.PARSE@soqllink(c,'SELECT Account.Name, (SELECT Contact.LastName FROM Account.Contacts) FROM Account'); 
   BEGIN 
     nr:=0; 
     WHILE (true) 
        LOOP 
          nr:=DBMS_HS_PASSTHROUGH.FETCH_ROW@soqllink(c,false); 
            DBMS_HS_PASSTHROUGH.GET_VALUE@soqllink(c,1,val1);
            DBMS_HS_PASSTHROUGH.GET_VALUE@soqllink(c,2,val2);   
            DBMS_OUTPUT.PUT_LINE('Name '||val1);
            DBMS_OUTPUT.PUT_LINE('LastName '||val2);
        END LOOP; 
        EXCEPTION 
        WHEN no_data_found THEN 
           BEGIN 
              DBMS_OUTPUT.PUT_LINE('No more rows.'); 
              DBMS_HS_PASSTHROUGH.CLOSE_CURSOR@soqllink(c); 
           END; 
   END; 
END; 
/