10-12-2012 10:05 AM - edited 10-12-2012 10:33 AM
Hi,
I was wondering if it's possible to execute a custom sql query (built in mysql)?
example : "SELECT assy_021_check(0801979291) AS TEST"
assy_021_check is a function code into mysql database.
With a regulat query, verything works, but if I use this query, I get a error code saying that the ODBC drive does not supporte the resquested properties.
Also, does a direct connection is supported my the sqlToolkit (without using ODBC driver)?
Thanks for your help.
10-15-2012 03:30 PM
It appears that you're trying to execute a stored procedure. You can't just call the stored procedure like you're doing, you'll have to use the SQL Toolkit's stored procedure functions. The SQL toolkit doesn't come with any example programs showing how to use stored procedures but the various stored procedure functions themselves do have code snippets however. This one is from DBSetParamInt:
/* This example works with Microsoft SQL Server. */
/* Create a stored procedure with input, output, */
/* and return value parameters. */
resCode = DBImmediateSQL (hdbc, "create proc sp_AdoTest(@InParam int, @OutParam int OUTPUT ) \
AS SELECT * FROM meas WHERE val1 >= @InParam \
SET @OutParam = @InParam * 2 \
RETURN @InParam + 10");
/* Prepare a statement that calls the stored procedure. */
resCode = DBSetAttributeDefault (hdbc, ATTR_DB_COMMAND_TYPE, DB_COMMAND_STORED_PROC);
hstmt = DBPrepareSQL (hdbc, "sp_AdoTest");
resCode = DBSetAttributeDefault (hdbc, ATTR_DB_COMMAND_TYPE, DB_COMMAND_UNKNOWN);
/* Refresh the parameters from the stored procedure. */
resCode = DBRefreshParams (hstmt);
/* Set the input parameter. */
resCode = DBSetParamInt (hstmt, 2, 10);
/* Execute the statement. */
resCode = DBExecutePreparedSQL (hstmt);
while ((resCode = DBFetchNext (hstmt)) == DB_SUCCESS) {
/* Process records returned by the stored procedure. */
}
/* Close the statement. Output parameters are invalid */
/* until you close the statement. */
resCode = DBClosePreparedSQL (hstmt);
/* Examine the parameter values. */
resCode = DBGetParamInt (hstmt, 1, &retParam);
resCode = DBGetParamInt (hstmt, 2, &inParam);
resCode = DBGetParamInt (hstmt, 3, &outParam);
/* Discard the statement. */
hstmt = DBDiscardSQLStatement (hstmt);
Now obviously you have your stored procedure already created so you can skip the first DBImmediateSQL call which creates the stored procedure, but you'll still have to set the command types, "prepare" the SQL and refresh the params. You'd want to use either DBSetParamInt or DBSetParamChar (I can't tell exactly what datatype your stored procedure is using). Once you have your parameters set up, just call DBExecutePreparedSQL and everything should be good. You'd just use DBGetParam____ to examine the output values.
One thing to note, the parameter Index for these functions is 1-based, but I'm pretty sure that the 1-index is always going to be the return value, even if your stored procedure doesn't return anything, so when setting your parameter values they should start at index 2 and continue from there. I'm going off of memory on that, so if it's wrong I apologize.