
部分内容参考了http://blog.csdn.net/victor_ww/article/details/44240063
但是由于上面的文章,在我那里不能完全执行,所以我这边整理了一份可以运行成功的例子。
有的时候,我们会使用到函数,并且想使用函数中查询一些数据,并且当是多条结果集的时候,就需要使用游标了。
使用游标分为两种方式,这个也是自己参考上面的文章,然后自己瞎摸索出来的,多试几次。
1、没有where 条件的CREATE OR REPLACE FUNCTION cursor_demo() RETURNS refcursor AS $BODY$ declare unbound_refcursor refcursor; v_ID int; v_step_desc varchar(1000); begin open unbound_refcursor for execute 'select ID,step_desc from t_runtime_step_log'; loop fetch unbound_refcursor into v_ID,v_step_desc; if found then raise notice '%-%',v_ID,v_step_desc; 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;
上面只是声明了一个函数,外部调用需要采用下面的方式。
begin; select cursor_demo(); commit;
调用时,如果不使用begin和commit的话,会提示错误。
当然也可以再封装一个函数, 该函数中写上上面的代码。
unnamed portal
CREATE OR REPLACE FUNCTION cursor_demo3(p_condition integer) RETURNS refcursor AS $BODY$ declare bound_param_cursor cursor(ID_condition integer) for select * from t_runtime_step_log where ID > ID_condition; begin open bound_param_cursor(p_condition); return bound_param_cursor; end; $BODY$ LANGUAGE plpgsql;
上面只是声明了一个函数,外部调用需要采用下面的方式。
begin; select cursor_demo(); commit;
调用时,如果不使用begin和commit的话,会提示错误。
当然也可以再封装一个函数, 该函数中写上上面的代码。
unnamed portal
总结以上是内存溢出为你收集整理的postgresql数据库的游标使用例子说明全部内容,希望文章能够帮你解决postgresql数据库的游标使用例子说明所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)