云数据库 GAUSSDB-游标循环:示例

时间:2024-11-13 14:46:09

示例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
gaussdb=# BEGIN
FOR ROW_TRANS IN 
        SELECT first_name FROM hr.staffs 
    LOOP 
        DBE_OUTPUT.PRINT_LINE (ROW_TRANS.first_name );
    END LOOP;
END;
/
--执行结果
gaussdb=# Tom
ANONYMOUS BLOCK EXECUTE

--创建表
gaussdb=# CREATE TABLE integerTable1( A INTEGER) DISTRIBUTE BY hash(A);
gaussdb=# CREATE TABLE integerTable2( B INTEGER) DISTRIBUTE BY hash(B);
gaussdb=# INSERT INTO integerTable2 VALUES(2);

--多游标共享游标属性的标量
gaussdb=# DECLARE
   CURSOR C1 IS SELECT A FROM integerTable1;--声明游标
   CURSOR C2 IS SELECT B FROM integerTable2;
   PI_A INTEGER;
   PI_B INTEGER;
BEGIN
   OPEN C1;--打开游标
   OPEN C2;
   FETCH C1 INTO PI_A; ----  C1%FOUND 和 C2%FOUND 值为 FALSE
   FETCH C2 INTO PI_B; ----  C1%FOUND 和 C2%FOUND 的值都为 TRUE
   --判断游标状态
   IF C1%FOUND THEN
       IF C2%FOUND THEN
         DBE_OUTPUT.PRINT_LINE('Dual cursor share parameter.');
      END IF;
   END IF;
   CLOSE C1;--关闭游标
   CLOSE C2;
END;
/
ANONYMOUS BLOCK EXECUTE
--删除临时表
gaussdb=# DROP TABLE integerTable1;
gaussdb=# DROP TABLE integerTable2;
support.huaweicloud.com/distributed-devg-v8-gaussdb/gaussdb-12-0799.html