Having problems with your account or logging in?
A lot of changes are happening in the community right now. Some may affect you. READ MORE HERE

How do I bind a script variable to Dynamic SQL ?

How do I bind a script variable to Dynamic SQL ?

   You need to use ::VarName when binding a variable, or "::VarName" when it is a string data type.

Please see the sample code below:

//----------------------------------------------------------------------
// Recorded by SilkPerformer Recorder
//----------------------------------------------------------------------
benchmark SilkPerformerRecorder

use "Kernel.bdh"
use "Odbc.bdh"


var
   c1 : cursor;
   ghDbc1 : number;
   ghEnv1 : number;
   sTest : string;
   sFirstname : string;

dcluser
   user VUser
   transactions
      TMain : 1;

dcltrans
transaction TMain
var

begin
/* CONNECTION SPECIFIC FUNCTIONS */
   OdbcAlloc(SQL_HANDLE_ENV, ghEnv1);
   OdbcAlloc(SQL_HANDLE_DBC, ghDbc1, ghEnv1);
   OdbcConnect(ghDbc1,"DSN=My_DSN;UID=sa;UseProcForPrepare=0;PWD="";");
   OdbcOpen(c1, ghDbc1);
   OdbcSetConnectAttr(ghDbc1, SQL_ATTR_AUTOCOMMIT, SQL_AUTOCOMMIT_OFF);

/* ///////////////////////// */
/* BINDING & FETCHING FUNCTIONS */
   sTest := "J%";
// variable name to be used in Store Procedure
   OdbcExecDirect(c1, TMain_SQL001a);
// execute SQL
// Define placeholders
   OdbcDefine(c1, "1", SQL_C_CHAR, 12);
   OdbcDefine(c1, "2", SQL_C_CHAR, 11);
   OdbcDefine(c1, "3", SQL_C_CHAR, 11);
   OdbcDefine(c1, "4", SQL_C_CHAR, 61);
   OdbcDefine(c1, "5", SQL_C_CHAR, 41);
   OdbcDefine(c1, "6", SQL_C_CHAR, 81);
   OdbcDefine(c1, "7", SQL_C_CHAR, 11);
   OdbcFetch(c1, 1, 1, SQL_FETCH_NEXT);

// now fetch the result set

   print( "rows fetched = " + string( RsRows() ) );
// print out nRows

   print( "Person's Name = " + RsGetString("5") + " "+ RsGetString("4") );
// print out first and last name of person

/* ///////////////////////// */

/* CONNECTION CLOSE SPECIFIC FUNCTIONS */
   OdbcClose(c1, SQL_DROP);
   OdbcCommit(ghDbc1);
   OdbcDisconnect(ghDbc1);
   OdbcFree(ghDbc1);
/* ///////////////////////// */
end TMain;

dclsql
   TMain_SQL001a:
      SELECT p_no,p_opening,p_title,p_lastname,p_firstname,p_aline1,p_zip
      FROM my_person
      WHERE p_firstname like "::sTest"; // string variable being used

The same approach of using ::VarName and "::VarName" applies to stored procedures.

DISCLAIMER:

Some content on Community Tips & Information pages is not officially supported by Micro Focus. Please refer to our Terms of Use for more detail.
Top Contributors
Version history
Revision #:
1 of 1
Last update:
‎2013-02-15 18:54
Updated by:
 
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.