zoukankan      html  css  js  c++  java
  • PostgreSQL 游标的种类

    1.说明:

    1)cursor和refcursor的区别:
    静态游标,不能修改里面的语句
    动态游标,可以多次open和close,指定不同的SQL
    隐式游标,DML和for操作,都在内部转换为了一个隐式游标在执行

    2)fetch的区别:
    使用fetch cur 和 fetch next from cur 一样,因为NEXT 是 FETCH 的默认选项.


    2.创建测试表:

    create table test(id int, info text, crt_time timestamp);
    insert into test select generate_series(1,10), 'test', now();
    

      

    3.测试:

    DO
    $$
    DECLARE
    id integer;
    one_row record;
    ret_cur CURSOR FOR SELECT * FROM test; --静态游标,不能修改里面的语句
    ret_cur2 refcursor;--动态游标,可以多次open和close,指定不同的SQL
    BEGIN
    id:=1;
    raise notice 'test id: %', id;
    
    --动态游标
    OPEN ret_cur2 FOR SELECT * FROM test;
    FETCH ret_cur2 INTO one_row;
    while ret_cur2%FOUND loop
    raise notice 'row info, id is: %, info is: %, crt_time is: %', one_row.id, one_row.info, one_row.crt_time;
    FETCH ret_cur2 INTO one_row;
    END loop;
    CLOSE ret_cur2;
    
    raise notice 'user fetch next: ';
    
    --静态游标:
    OPEN ret_cur;
    FETCH NEXT FROM ret_cur INTO one_row;
    while ret_cur%FOUND loop
    raise notice 'row info, id is: %, info is: %, crt_time is: %', one_row.id, one_row.info, one_row.crt_time;
    FETCH NEXT FROM ret_cur INTO one_row;
    END loop;
    CLOSE ret_cur;
    
    
    --隐式游标,实际上select * from test是存在一个内部的游标中的:
    raise notice '隐式游标:';
    FOR one_row IN (SELECT * FROM test) loop
    raise notice 'row info, id is: %, info is: %, crt_time is: %', one_row.id, one_row.info, one_row.crt_time;
    END loop;
    
    --最后执行:
    EXCEPTION WHEN NO_DATA_FOUND THEN 
    raise notice 'no data FOUND';
    
    END 
    $$
    

      

    4.执行结果:

    00000: test id: 1
    00000: row info, id is: 1, info is: test, crt_time is: 2020-09-09 15:01:35.837492
    00000: row info, id is: 2, info is: test, crt_time is: 2020-09-09 15:01:35.837492
    00000: row info, id is: 3, info is: test, crt_time is: 2020-09-09 15:01:35.837492
    00000: row info, id is: 4, info is: test, crt_time is: 2020-09-09 15:01:35.837492
    00000: row info, id is: 5, info is: test, crt_time is: 2020-09-09 15:01:35.837492
    00000: row info, id is: 6, info is: test, crt_time is: 2020-09-09 15:01:35.837492
    00000: row info, id is: 7, info is: test, crt_time is: 2020-09-09 15:01:35.837492
    00000: row info, id is: 8, info is: test, crt_time is: 2020-09-09 15:01:35.837492
    00000: row info, id is: 9, info is: test, crt_time is: 2020-09-09 15:01:35.837492
    00000: row info, id is: 10, info is: test, crt_time is: 2020-09-09 15:01:35.837492
    00000: user fetch next: 
    00000: row info, id is: 1, info is: test, crt_time is: 2020-09-09 15:01:35.837492
    00000: row info, id is: 2, info is: test, crt_time is: 2020-09-09 15:01:35.837492
    00000: row info, id is: 3, info is: test, crt_time is: 2020-09-09 15:01:35.837492
    00000: row info, id is: 4, info is: test, crt_time is: 2020-09-09 15:01:35.837492
    00000: row info, id is: 5, info is: test, crt_time is: 2020-09-09 15:01:35.837492
    00000: row info, id is: 6, info is: test, crt_time is: 2020-09-09 15:01:35.837492
    00000: row info, id is: 7, info is: test, crt_time is: 2020-09-09 15:01:35.837492
    00000: row info, id is: 8, info is: test, crt_time is: 2020-09-09 15:01:35.837492
    00000: row info, id is: 9, info is: test, crt_time is: 2020-09-09 15:01:35.837492
    00000: row info, id is: 10, info is: test, crt_time is: 2020-09-09 15:01:35.837492
    00000: 隐式游标:
    00000: row info, id is: 1, info is: test, crt_time is: 2020-09-09 15:01:35.837492
    00000: row info, id is: 2, info is: test, crt_time is: 2020-09-09 15:01:35.837492
    00000: row info, id is: 3, info is: test, crt_time is: 2020-09-09 15:01:35.837492
    00000: row info, id is: 4, info is: test, crt_time is: 2020-09-09 15:01:35.837492
    00000: row info, id is: 5, info is: test, crt_time is: 2020-09-09 15:01:35.837492
    00000: row info, id is: 6, info is: test, crt_time is: 2020-09-09 15:01:35.837492
    00000: row info, id is: 7, info is: test, crt_time is: 2020-09-09 15:01:35.837492
    00000: row info, id is: 8, info is: test, crt_time is: 2020-09-09 15:01:35.837492
    00000: row info, id is: 9, info is: test, crt_time is: 2020-09-09 15:01:35.837492
    00000: row info, id is: 10, info is: test, crt_time is: 2020-09-09 15:01:35.837492
    

      

    5.游标属性:

    %FOUND: bool - TRUE if >1 row returned 
    %NOTFOUND:bool - TRUE if 0 rows returned 
    %ISOPEN: bool - TRUE if cursor still open 
    %ROWCOUNT:int - number of rows affected by last SQL statement
    
    注:NO_DATA_FOUND和%NOTFOUND的用法是有区别的,小结如下: 
    1)SELECT . . . INTO 语句触发 NO_DATA_FOUND; 
    2)当一个显式光标的 where 子句未找到时触发 %NOTFOUND; 
    3)当UPDATE或DELETE 语句的where 子句未找到时触发 SQL%NOTFOUND; 
    4)在光标的提取(Fetch)循环中要用 %NOTFOUND 或%FOUND 来确定循环的退出条件,不要用NO_DATA_FOUND
    

      

  • 相关阅读:
    Beta 冲刺 (3/7)
    软件产品案例分析(团队)
    Beta 冲刺 (2/7)
    Beta 冲刺1
    第七次作业
    第六次作业(计算器第四步)
    课堂作业二 PAT1025 反转链表
    第五次作业(计算器第三步)
    课堂作业一(16/05/04)
    Calculator(补)
  • 原文地址:https://www.cnblogs.com/kuang17/p/13639457.html
Copyright © 2011-2022 走看看