背景: 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函数如何返回数据集的更多相关文章

  1. PostgreSQL函数如何返回数据集 [转]

    PostgreSQL函数如何返回数据集 以下主要介绍PostgreSQL函数/存储过程返回数据集,或者也叫结果集的示例. 背景: PostgreSQL里面没有存储过程,只有函数,其他数据库里的这两个对 ...

  2. postgresql 函数 参数为复合类型

    postgresql没有存储过程,但是函数功能很强大. 在近期开发的电商管理平台中,对于产品的类目管理,设计时有个属性字段,设为字符数组,但是EF不支持数组的操作,所以在添加和修改类目时,需要对属性的 ...

  3. PostgreSQL自学笔记:6 PostgreSQL函数

    6 PostgreSQL函数 6.2 数学函数 abs(x) 绝对值 pi() 圆周率π select abs(-3),pi(); cookie: MySQL中的pi()默认值3.141593, Po ...

  4. PostgreSQL函数(存储过程)----笔记

    PostgreSQL 函数也称为 PostgreSQL 存储过程. PostgreSQL 函数或存储过程是存储在数据库服务器上并可以使用SQL界面调用的一组SQL和过程语句(声明,分配,循环,控制流程 ...

  5. 用Python写了一个postgresql函数,感觉很爽

    用Python写了一个postgresql函数,感觉很爽 CREATE LANGUAGE plpythonu; postgresql函数 CREATE OR REPLACE FUNCTION myfu ...

  6. Oracle创建表语句(Create table)语法详解及示例、、 C# 调用Oracle 存储过程返回数据集 实例

    Oracle创建表语句(Create table)语法详解及示例 2010-06-28 13:59:13|  分类: Oracle PL/SQL|字号 订阅 创建表(Create table)语法详解 ...

  7. Entity Framework 6 Recipes 2nd Edition(11-6)译 -> 从一个”模型定义”函数里返回一个复杂类型

    11-6.从一个”模型定义”函数里返回一个复杂类型 问题 想要从一个”模型定义”函数返回一个复杂类型 解决方案 假设我们有一个病人(patient)和他们访客(visit)的模型,如 Figure 1 ...

  8. 测试函数用Return 返回值和用函数名返回值的区别

    '*************************************************************************'**模 块 名:工程1 - Form1'**说   ...

  9. Shell入门教程:Shell函数的返回值

    shell函数返回值一般有3种方式: 1.return语句(默认的返回值) shell函数的返回值可以和其他语言的返回值一样,通过return语句返回. 比如: #!/bin/bash functio ...

  10. 多层数据库应用基于Delphi DataSnap方法调用的实现(一)返回数据集

    从Delphi 2009开始,DataSnap技术发生了很大的变化,并在Delphi 2010和Delphi XE的后续版本中得到了持续的改进.Delphi 2009之前的DataSnap,虽然也实现 ...

随机推荐

  1. go-carbon 1.5.2版本发布, 修复已知 bug 和新增功能及葡萄牙语翻译文件

    carbon 是一个轻量级.语义化.对开发者友好的golang时间处理库,支持链式调用. 目前已被 [awesome-go](https://github.com/avelino/awesome-go ...

  2. node-sass报错(Node Sass could not find a binding for your current environment)

    解决方案:参考 https://stackoverflow.com/questions/37986800/node-sass-couldnt-find-a-binding-for-your-curre ...

  3. 跨机房ES同步实战

    作者:谢泽华 背景 众所周知单个机房在出现不可抗拒的问题(如断电.断网等因素)时,会导致无法正常提供服务,会对业务造成潜在的损失.所以在协同办公领域,一种可以基于同城或异地多活机制的高可用设计,在保障 ...

  4. 从面试题入手,畅谈 Vue 3 性能优化

    前言 今年又是一个非常寒冷的冬天,很多公司都开始人员精简.市场从来不缺前端,但对高级前端的需求还是特别强烈的.一些大厂的面试官为了区分候选人对前端领域能力的深度,经常会在面试过程中考察一些前端框架的源 ...

  5. vue 点击按钮添加一行dom节点

    如图,最近项目需求,点击添加一行dom节点,包含下拉框和input输入框 ,下面展示一下代码 <ul class="sales-menuItem-ul"> <li ...

  6. [python]《Python编程快速上手:让繁琐工作自动化》学习笔记7

    1. 用GUI 自动化控制键盘和鼠标第18章 (代码下载) pyautogui模块可以向Windows.OS X 和Linux 发送虚拟按键和鼠标点击.根据使用的操作系统,在安装pyautogui之前 ...

  7. python进阶之路18 os、sys、json模块

    os模块与sys模块 os模块主要与操作系统打交道 sys模块主要与python解释器打交道 os模块(重要) os模块主要与代码运行所在的操作系统打交道 import os os.path.spli ...

  8. [cocos2d-x]关于坐标系

    本文从cocos2dx官网看到,搬运过来学习一下. cocos2d-x3.X的坐标系 Cocos2d-x坐标系和OpenGL坐标系相同,都是起源于笛卡尔坐标系. 笛卡尔坐标系中定义右手系原点在左下角, ...

  9. 这可能是Matplotlib和Seaborn最全的入门文档

    matplotlib是python第一个数据可视化库,在数据分析,可视化领域的地位和贡献是无法磨灭的.但也正是因为有了这位老大哥的出现给后续基于matplotlib实现的绘图库实现了可能. 而对于绘图 ...

  10. html内容超宽后,缩小可视区域后,会引起部分背景色宽度出现显示异常情况,解决如下: