PostgreSQL中調(diào)用存儲(chǔ)過(guò)程并返回?cái)?shù)據(jù)集實(shí)例
這里用一個(gè)實(shí)例來(lái)演示PostgreSQL存儲(chǔ)過(guò)程如何返回?cái)?shù)據(jù)集。
1、首先準(zhǔn)備數(shù)據(jù)表
//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 onaccount int;
alter table member add store_name text;
2、插入測(cè)試數(shù)據(jù)
insert into member_category(name, discount_rate, base_integral) values('白金會(huì)員', 6.5, 10000);
insert into member_category(name, discount_rate, base_integral) values('高級(jí)會(huì)員', 7.5, 1000);
insert into member_category(name, discount_rate, base_integral) values('中級(jí)會(huì)員', 8.5, 100);
insert into member_category(name, discount_rate, base_integral) values('普通會(huì)員', 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', '超白金會(huì)員,一切免單', '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、創(chuàng)建存儲(chǔ)過(guò)程
--調(diào)用存儲(chǔ)過(guò)程f_get_member_info, 返回會(huì)員的所有信息
--memberType:會(huì)員類型 status:會(huì)員狀態(tài) findCondition:查詢條件(卡號(hào)/電話/姓名) 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、調(diào)用存儲(chǔ)過(guò)程
--調(diào)用存儲(chǔ)過(guò)程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、測(cè)試結(jié)果
- PostgreSQL存儲(chǔ)過(guò)程循環(huán)調(diào)用方式
- Postgresql 存儲(chǔ)過(guò)程(plpgsql)兩層for循環(huán)的操作
- PostgreSQL存儲(chǔ)過(guò)程用法實(shí)戰(zhàn)詳解
- Mybatis調(diào)用PostgreSQL存儲(chǔ)過(guò)程實(shí)現(xiàn)數(shù)組入?yún)鬟f
- 初識(shí)PostgreSQL存儲(chǔ)過(guò)程
- PostgreSQL 存儲(chǔ)過(guò)程的進(jìn)階講解(含游標(biāo)、錯(cuò)誤處理、自定義函數(shù)、事務(wù))
相關(guān)文章
PostgreSQL中Slony-I同步復(fù)制部署教程
這篇文章主要給大家介紹了關(guān)于PostgreSQL中Slony-I同步復(fù)制部署的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用PostgreSQL具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-06-06postgresql 存儲(chǔ)函數(shù)調(diào)用變量的3種方法小結(jié)
這篇文章主要介紹了postgresql 存儲(chǔ)函數(shù)調(diào)用變量的3種方法小結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-01-01postgreSQL中的row_number() 與distinct用法說(shuō)明
這篇文章主要介紹了postgreSQL中的row_number() 與distinct用法說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-01-01查看postgresql數(shù)據(jù)庫(kù)用戶系統(tǒng)權(quán)限、對(duì)象權(quán)限的方法
這篇文章主要介紹了查看postgresql數(shù)據(jù)庫(kù)用戶系統(tǒng)權(quán)限、對(duì)象權(quán)限的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12PostgreSQL?pg_filenode.map文件介紹
這篇文章主要介紹了PostgreSQL誤刪pg_filenode.map怎么辦,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2022-09-09postgresql 實(shí)現(xiàn)啟動(dòng)、狀態(tài)查看、關(guān)閉
這篇文章主要介紹了postgresql 實(shí)現(xiàn)啟動(dòng)、狀態(tài)查看、關(guān)閉的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-01-01