postgresql 游標,函數,存儲過程使用例子


CREATE OR REPLACE FUNCTION cursor_demo()  
  RETURNS refcursor AS  --返回一個游標
$BODY$  
declare  --定義變量及游標
    unbound_refcursor refcursor;  --游標
    t_accid varchar;    --變量
        t_accid2 int;    --變量

begin  --函數開始
    open unbound_refcursor for execute 'select name from cities_bak';  --打開游標 並注入要搜索的字段的記錄
    loop  --開始循環
        fetch unbound_refcursor into t_accid;  --將游標指定的值賦值給變量
          
        if found then  --任意的邏輯
            raise notice '%-',t_accid;  
        else  
            exit;  
        end if;  
    end loop;  --結束循環
    close unbound_refcursor;  --關閉游標
    raise notice 'the end of msg...';  --打印消息
    return unbound_refcursor; --為函數返回一個游標
exception when others then  --拋出異常
    raise exception 'error-----(%)',sqlerrm;--字符“%”是后面要顯示的數據的占位符
end;  --結束
$BODY$  

  LANGUAGE plpgsql;  --規定語言

 

select cursor_demo(); --調用

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM