在Oracle Pro*C嵌入式SQL (Ref: https://docs.oracle.com/cd/B10501_01/appdev.920/a97269/pc_06sql.htm),我想FETCH INTO三个主机变量:例如
EXEC SQL FETCH emp_cursor
INTO :emp_name, :emp_number, :salary;
在另一个变体中,我想使用两个主机变量,如下所示:
EXEC SQL FETCH emp_cursor
INTO :emp_name, :emp_number;
是否有任何机制,我可以有一个字符串声明和操纵有动态内容的FETCH与三个变量或FETCH与两个变量的第一个例子,如在第二个。有什么机制能产生这种动力吗?此外,如果我想获取200行最大,这些主机变量应该如何声明?
使用descriptor
。见此链接https://docs.oracle.com/cd/B10501_01/appdev.920/a97269/pc_14ady.htm
cursor
:
SELECT ename, empno FROM emp ;
您需要在descriptor
中创建两个values
(name, empno)。这些values
需要3个属性: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
...
}