PostgreSQL函数如何返回数据集
背景: PostgreSQL里面没有存储过程,只有函数,其他数据库里的这两个对象在PG里都叫函数。 函数由函数头,体和语言所组成,函数头主要是函数的定义,变量的定义等,函数体主要是函数的实现,函数的语言是指该函数实现的方式,目前内置的有c,plpgsql,sql和internal,可以通过pg_language来查看当前DB支持的语言,也可以通过扩展来支持python等
函数返回值一般是类型,比如return int,varchar,返回结果集时就需要setof来表示。
一、数据准备
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);
二、例子
1.sql一例
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)
上面的例子相对简单,如果要返回不是表结构的数据集该怎么办呢?看下面
2.返回指定结果集
a.用新建type来构造返回的结果集
--新建的type在有些图形化工具界面中可能看不到,
要查找的话可以通过select * from pg_class where relkind='c'去查,c表示composite type
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)
这样同一个函数就可以返回不同的结果集了,很灵活。
PostgreSQL函数如何返回数据集的更多相关文章
- PostgreSQL函数如何返回数据集 [转]
PostgreSQL函数如何返回数据集 以下主要介绍PostgreSQL函数/存储过程返回数据集,或者也叫结果集的示例. 背景: PostgreSQL里面没有存储过程,只有函数,其他数据库里的这两个对 ...
- postgresql 函数 参数为复合类型
postgresql没有存储过程,但是函数功能很强大. 在近期开发的电商管理平台中,对于产品的类目管理,设计时有个属性字段,设为字符数组,但是EF不支持数组的操作,所以在添加和修改类目时,需要对属性的 ...
- PostgreSQL自学笔记:6 PostgreSQL函数
6 PostgreSQL函数 6.2 数学函数 abs(x) 绝对值 pi() 圆周率π select abs(-3),pi(); cookie: MySQL中的pi()默认值3.141593, Po ...
- PostgreSQL函数(存储过程)----笔记
PostgreSQL 函数也称为 PostgreSQL 存储过程. PostgreSQL 函数或存储过程是存储在数据库服务器上并可以使用SQL界面调用的一组SQL和过程语句(声明,分配,循环,控制流程 ...
- 用Python写了一个postgresql函数,感觉很爽
用Python写了一个postgresql函数,感觉很爽 CREATE LANGUAGE plpythonu; postgresql函数 CREATE OR REPLACE FUNCTION myfu ...
- Oracle创建表语句(Create table)语法详解及示例、、 C# 调用Oracle 存储过程返回数据集 实例
Oracle创建表语句(Create table)语法详解及示例 2010-06-28 13:59:13| 分类: Oracle PL/SQL|字号 订阅 创建表(Create table)语法详解 ...
- Entity Framework 6 Recipes 2nd Edition(11-6)译 -> 从一个”模型定义”函数里返回一个复杂类型
11-6.从一个”模型定义”函数里返回一个复杂类型 问题 想要从一个”模型定义”函数返回一个复杂类型 解决方案 假设我们有一个病人(patient)和他们访客(visit)的模型,如 Figure 1 ...
- 测试函数用Return 返回值和用函数名返回值的区别
'*************************************************************************'**模 块 名:工程1 - Form1'**说 ...
- Shell入门教程:Shell函数的返回值
shell函数返回值一般有3种方式: 1.return语句(默认的返回值) shell函数的返回值可以和其他语言的返回值一样,通过return语句返回. 比如: #!/bin/bash functio ...
- 多层数据库应用基于Delphi DataSnap方法调用的实现(一)返回数据集
从Delphi 2009开始,DataSnap技术发生了很大的变化,并在Delphi 2010和Delphi XE的后续版本中得到了持续的改进.Delphi 2009之前的DataSnap,虽然也实现 ...
随机推荐
- 【每日一题】【迭代器,泛型】2022年1月8日-NC93 设计LRU缓存结构
描述设计LRU(最近最少使用)缓存结构,该结构在构造时确定大小,假设大小为 k ,并有如下两个功能1. set(key, value):将记录(key, value)插入该结构2. get(key): ...
- <五>模板的完全特例化和非完全特例化
模板作为C++泛型编程的基础十分重要,其使得一份代码能用于处理多种数据类型.而有些时候,我们会希望对一些特定的数据类型执行不同的代码,这时就需要使用模板特例化(template specializat ...
- npm设置
1.默认安装完node.js后会自己安装npm,通过npm下载全局模块默认安装到C:\Users\wangyc\AppData\Roaming目录下,主要有两个文件夹:npm.npm-cache 2. ...
- 自定义RBAC(5)
您好,我是湘王,这是我的博客园,欢迎您来,欢迎您再来- 把实体类及Service类都准备好了之后,就可以开始继续写业务代码了.Spring Security的强大之一就在于它的拦截器.那么这里也可以参 ...
- 周结之json补充、正则re模块、hashlib模块、logging模块
周结 目录 周结 json补充 正则表达式 re模块 第三方模块的下载 request模块 办公自动化openpyxl模块 hashlib加密模块 subprocess模块 logging日志模块 j ...
- [Linux Kernel 源码分析] 通过vconfig配置vlan的系统调用/驱动流程分析
By YuCloud (蓝天上的云℡ - 博客园 https://www.cnblogs.com/yucloud/) 转载请注明出处 vconfig源码分析 vlan/vconfig.c at mas ...
- RestTemplate获取json数组
1.需求描述 接口返回的是一个json数组,要获取到接口返回值并用实体类list接住. 2.解决方法 使用springboot框间自带的Http的工具类RestTemplate调接口,其返回值用hut ...
- Base58算法加密解密(Python实现)
def b58encode(tmp:str) -> str: tmp = list(map(ord,tmp)) temp = tmp[0] base58 = "123456789ABC ...
- AtCoder Regular Contest 148 B - dp
题面 For a string \(T\) of length \(L\) consisting of d and p, let \(f(T)\) be \(T\) rotated \(180\) d ...
- windows安装wordcloud遇到的坑汇总
pip install wordcloud报错,缺少visual studio包 不要偷懒,一定要从报错的地方去下载完整版本 然后安装c++ 重启后就不会报错了