使用descriptor
. 看到这个链接https://docs.oracle.com/cd/B10501_01/appdev.920/a97269/pc_14ady.htm
例如,您需要cursor
为该字符串使用 a:
SELECT ename, empno FROM emp ;
您需要在 .Those 中创建两个( values
ename, empno)需要 3 个属性:和(请参阅链接中的表格)descriptor
values
type
len
data
//attributes for ename
int ename_type = 97, ename_len = 30 ;
char ename_data[31] ;
//attributes for empno
int empno_type = 3, empno_len = 4 ;
int empno_data ;
完整的代码在这里:
char* dyn_statement = "SELECT ename, empno FROM emp"
;
int ename_type = 97, ename_len = 30 ;
char ename_data[31] ;
int empno_type = 3, empno_len = 4 ;
int empno_data ;
long SQLCODE = 0 ;
...
main ()
{
/* Place preliminary code, including connection, here. */
...
EXEC SQL ALLOCATE DESCRIPTOR 'out'; //start the descriptor
EXEC SQL PREPARE s FROM :dyn_statement ;
EXEC SQL DECLARE c CURSOR FOR s ;
EXEC SQL DESCRIBE OUTPUT s USING DESCRIPTOR 'out' ;
//pass ename's attributes for DESCRIPTOR
EXEC SQL SET DESCRIPTOR 'out' VALUE 1 TYPE = :ename_type,
LENGTH = :ename_len, DATA = :ename_data ;
//pass empno's attributes for DESCRIPTOR
EXEC SQL SET DESCRIPTOR 'out' VALUE 2 TYPE = :empno_type,
LENGTH = :empno_len, DATA = :empno_data ;
EXEC SQL WHENEVER NOT FOUND DO BREAK ;
while (SQLCODE == 0)
{
EXEC SQL FETCH c INTO DESCRIPTOR 'out' ;
EXEC SQL GET DESCRIPTOR 'out' VALUE 1 :ename_data = DATA ;
EXEC SQL GET DESCRIPTOR 'out' VALUE 2 :empno_data = DATA ;
printf("\nEname = %s Empno = %s", ename_data, empno_data) ;
}
EXEC SQL CLOSE c ;
EXEC SQL DEALLOCATE DESCRIPTOR 'out' ;//close descriptor
...
}