PostgreSQL中调用存储过程并返回数据集实例

所属分类: 数据库 / PostgreSQL 阅读数: 130
收藏 0 赞 0 分享

这里用一个实例来演示PostgreSQL存储过程如何返回数据集。

1、首先准备数据表

复制代码 代码如下:

//member_category
create table member_category(id serial, name text, discount_rate real, base_integral integer);
alter table member_category add primary key(id);
alter table member_category add check(name<>'');

//member
create table member(id serial, member_num text, name text, category_id integer, account numeric(16,2), integral integer, phone text, birthday date, qq integer, email text, status integer, address text, tip text, start_date date, valid_date integer, password text, creator integer, store_name text);
alter table member add primary key(id);
alter table member add foreign key(creator) references employee;
alter table member add foreign key(category_id) references member_category;
alter table member add  onaccount int;

alter table member add &nbsp;onaccount int;
alter table member add &nbsp;store_name text;


2、插入测试数据
复制代码 代码如下:

insert into member_category(name, discount_rate, base_integral) values('白金会员', 6.5, 10000);
insert into member_category(name, discount_rate, base_integral) values('高级会员', 7.5, 1000);
insert into member_category(name, discount_rate, base_integral) values('中级会员', 8.5, 100);
insert into member_category(name, discount_rate, base_integral) values('普通会员', 9.5, 10);

insert into member(member_num, name, category_id, account, integral, phone, birthday, qq, email, onaccount, status, address, tip, start_date, valid_date, password, store_name) values('1000001', 'wuyilun', 1, 100000.00, 100000, 18814117777, '1990-12-12', 12345678, '123456@qq.com', 0, 1, 'B3-440', '超白金会员,一切免单', '2014-01-15', 1000000, 12345, '华南理工门店');
insert into member(member_num, name, category_id, account, integral, phone, birthday, qq, email, onaccount, status, address, tip, start_date, valid_date, password, store_name) values('1000002', '李小路', 2, 1000.00, 100000, 188141177234, '1990-12-12', 12345678, '123456@qq.com', 0, 1, 'B3-444', '...', '2014-01-15', 1000000, 12345, '华南理工门店');
insert into member(member_num, name, category_id, account, integral, phone, birthday, qq, email, onaccount, status, address, tip, start_date, valid_date, password, store_name) values('1000003', '洪金包', 3, 1000.00, 100000, 18814117234, '1990-12-12', 12345678, '123456@qq.com', 0, 1, 'B3-443', '...', '2014-01-15', 1000000, 12345, '华南理工门店');
insert into member(member_num, name, category_id, account, integral, phone, birthday, qq, email, onaccount, status, address, tip, start_date, valid_date, password, store_name) values('1000004', '成龙', 4, 100.00, 100000, 18814117723, '1990-12-12', 12345678, '123456@qq.com', 0, 1, 'B3-442', '...', '2014-01-15', 1000000, 12345, '华南理工门店');
insert into member(member_num, name, category_id, account, integral, phone, birthday, qq, email, onaccount, status, address, tip, start_date, valid_date, password, store_name) values('1000005', '范兵兵', 4, 100.00, 100000, 18814117327, '1990-12-12', 12345678, '123456@qq.com', 0, 1, 'B3-441', '...', '2014-01-15', 1000000, 12345, '华南理工门店');


3、创建存储过程
复制代码 代码如下:

--调用存储过程f_get_member_info, 返回会员的所有信息
--memberType:会员类型 status:会员状态  findCondition:查询条件(卡号/电话/姓名)  store_name:商店名称  
create or replace function f_get_member_info(memberType int, status int, findCondition text, store_name text) returns setof record as
$$
declare
rec record;
begin
  for rec in EXECUTE 'select m.member_num, m.name, m_t.name, m_t.discount_rate, m.account,  m.integral, m.phone, m.birthday, m.qq, m.email, m.onAccount, m.status, m.address, m.tip, m.start_date, m.valid_date, m.store_name from member m, member_category m_t where m.category_id = m_t.id and m_t.id = '|| memberType ||' and m.status = '|| status ||' and m.store_name = '''|| store_name ||''' and (m.member_num like ''%'|| findCondition ||'%'' or m.name like ''%'|| findCondition ||'%'' or m.phone like ''%'|| findCondition ||'%'');' loop
    return next rec;
  end loop;
return;
end
$$
language 'plpgsql';

4、调用存储过程
复制代码 代码如下:

--调用存储过程f_get_member_info示例
select * from f_get_member_info(4, 1, '', '华南理工门店') as member(member_num text,mname text,name text,discount_rate real,account numeric(16,2),integral int,phone text,birthday date,qq int,email text,onAccount int,status int,address text,tip text,start_date date,valid_date int,store_nam text);

5、测试结果

更多精彩内容其他人还在看

详解PostgreSQL 语法中关键字的添加

这篇文章主要介绍了详解PostgreSQL 语法中关键字的添加的相关资料,这里说明下在parser语法解析模块添加关键字,需要的朋友可以参考下
收藏 0 赞 0 分享

在Ubuntu中安装Postgresql数据库的步骤详解

PostgreSQL 是一款强大的,开源的,对象关系型数据库系统。它支持所有的主流操作系统,包括 Linux、Unix(AIX、BSD、HP-UX,SGI IRIX、Mac OS、Solaris、Tru64) 以及 Windows 操作系统。本文给大家介绍了在Ubuntu中安装P
收藏 0 赞 0 分享

Ubuntu中卸载Postgresql出错的解决方法

这篇文章主要给大家介绍了关于在Ubuntu中卸载Postgresql出错的解决方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。
收藏 0 赞 0 分享

PostgreSQL更新表时时间戳不会自动更新的解决方法

这篇文章主要为大家详细介绍了PostgreSQL更新表时时间戳不会自动更新的解决方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Abp.NHibernate连接PostgreSQl数据库的方法

这篇文章主要为大家详细介绍了Abp.NHibernate连接PostgreSQl数据库的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

PostgreSQL使用IP无法连接的解决方法

这篇文章主要介绍了PostgreSQL使用localhost可以连接/使用IP无法连接的问题解决,需要的朋友可以参考下
收藏 0 赞 0 分享

Postgresql开启远程访问的步骤全纪录

postgre一般默认为本地连接,不支持远程访问,所以如果要开启远程访问,需要更改安装文件的配置。下面这篇文章主要给大家介绍了关于Postgresql开启远程访问的相关资料,需要的朋友可以参考借鉴,下面来一起看看吧。
收藏 0 赞 0 分享

PostgreSQL中Slony-I同步复制部署教程

这篇文章主要给大家介绍了关于PostgreSQL中Slony-I同步复制部署的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用PostgreSQL具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

PostgreSQL实战之启动恢复读取checkpoint记录失败的条件详解

这篇文章主要给大家介绍了关于PostgreSQL实战之启动恢复读取checkpoint记录失败的条件的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

PostgreSQL存储过程用法实战详解

这篇文章主要介绍了PostgreSQL存储过程用法,结合具体实例详细分析了PostgreSQL数据库存储过程的定义、使用方法及相关操作注意事项,并附带一个完整实例供大家参考,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多