create table department(id int primary key, name text); create table employee(id int primary key, name text, salary int, departmentid int references department); insert into department values (1, 'Management'),(2, 'IT'),(3, 'BOSS'); insert into employee values (1, 'kenyon', 30000, 1); insert into employee values (2, 'francs', 50000, 1); insert into employee values (3, 'digoal', 60000, 2); insert into employee values (4, 'narutu', 120000, 3);2、例子
create or replace function f_get_employee() returns setof employee as $$ select * from employee; $$ language 'sql';等同的另外一個效果(Query)
create or replace function f_get_employee_query() returns setof employee as $$ begin return query select * from employee; end; $$ language plpgsql;查詢圖解以下
postgres=# select * from f_get_employee(); id | name | salary | departmentid ----+--------+--------+-------------- 1 | kenyon | 30000 | 1 2 | francs | 50000 | 1 3 | digoal | 60000 | 2 4 | narutu | 120000 | 3 (4 rows)查詢出來的函數還能夠像普通的表同樣按條件查詢 ,但若是查詢的方式不同,則結果也不同,如下查詢方式將會獲得相似數組的效果
postgres=# select f_get_employee(); f_get_employee --------------------- (1,kenyon,30000,1) (2,francs,50000,1) (3,digoal,60000,2) (4,narutu,120000,3) (4 rows)由於返回的結果集相似一個表的數據集,PostgreSQL還支持對該函數執行結果進行條件判斷並過濾
postgres=# select * from f_get_employee() where id >3; id | name | salary | departmentid ----+--------+--------+-------------- 4 | narutu | 120000 | 3 (1 row)上面的例子相對簡單,若是要返回不是表結構的數據集該怎麼辦呢?看下面
--新建的type在有些圖形化工具界面中可能看不到,
要查找的話能夠經過select * from pg_class where relkind='c'去查,c表示composite type php
create type dept_salary as (departmentid int, totalsalary int); create or replace function f_dept_salary() returns setof dept_salary as $$ declare rec dept_salary%rowtype; begin for rec in select departmentid, sum(salary) as totalsalary from f_get_employee() group by departmentid loop return next rec; end loop; return; end; $$ language 'plpgsql';b.用Out傳出的方式
create or replace function f_dept_salary_out(out o_dept text,out o_salary text) returns setof record as $$ declare v_rec record; begin for v_rec in select departmentid as dept_id, sum(salary) as total_salary from f_get_employee() group by departmentid loop o_dept:=v_rec.dept_id; o_salary:=v_rec.total_salary; return next; end loop; end; $$ language plpgsql;執行結果:
postgres=# select * from f_dept_salary(); departmentid | totalsalary --------------+------------- 1 | 80000 3 | 120000 2 | 60000 (3 rows) postgres=# select * from f_dept_salary_out(); o_dept | o_salary --------+---------- 1 | 80000 3 | 120000 2 | 60000 (3 rows)c.根據執行函數變量不一樣返回不一樣數據集
create or replace function f_get_rows(text) returns setof record as $$ declare rec record; begin for rec in EXECUTE 'select * from ' || $1 loop return next rec; end loop; return; end $$ language 'plpgsql';執行結果:
postgres=# select * from f_get_rows('department') as dept(deptid int, deptname text); deptid | deptname --------+------------ 1 | Management 2 | IT 3 | BOSS (3 rows) postgres=# select * from f_get_rows('employee') as employee(employee_id int, employee_name text,employee_salary int,dept_id int); employee_id | employee_name | employee_salary | dept_id -------------+---------------+-----------------+--------- 1 | kenyon | 30000 | 1 2 | francs | 50000 | 1 3 | digoal | 60000 | 2 4 | narutu | 120000 | 3 (4 rows)這樣同一個函數就能夠返回不一樣的結果集了,很靈活。 參考:http://bbs.pgsqldb.com/client/post_show.php?zt_auto_bh=53950