postgresql 函数&存储过程 ; 递归查询
函数:http://my.oschina.net/Kenyon/blog/108303
紧接上述,补充一下:
输入/输出参数的函数demo(输入作为变量影响sql结果,输出作为结果返回)
create or replace function f_dept_salary_out2(int, out o_dept text,out o_salary text)
returns setof record as
$$
declare
v_rec record;
begin
for v_rec in EXECUTE 'select departmentid as dept_id, sum(salary) as total_salary from f_get_employee() group by departmentid having departmentid='||$1 loop
o_dept:=v_rec.dept_id;
o_salary:=v_rec.total_salary;
return next;
end loop;
end;
$$
language plpgsql; select * from f_dept_salary_out2(2);
递归查询:http://my.oschina.net/kenshiro/blog/160129
由于资源页的数据源没有,因此在这里重新做了个demo,如下:
1. 准备数据
create table Tag(
id int,
name text,
parent_id int
); insert into Tag values(1,'all products',-1),(2,'plastic',1),(3,'metal',1),(4,'toy',2),(5,'furniture',2),(6,'knife',3);
这是一个产品的tag表。如下:

树形描述如下:
all products --------plastic--------toy
| |--furniture
|---metal --------knife
2. 找子孙
即:根据某个节点寻找子树。
例如:找到plastic及下面所有的tag,sql如下:
with recursive r as(
select * from Tag where id = 2
union all
select Tag.* from Tag, r where Tag.parent_id = r.id
) select * from r order by id;
输出会如下:
id name parent_id
2 plastic 1
4 toy 2
4 furniture 2
3. 找祖先
即:根据某个节点寻找其所有父系节点。
例如:找到knife的所有父辈们,sql如下:
with recursive r as (
select * from Tag where id = 6
union all
select Tag.* from Tag, r where Tag.id = r.parent_id
)
select * from r order by id desc;
输出会如下:
id name parent_id
6 knife 3
3 metal 1
1 all products -1
后记:把r理解成下"当前"记录就好了。
postgresql 函数&存储过程 ; 递归查询的更多相关文章
- PostgreSQL函数(存储过程)----笔记
PostgreSQL 函数也称为 PostgreSQL 存储过程. PostgreSQL 函数或存储过程是存储在数据库服务器上并可以使用SQL界面调用的一组SQL和过程语句(声明,分配,循环,控制流程 ...
- PostgreSQL函数如何返回数据集 [转]
PostgreSQL函数如何返回数据集 以下主要介绍PostgreSQL函数/存储过程返回数据集,或者也叫结果集的示例. 背景: PostgreSQL里面没有存储过程,只有函数,其他数据库里的这两个对 ...
- postgresql 函数 参数为复合类型
postgresql没有存储过程,但是函数功能很强大. 在近期开发的电商管理平台中,对于产品的类目管理,设计时有个属性字段,设为字符数组,但是EF不支持数组的操作,所以在添加和修改类目时,需要对属性的 ...
- 用PL/pgSQL写postgreSQL的存储过程[转]
http://blog.chinaunix.net/uid-7591044-id-1742967.html 今天学会了用 PL/pgSQL 写 postgreSQL 的存储过程,网上资料实在少得可怜, ...
- mysqldump导出--数据+结构+(函数+存储过程)
#导出某个数据库--结构+数据shell>mysqldump -h192.168.161.124 -uroot -pxxxxxx --opt db_name |gzip -9 > /db_ ...
- PostgreSQL自学笔记:6 PostgreSQL函数
6 PostgreSQL函数 6.2 数学函数 abs(x) 绝对值 pi() 圆周率π select abs(-3),pi(); cookie: MySQL中的pi()默认值3.141593, Po ...
- Java -- JDBC 学习--调用函数&存储过程
调用函数&存储过程 /** * 如何使用 JDBC 调用存储在数据库中的函数或存储过程 */ @Test public void testCallableStatment() { Connec ...
- 用Python写了一个postgresql函数,感觉很爽
用Python写了一个postgresql函数,感觉很爽 CREATE LANGUAGE plpythonu; postgresql函数 CREATE OR REPLACE FUNCTION myfu ...
- postgresql 函数返回结果集(zz)
pgsql function 系列之一:返回结果集--------------------------------------------------------------------------- ...
随机推荐
- Js获取指定Url参数
在 C#.PHP.JSP 中,都有直接获取 Url 中指定参数的方法,但 Javascript 却没有这样的现在方法,得自己写一个.在 Web 的开发过程中,获取 Url 中的参数是十分常用的操作,所 ...
- php的if语句单行与多行
//正确: //错误写法 $b = //if前面不能带 等号
- ecshop 配送方式支持"货到付款"
ecshop配送方式,支持货到付款方法. 打开表 ecs_shipping,找到字段support_cod,修改1则支持货到付款 扩展 问题描述:已经修改了ecs_shipping表里的support ...
- VisualStudio.gitignore git 忽略
https://github.com/kaedei/gitignore/blob/master/VisualStudio.gitignore
- Cobbler自动化批量部署CentOS6.5系统
Cobbler作为一个预备工具,使批量部署Red Hat/Centos/Fedora系统更容易,同时也支持Suse和Debian系统的部署. 它提供以下服务集成: * PXE服务支持 * DHCP服务 ...
- systemctl 取代 service
要使用systemd, linux内核版本要高于: 2.6.39 systemctl的命令格式: systemctl 动作命令(如start stop restart status) 服务名称.ser ...
- 来聊聊apply和call
今天在群里讨论的时候,看到有人问apply怎么使用,突然想起自己刚接触这个方法的时候,也是一样的摸不着头脑. 记得当时上网看了很多讲解,可实际用的时候还是感觉有些蒙蒙哒orz.... 后来想一想,也许 ...
- C#中的那些全局异常捕获
1.WPF全局捕获异常 public partial class App : Application { public App() { // 在异 ...
- 关于COOKIE学习的一二
index.php <?php setcookie("name","dalisng",time()+3600); setcookie("addr ...
- bootstrap-dropdown
功能:实现点击时下拉框显示 插件:dropdown.js 要点:dropdown功能往往用在导航栏.导航条上,用作标题显示.dropdown与<ul><li>标签搭配用. 以d ...