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

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

示例

 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
43
44
45
46
47
48
49
50
51
-- 如果需要游标循环并发执行,需要打开以下GUC参数
gaussdb=# SET query_dop=4;
SET
gaussdb=# SET sql_beta_feature='enable_plsql_smp';
SET
gaussdb=# BEGIN
FOR ROW_TRANS IN 
        SELECT first_name FROM hr.staffs 
    LOOP 
        DBE_OUTPUT.PRINT_LINE (ROW_TRANS.first_name );
    END LOOP;
END;
/
Tom
ANONYMOUS BLOCK EXECUTE

--创建表
gaussdb=# CREATE TABLE integerTable1( A INTEGER);
CREATE TABLE
gaussdb=# CREATE TABLE integerTable2( B INTEGER);
CREATE TABLE
gaussdb=# INSERT INTO integerTable2 VALUES(2);
INSERT 0 1
--多游标共享游标属性的标量
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;
DROP TABLE
gaussdb=# DROP TABLE integerTable2;
DROP TABLE
support.huaweicloud.com/centralized-devg-v8-gaussdb/gaussdb-42-0801.html