函数: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 函数&存储过程 ; 递归查询的更多相关文章

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

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

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

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

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

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

  4. 用PL/pgSQL写postgreSQL的存储过程[转]

    http://blog.chinaunix.net/uid-7591044-id-1742967.html 今天学会了用 PL/pgSQL 写 postgreSQL 的存储过程,网上资料实在少得可怜, ...

  5. mysqldump导出--数据+结构+(函数+存储过程)

    #导出某个数据库--结构+数据shell>mysqldump -h192.168.161.124 -uroot -pxxxxxx --opt db_name |gzip -9 > /db_ ...

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

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

  7. Java -- JDBC 学习--调用函数&存储过程

    调用函数&存储过程 /** * 如何使用 JDBC 调用存储在数据库中的函数或存储过程 */ @Test public void testCallableStatment() { Connec ...

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

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

  9. postgresql 函数返回结果集(zz)

    pgsql function 系列之一:返回结果集--------------------------------------------------------------------------- ...

随机推荐

  1. PetaPoco 使用总结(一)

    PetaPoco 使用总结(一) 前段时间,公司的一个项目希望用一个ORM 的框架,通过对比 Dapper 和 PetaPoco ,虽然Dapper 功能很强大,速度更快. 但是最终还是选择了比较简单 ...

  2. Beyond Compare for mac 无限试用方法

    1.在官网(http://www.scootersoftware.com/download.php)下载最新的 beyond compare. 2.解压后, 把 beyond compare 复制到应 ...

  3. yum提示字符编码错误

    1.问题描述: [root@localhost data]# yum Loaded plugins: product-id, refresh-packagekit, security, subscri ...

  4. 为什么要使用class.forname在DriverManager.getConnection之前

    JDBC在getConnection之前为什么要调用Class.forName 获取一个数据库连接的通用模板如下: String driver = "oracle.jdbc.OracleDr ...

  5. JavaWeb学习总结(一)——JavaWeb开发入门

    http://www.cnblogs.com/xdp-gacl/p/3729033.html 只为成功找方法,不为失败找借口! JavaWeb学习总结(一)--JavaWeb开发入门 一.基本概念 1 ...

  6. Python之路【番外篇】回顾&类的静态字段

    回顾 回顾:字符串.列表.字典的修改关于内存的情况 一.字符串 str1 = 'luotianshuai' str2 = str1 print id(str1) print id(str2) prin ...

  7. json_decode

    <?php $json = '{"a":1,"b":2,"c":3,"d":4,"e":5}' ...

  8. HTML5实战与剖析之触摸事件(touchstart、touchmove和touchend)(转)

    HTML5中新添加了很多事件,但是由于他们的兼容问题不是很理想,应用实战性不是太强,所以在这里基本省略,咱们只分享应用广泛兼容不错的事件,日后随着兼容情况提升以后再陆续添加分享.今天为大家介绍的事件主 ...

  9. Python开发【第十五篇】:Web框架之Tornado

    概述 Tornado 是 FriendFeed 使用的可扩展的非阻塞式 web 服务器及其相关工具的开源版本.这个 Web 框架看起来有些像web.py 或者 Google 的 webapp,不过为了 ...

  10. python递归理解图

    递归:下一级只能return给自己的上一级. import re val="9-2*5/3+7/3*99/4*2998+10*568/14" val="9-2*5/3+7 ...