Subject | Using the API to execute a stored proceedure with out params |
---|---|
Author | edcurren |
Post date | 2005-07-05T15:10:28Z |
Hello all,
I am writing code to call a SP w/ 7 in parameters and 1 out
parameter. The code I'm calling is below. On the isc_dsql_execute
function I am getting the following error message:
Unsuccessful execution caused by system error that does not preclude
successful execution of subsequent statements
message length error (encountered 0, expected 8)
I thought perhaps it was a dialect issue, but I get the same results
when using " or '.
Thanks to all for your kind help. It is very much appreciated.
The code that calls the function is:
sprintf(exe_proc, "EXECUTE PROCEDURE ADDCUSTOMER \"%s\", \"%s\", \"%
s\", \"%s\", \"%s\", \"%s\", \"%s\"", HonoraryTitleToString
(title).c_str(), (char *)_bstr_t(firstName), (char *)_bstr_t
(middleName), (char *)_bstr_t(lastName), GenerationToString
(generation).c_str(), (char *)_bstr_t(departmentName), (char *)_bstr_t
(companyName));
string retVal = ExecuteQueryAsXml(exe_proc, true);
The function code is:
string CDatabase::ExecuteQueryAsXml(const char *sqlToExecute, bool
trimSpaces)
{
short flag0 = 0, flag1 = 0;
short flag2 = 0;
short dtype;
isc_stmt_handle stmt = NULL; /* statement
handle */
isc_db_handle DB = NULL; /* database
handle */
isc_tr_handle trans = NULL; /*
transaction handle */
ISC_STATUS_ARRAY status; /* status
vector */
ISC_STATUS *pStatus = status;
XSQLDA * sqlda;
XSQLVAR* var;
long fetch_stat;
char nwldb[128];
string returnData;
CkXml rootNode;
CkString xmlString;
bool firstFetch = true;
const char *username = "SYSDBA";
const char *password = "masterkey";
char dpb_buffer[256], *dpb;
short dpb_length;
sprintf(nwldb, "..\\..\\Database\\db.fdb");
dpb = dpb_buffer;
*dpb++ = isc_dpb_version1;
*dpb++ = isc_dpb_num_buffers;
*dpb++ = 1;
*dpb++ = 90;
// Set the db user name
*dpb++ = isc_dpb_user_name;
*dpb++ = strlen(username);
strcpy(dpb, username);
dpb += strlen(username);
// set the db password
*dpb++ = isc_dpb_password;
*dpb++ = strlen(password);
strcpy(dpb, password);
dpb += strlen(password);
dpb_length = dpb - dpb_buffer;
if (isc_attach_database(status, 0, nwldb, &DB, dpb_length,
dpb_buffer))
{
if (status[0] == 1 && status[1])
{
char outmsg[1024];
char msg[512];
isc_sql_interprete(isc_sqlcode(status), msg,
512);
strcpy(outmsg, msg);
while( isc_interprete(msg, &pStatus) )
{
strcat(outmsg, msg);
strcat(outmsg, "\n");
}
}
}
if (isc_start_transaction(status, &trans, 1, &DB, 0, NULL))
{
if (status[0] == 1 && status[1])
{
char outmsg[1024];
char msg[512];
isc_sql_interprete(isc_sqlcode(status), msg,
512);
strcpy(outmsg, msg);
while( isc_interprete(msg, &pStatus) )
{
strcat(outmsg, msg);
strcat(outmsg, "\n");
}
}
}
/* Allocate an output SQLDA. */
sqlda = (XSQLDA *) malloc(XSQLDA_LENGTH(3));
sqlda->sqln = 3;
sqlda->version = SQLDA_VERSION1;
/* Allocate a statement. */
if (isc_dsql_allocate_statement(status, &DB, &stmt))
{
if (status[0] == 1 && status[1])
{
char outmsg[1024];
char msg[512];
isc_sql_interprete(isc_sqlcode(status), msg,
512);
strcpy(outmsg, msg);
while( isc_interprete(msg, &pStatus) )
{
strcat(outmsg, msg);
strcat(outmsg, "\n");
}
}
}
/* Prepare the statement. */
if (isc_dsql_prepare(status, &trans, &stmt, 0, (char*)
sqlToExecute, 1, sqlda))
{
if (status[0] == 1 && status[1])
{
char outmsg[1024];
char msg[512];
isc_sql_interprete(isc_sqlcode(status), msg,
512);
strcpy(outmsg, msg);
while( isc_interprete(msg, &pStatus) )
{
strcat(outmsg, msg);
strcat(outmsg, "\n");
}
}
}
isc_dsql_describe(status, &stmt, 1, sqlda);
if (sqlda->sqld > sqlda->sqln)
{
int parameterCount = sqlda->sqld;
free(sqlda);
sqlda = (XSQLDA *)malloc(XSQLDA_LENGTH
(parameterCount));
sqlda->sqln = parameterCount;
sqlda->version = SQLDA_VERSION1;
isc_dsql_describe(status, &stmt, 1, sqlda);
}
int i;
for (i=0, var = sqlda->sqlvar; i < sqlda->sqld; i++, var++)
{
dtype = (var->sqltype & ~1); /* drop flag bit for now
*/
switch(dtype)
{
case SQL_DATE:
case SQL_BLOB:
case SQL_ARRAY:
var->sqldata = (char *)malloc(sizeof
(ISC_QUAD)*var->sqllen);
break;
case SQL_VARYING:
var->sqltype = SQL_TEXT;
var->sqldata = (char *)malloc(sizeof
(char)*var->sqllen + 2);
break;
case SQL_TEXT:
var->sqldata = (char *)malloc(sizeof
(char)*var->sqllen);
break;
case SQL_LONG:
var->sqldata = (char *)malloc(sizeof
(long));
break;
case SQL_SHORT:
var->sqldata = (char *)malloc(sizeof
(short));
break;
case SQL_DOUBLE:
var->sqldata = (char *)malloc(sizeof
(double));
break;
case SQL_FLOAT:
var->sqldata = (char *)malloc(sizeof
(float));
break;
} /* end of switch statements */
if (var->sqltype & 1)
{
/* allocate variable to hold NULL status */
var->sqlind = (short *)malloc(sizeof(short));
}
} /* end of for loop */
/* Execute the statement. */
if (isc_dsql_execute(status, &trans, &stmt, 1, NULL))
{
if (status[0] == 1 && status[1])
{
char outmsg[1024];
char msg[512];
isc_sql_interprete(isc_sqlcode(status), msg,
512);
strcpy(outmsg, msg);
strcat(outmsg, "\n");
while( isc_interprete(msg, &pStatus) )
{
strcat(outmsg, msg);
strcat(outmsg, "\n");
}
FILE *f = fopen
("C:\\Temp\\DumpErr.txt", "w+r");
fwrite(outmsg, 1, strlen(outmsg), f);
fclose(f);
}
}
while ((fetch_stat = isc_dsql_fetch(status, &stmt, 1, sqlda)) ==
0)
{
if( firstFetch )
{
rootNode.put_Tag(sqlda->sqlvar[0].relname);
firstFetch = false;
}
//for (i = 0; i < 10; i++)
for(i=0, var = sqlda->sqlvar; i < sqlda->sqld; i++,
var++)
{
CkXml *xmlData = new CkXml();
xmlData->put_Tag(sqlda->sqlvar[i].sqlname);
char *data = new char[sqlda->sqlvar
[i].sqllen];
dtype = (var->sqltype & ~1);
long l;
switch(dtype)
{
case SQL_VARYING:
case SQL_TEXT:
data = (char *)sqlda->sqlvar
[i].sqldata;
data[sqlda->sqlvar[i].sqllen]
= '\0';
if( trimSpaces )
{
string trimedData =
TrimRight(data);
xmlData->put_Content
(trimedData.c_str());
}
else
{
xmlData->put_Content
(data);
}
break;
case SQL_SHORT:
case SQL_LONG:
l = (ISC_INT64) *(long *)
sqlda->sqlvar[i].sqldata;
data = ltoa(l, data, 10);
xmlData->put_Content(data);
break;
case SQL_FLOAT:
case SQL_DOUBLE:
break;
}
rootNode.AddChildTree(xmlData);
delete xmlData;
}
}
rootNode.GetXml(xmlString);
returnData = xmlString.getString();
if (fetch_stat != 100L)
{
if (status[0] == 1 && status[1])
{
char outmsg[1024];
char msg[512];
isc_sql_interprete(isc_sqlcode(status), msg,
512);
strcpy(outmsg, msg);
while( isc_interprete(msg, &pStatus) )
{
strcat(outmsg, msg);
strcat(outmsg, "\n");
}
}
}
/* Free statement handle. */
if (isc_dsql_free_statement(status, &stmt, DSQL_close))
{
if (status[0] == 1 && status[1])
{
char outmsg[1024];
char msg[512];
isc_sql_interprete(isc_sqlcode(status), msg,
512);
strcpy(outmsg, msg);
while( isc_interprete(msg, &pStatus) )
{
strcat(outmsg, msg);
strcat(outmsg, "\n");
}
}
}
if (isc_commit_transaction(status, &trans))
{
if (status[0] == 1 && status[1])
{
char outmsg[1024];
char msg[512];
isc_sql_interprete(isc_sqlcode(status), msg,
512);
strcpy(outmsg, msg);
while( isc_interprete(msg, &pStatus) )
{
strcat(outmsg, msg);
strcat(outmsg, "\n");
}
}
}
if (isc_detach_database(status, &DB))
{
if (status[0] == 1 && status[1])
{
char outmsg[1024];
char msg[512];
isc_sql_interprete(isc_sqlcode(status), msg,
512);
strcpy(outmsg, msg);
while( isc_interprete(msg, &pStatus) )
{
strcat(outmsg, msg);
strcat(outmsg, "\n");
}
}
}
free( sqlda );
return returnData;
}
I am writing code to call a SP w/ 7 in parameters and 1 out
parameter. The code I'm calling is below. On the isc_dsql_execute
function I am getting the following error message:
Unsuccessful execution caused by system error that does not preclude
successful execution of subsequent statements
message length error (encountered 0, expected 8)
I thought perhaps it was a dialect issue, but I get the same results
when using " or '.
Thanks to all for your kind help. It is very much appreciated.
The code that calls the function is:
sprintf(exe_proc, "EXECUTE PROCEDURE ADDCUSTOMER \"%s\", \"%s\", \"%
s\", \"%s\", \"%s\", \"%s\", \"%s\"", HonoraryTitleToString
(title).c_str(), (char *)_bstr_t(firstName), (char *)_bstr_t
(middleName), (char *)_bstr_t(lastName), GenerationToString
(generation).c_str(), (char *)_bstr_t(departmentName), (char *)_bstr_t
(companyName));
string retVal = ExecuteQueryAsXml(exe_proc, true);
The function code is:
string CDatabase::ExecuteQueryAsXml(const char *sqlToExecute, bool
trimSpaces)
{
short flag0 = 0, flag1 = 0;
short flag2 = 0;
short dtype;
isc_stmt_handle stmt = NULL; /* statement
handle */
isc_db_handle DB = NULL; /* database
handle */
isc_tr_handle trans = NULL; /*
transaction handle */
ISC_STATUS_ARRAY status; /* status
vector */
ISC_STATUS *pStatus = status;
XSQLDA * sqlda;
XSQLVAR* var;
long fetch_stat;
char nwldb[128];
string returnData;
CkXml rootNode;
CkString xmlString;
bool firstFetch = true;
const char *username = "SYSDBA";
const char *password = "masterkey";
char dpb_buffer[256], *dpb;
short dpb_length;
sprintf(nwldb, "..\\..\\Database\\db.fdb");
dpb = dpb_buffer;
*dpb++ = isc_dpb_version1;
*dpb++ = isc_dpb_num_buffers;
*dpb++ = 1;
*dpb++ = 90;
// Set the db user name
*dpb++ = isc_dpb_user_name;
*dpb++ = strlen(username);
strcpy(dpb, username);
dpb += strlen(username);
// set the db password
*dpb++ = isc_dpb_password;
*dpb++ = strlen(password);
strcpy(dpb, password);
dpb += strlen(password);
dpb_length = dpb - dpb_buffer;
if (isc_attach_database(status, 0, nwldb, &DB, dpb_length,
dpb_buffer))
{
if (status[0] == 1 && status[1])
{
char outmsg[1024];
char msg[512];
isc_sql_interprete(isc_sqlcode(status), msg,
512);
strcpy(outmsg, msg);
while( isc_interprete(msg, &pStatus) )
{
strcat(outmsg, msg);
strcat(outmsg, "\n");
}
}
}
if (isc_start_transaction(status, &trans, 1, &DB, 0, NULL))
{
if (status[0] == 1 && status[1])
{
char outmsg[1024];
char msg[512];
isc_sql_interprete(isc_sqlcode(status), msg,
512);
strcpy(outmsg, msg);
while( isc_interprete(msg, &pStatus) )
{
strcat(outmsg, msg);
strcat(outmsg, "\n");
}
}
}
/* Allocate an output SQLDA. */
sqlda = (XSQLDA *) malloc(XSQLDA_LENGTH(3));
sqlda->sqln = 3;
sqlda->version = SQLDA_VERSION1;
/* Allocate a statement. */
if (isc_dsql_allocate_statement(status, &DB, &stmt))
{
if (status[0] == 1 && status[1])
{
char outmsg[1024];
char msg[512];
isc_sql_interprete(isc_sqlcode(status), msg,
512);
strcpy(outmsg, msg);
while( isc_interprete(msg, &pStatus) )
{
strcat(outmsg, msg);
strcat(outmsg, "\n");
}
}
}
/* Prepare the statement. */
if (isc_dsql_prepare(status, &trans, &stmt, 0, (char*)
sqlToExecute, 1, sqlda))
{
if (status[0] == 1 && status[1])
{
char outmsg[1024];
char msg[512];
isc_sql_interprete(isc_sqlcode(status), msg,
512);
strcpy(outmsg, msg);
while( isc_interprete(msg, &pStatus) )
{
strcat(outmsg, msg);
strcat(outmsg, "\n");
}
}
}
isc_dsql_describe(status, &stmt, 1, sqlda);
if (sqlda->sqld > sqlda->sqln)
{
int parameterCount = sqlda->sqld;
free(sqlda);
sqlda = (XSQLDA *)malloc(XSQLDA_LENGTH
(parameterCount));
sqlda->sqln = parameterCount;
sqlda->version = SQLDA_VERSION1;
isc_dsql_describe(status, &stmt, 1, sqlda);
}
int i;
for (i=0, var = sqlda->sqlvar; i < sqlda->sqld; i++, var++)
{
dtype = (var->sqltype & ~1); /* drop flag bit for now
*/
switch(dtype)
{
case SQL_DATE:
case SQL_BLOB:
case SQL_ARRAY:
var->sqldata = (char *)malloc(sizeof
(ISC_QUAD)*var->sqllen);
break;
case SQL_VARYING:
var->sqltype = SQL_TEXT;
var->sqldata = (char *)malloc(sizeof
(char)*var->sqllen + 2);
break;
case SQL_TEXT:
var->sqldata = (char *)malloc(sizeof
(char)*var->sqllen);
break;
case SQL_LONG:
var->sqldata = (char *)malloc(sizeof
(long));
break;
case SQL_SHORT:
var->sqldata = (char *)malloc(sizeof
(short));
break;
case SQL_DOUBLE:
var->sqldata = (char *)malloc(sizeof
(double));
break;
case SQL_FLOAT:
var->sqldata = (char *)malloc(sizeof
(float));
break;
} /* end of switch statements */
if (var->sqltype & 1)
{
/* allocate variable to hold NULL status */
var->sqlind = (short *)malloc(sizeof(short));
}
} /* end of for loop */
/* Execute the statement. */
if (isc_dsql_execute(status, &trans, &stmt, 1, NULL))
{
if (status[0] == 1 && status[1])
{
char outmsg[1024];
char msg[512];
isc_sql_interprete(isc_sqlcode(status), msg,
512);
strcpy(outmsg, msg);
strcat(outmsg, "\n");
while( isc_interprete(msg, &pStatus) )
{
strcat(outmsg, msg);
strcat(outmsg, "\n");
}
FILE *f = fopen
("C:\\Temp\\DumpErr.txt", "w+r");
fwrite(outmsg, 1, strlen(outmsg), f);
fclose(f);
}
}
while ((fetch_stat = isc_dsql_fetch(status, &stmt, 1, sqlda)) ==
0)
{
if( firstFetch )
{
rootNode.put_Tag(sqlda->sqlvar[0].relname);
firstFetch = false;
}
//for (i = 0; i < 10; i++)
for(i=0, var = sqlda->sqlvar; i < sqlda->sqld; i++,
var++)
{
CkXml *xmlData = new CkXml();
xmlData->put_Tag(sqlda->sqlvar[i].sqlname);
char *data = new char[sqlda->sqlvar
[i].sqllen];
dtype = (var->sqltype & ~1);
long l;
switch(dtype)
{
case SQL_VARYING:
case SQL_TEXT:
data = (char *)sqlda->sqlvar
[i].sqldata;
data[sqlda->sqlvar[i].sqllen]
= '\0';
if( trimSpaces )
{
string trimedData =
TrimRight(data);
xmlData->put_Content
(trimedData.c_str());
}
else
{
xmlData->put_Content
(data);
}
break;
case SQL_SHORT:
case SQL_LONG:
l = (ISC_INT64) *(long *)
sqlda->sqlvar[i].sqldata;
data = ltoa(l, data, 10);
xmlData->put_Content(data);
break;
case SQL_FLOAT:
case SQL_DOUBLE:
break;
}
rootNode.AddChildTree(xmlData);
delete xmlData;
}
}
rootNode.GetXml(xmlString);
returnData = xmlString.getString();
if (fetch_stat != 100L)
{
if (status[0] == 1 && status[1])
{
char outmsg[1024];
char msg[512];
isc_sql_interprete(isc_sqlcode(status), msg,
512);
strcpy(outmsg, msg);
while( isc_interprete(msg, &pStatus) )
{
strcat(outmsg, msg);
strcat(outmsg, "\n");
}
}
}
/* Free statement handle. */
if (isc_dsql_free_statement(status, &stmt, DSQL_close))
{
if (status[0] == 1 && status[1])
{
char outmsg[1024];
char msg[512];
isc_sql_interprete(isc_sqlcode(status), msg,
512);
strcpy(outmsg, msg);
while( isc_interprete(msg, &pStatus) )
{
strcat(outmsg, msg);
strcat(outmsg, "\n");
}
}
}
if (isc_commit_transaction(status, &trans))
{
if (status[0] == 1 && status[1])
{
char outmsg[1024];
char msg[512];
isc_sql_interprete(isc_sqlcode(status), msg,
512);
strcpy(outmsg, msg);
while( isc_interprete(msg, &pStatus) )
{
strcat(outmsg, msg);
strcat(outmsg, "\n");
}
}
}
if (isc_detach_database(status, &DB))
{
if (status[0] == 1 && status[1])
{
char outmsg[1024];
char msg[512];
isc_sql_interprete(isc_sqlcode(status), msg,
512);
strcpy(outmsg, msg);
while( isc_interprete(msg, &pStatus) )
{
strcat(outmsg, msg);
strcat(outmsg, "\n");
}
}
}
free( sqlda );
return returnData;
}