postgresql数据库的游标使用例子说明
2017-02-20 15:00
253 查看
部分内容参考了http://blog.csdn.net/victor_ww/article/details/44240063
但是由于上面的文章,在我那里不能完全执行,所以我这边整理了一份可以运行成功的例子。
有的时候,我们会使用到函数,并且想使用函数中查询一些数据,并且当是多条结果集的时候,就需要使用游标了。
使用游标分为两种方式,这个也是自己参考上面的文章,然后自己瞎摸索出来的,多试几次。
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
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和commit的话,会提示错误。
当然也可以再封装一个函数, 该函数中写上上面的代码。
unnamed portal
但是由于上面的文章,在我那里不能完全执行,所以我这边整理了一份可以运行成功的例子。
有的时候,我们会使用到函数,并且想使用函数中查询一些数据,并且当是多条结果集的时候,就需要使用游标了。
使用游标分为两种方式,这个也是自己参考上面的文章,然后自己瞎摸索出来的,多试几次。
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
2、附带where条件的
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
相关文章推荐
- gitlab 使用postgresql数据库的一些说明
- postgresql 游标,函数,存储过程使用例子
- 使用PostgreSQL数据库做PHP开发
- 1. PHP数据库连接说明及例子
- 使用游标收缩数据库日志文件并调整数据库属性选项栏部分设置
- 以实例说明如何使用C#从数据库中提取数据,按要求自动生成定制的Excel表格
- SAP ABAP/4学习--学习使用OO在ABAP中.简单入门概念.用个例子来说明
- Rave使用体会(22):Rave中连接ADO数据库的例子
- 游标使用说明
- 数据库中游标的使用.....用于循环
- window.open的例子和使用方法以及参数说明
- 使用PowerDesigner 设计数据库(http://www.ianywhere.com/上的例子)
- 【专题】window.open的例子和使用方法以及参数说明(完整版)
- 使用纯JAVA连接SQL SERVER 数据库的例子
- window.open的例子和使用方法以及参数说明
- 一个使用ibatis对数据库增删改查的例子
- SAP ABAP/4学习--学习使用OO在ABAP中.简单入门概念.用个例子来说明
- window.open的例子和使用方法以及参数说明(完整版)
- 举一个简单例子来说明:如何在JSP中使用JavaBean
- 自己写的一个使用游标的小例子