I am trying to figure out how to execute dynamic SQL using SAPDB
precompiler. I followed the example in the Precompiler user manual
section 2.10.3 'Dynamic SQL statement with descriptor' (page 2-45).
The attached is my .cpc program. In line 36,
if I use 'exec sql execute dbt3_fetch', sqlca.sqlcode is -9405.
If I use 'exec sql execute dbt3_fetch using descriptor' sqlca.sqlcode is
100.
set_decribe_area() seemed to work correctly. My guess is something is
missing in the statement:
EXEC SQL prepare dbt3_fetch from 'fetch using descriptor'
Thanks in advance for your help,
--
Jenny Zhang
Open Source Development Lab Inc
15275 SW Koll Parkway - Suite H
Beaverton, OR 97006
(503)626-2455 ext 31
#include <stdio.h>
#include <string.h>
EXEC SQL include sqlca;
void set_describe_area();
void get_output();
/* declare the host variables which are binded to sqlda */
double hostreal[10];
int hostint[5];
char hostchar40[10][41];
char hostchar117[1][118];
main()
{
exec sql begin declare section;
char stmt[1024];
exec sql end declare section;
exec sql set serverdb 'DBT3' on 'localhost';
exec sql connect dbt identified by dbt;
if (sqlca.sqlcode !=0) printf("sql returned error: %d\n", sqlca.sqlcode);
strcpy(stmt, "select s_name from supplier where s_suppkey<10");
EXEC SQL prepare dbt3_query from :stmt;
EXEC SQL execute dbt3_query;
EXEC SQL prepare dbt3_fetch from 'fetch using descriptor';
EXEC SQL describe dbt3_fetch;
printf("state 1 sqlcode %d\n",sqlca.sqlcode );
/* provide the SQLDA with user information */
set_describe_area();
printf("state 2 sqlcode %d\n",sqlca.sqlcode );
/* process the result table */
EXEC SQL execute dbt3_fetch using descriptor;
printf("state 4 sqlcode %d\n",sqlca.sqlcode );
if (sqlca.sqlcode == 0)
{
printf("state 5\n");
get_output();
// EXEC SQL execute dbt3_query;
}
printf("state 6\n");
EXEC SQL close c_dbt3;
}
void set_describe_area()
{
sqlvartype *actvar;
int i, hostint_index, hostreal_index, hostchar40_index, hostchar117_index;
hostint_index=0;
hostreal_index=0;
hostchar40_index=0;
hostchar117_index=0;
printf("sqlda.sqln is %d\n", sqlda.sqln);
for (i=0; i<sqlda.sqln; i++)
{
printf("i is %d\n", i);
actvar = &sqlda.sqlvar[i];
printf("coltype %d\n", (*actvar).coltype);
switch ((*actvar).coltype)
{
case 0:
if ((*actvar).colfrac == 0)
{
(*actvar).hostvartype=1;
(*actvar).hostvaraddr =
&hostint[hostint_index];
hostint_index++;
}
else
{
(*actvar).collength=18;
(*actvar).colfrac=8;
(*actvar).hostvartype=3;
(*actvar).hostvaraddr =
&hostreal[hostreal_index];
hostreal_index++;
}
break;
case 2:
(*actvar).hostvartype = 6;
if ((*actvar).collength < 41)
{
(*actvar).collength = 40;
(*actvar).hostvaraddr =
hostchar40[hostchar40_index];
hostchar40_index++;
}
else
{
(*actvar).collength = 117;
(*actvar).hostvaraddr =
hostchar117[hostchar117_index];
hostchar117_index++;
}
break;
default:
printf("invalid type\n");
}
}
}
void get_output()
{
int i;
//for (i=0; i<10; i++)
printf("id %d: s_name %s\n", i, hostchar40[0]);
}