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 11g and later, at no additional cost
To get started:
- Install and license the Salesforce ODBC driver on the machine where Oracle is installed.
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 DG4ODBC, 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 DG4ODBC, 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:
- In the ODBC Data Source Administrator, choose the System DSN tab, and then choose Add.
- In the Create New Data Source dialog box, choose Easysoft Salesforce SOQL ODBC driver, and then choose Finish.
- 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.
- Use the Test button to verify that you can successfully connect to Salesforce.
Using Salesforce SOQL from Oracle
-
Create a DG4ODBC init file. To do this, change to the
%ORACLE_HOME%\hs\admin
directory. Create a copy of the fileinitdg4odbc.ora
. Name the new fileinitsoql.ora
.Note In these instructions, replace
%ORACLE_HOME%
with the location of your OracleHOME
directory. For example,C:\oraclexe\app\oracle\product\11.2.0\server
. -
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 ODBC driver data source that connects to the target Salesforce server. Replacemy_salesforce_user_name
with the user portion of your Salesforce user name. Replacemy_salesforce_user_domain
with the domain portion of your Salesforce user name. For example:HS_FDS_CONNECT_INFO = "my_salesforce_dsn;UID=myuser;DOMAIN=mydomain"
-
Add an entry to
%ORACLE_HOME%\network\admin\listener.ora
that creates aSID_NAME
for DG4ODBC. For example:SID_LIST_LISTENER = (SID_LIST = (SID_DESC= (SID_NAME=soql) (ORACLE_HOME=%ORACLE_HOME%) (PROGRAM=dg4odbc) ) )
-
Add a DG4ODBC entry to
%ORACLE_HOME%\network\admin\tnsnames.ora
that specifies theSID_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. -
Start (or restart) the Oracle Listener:
cd %ORACLE_HOME%\bin lsnrctl stop lsnrctl start
- Connect to your Oracle database in SQL*Plus.
-
In SQL*Plus, create a database link for the target Salesforce server. For example:
CREATE PUBLIC DATABASE LINK soqllink CONNECT TO "my_salesforce_user" IDENTIFIED by "my_salesforce_password" USING 'soql';
Replace
my_salesforce_user
andmy_salesforce_password
with a valid user name and password for the target Salesforce 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; /