postgresql 游标简单例子

来源:互联网 发布:淘宝联盟一直不提现 编辑:程序博客网 时间:2024/06/09 14:31
CREATE OR REPLACE FUNCTION cursor_demo()  
  RETURNS refcursor AS  //返回一个游标
$BODY$  
declare  //定义变量及游标
    unbound_refcursor refcursor;  //游标
    t_accid int;    //变量
begin  //函数开始
    open unbound_refcursor for execute 'select acc_id from t_account';  //打开游标 并注入要搜索的字段的记录
    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() 调用


详细请看http://blog.csdn.net/victor_ww/article/details/44240063




1 0
原创粉丝点击