DECLARE cursor_name [ BINARY ] [ INSENSITIVE ] [ [ NO ] SCROLL ] CURSOR [ { WITH | WITHOUT } HOLD ] FOR prepared_name DECLARE cursor_name [ BINARY ] [ INSENSITIVE ] [ [ NO ] SCROLL ] CURSOR [ { WITH | WITHOUT } HOLD ] FOR query
DECLARE为了迭代预备语句结果集声明了游标。 该命令与直接SQL命令DECLARE略微有些不同语法: 后者执行查询并且为检索准备结果集,这个嵌入SQL命令只为迭代查询结果集 声明作为"循环变量"的名字; 当使用OPEN命令打开游标时,发生实际执行情况。
为查询声明游标的例子:
EXEC SQL DECLARE C CURSOR FOR SELECT * FROM My_Table; EXEC SQL DECLARE C CURSOR FOR SELECT Item1 FROM T; EXEC SQL DECLARE cur1 CURSOR FOR SELECT version();
为预备语句声明游标的例子:
EXEC SQL PREPARE stmt1 AS SELECT version(); EXEC SQL DECLARE cur1 CURSOR FOR stmt1;