postgresql 函数demo
create or replace function refresh_product_usage() returns void as $$
declare
rec record;
sub_rec record;
init_pro_id integer;
parent_product_id integer;
now_bom_id integer;
total_product_qty float;
cinsider_efficiency boolean:=true; begin
TRUNCATE TABLE product_usage;
for rec in select id,bom_id,product_id,product_qty,product_efficiency from mrp_bom where bom_id is not null loop
now_bom_id:=rec.bom_id;
total_product_qty:= rec.product_qty;
if cinsider_efficiency then
total_product_qty = total_product_qty/rec.product_efficiency;
end if;
loop
for sub_rec in select product_id as parent_product_id from mrp_bom where id =now_bom_id loop
parent_product_id:=sub_rec.parent_product_id;
end loop; if not exists(select id from mrp_bom where bom_id is not null and product_id = parent_product_id ) then --(no record)-->root bom
if exists(select id from product_usage where bom_id = now_bom_id and product_id = rec.product_id) then
update product_usage set product_qty = product_qty + total_product_qty where bom_id = now_bom_id and product_id = rec.product_id;
else
insert into product_usage(bom_id,product_id,product_qty) values(now_bom_id, rec.product_id, total_product_qty);
end if;
exit;
else
for sub_rec in select bom_id,product_qty,product_efficiency from mrp_bom where bom_id is not null and product_id = parent_product_id limit 1 loop
now_bom_id:=sub_rec.bom_id;
total_product_qty = total_product_qty* sub_rec.product_qty;
if cinsider_efficiency then
total_product_qty = total_product_qty/sub_rec.product_efficiency;
end if;
end loop;
end if; end loop; end loop;
end;
$$ LANGUAGE plpgsql;
实际上,本来打算只写一个sql代码块,也就是只要以下部分:
declare
rec record;
sub_rec record;
init_pro_id integer;
parent_product_id integer;
now_bom_id integer;
total_product_qty float;
cinsider_efficiency boolean:=true; begin
TRUNCATE TABLE product_usage;
for rec in select id,bom_id,product_id,product_qty,product_efficiency from mrp_bom where bom_id is not null loop
now_bom_id:=rec.bom_id;
total_product_qty:= rec.product_qty;
if cinsider_efficiency then
total_product_qty = total_product_qty/rec.product_efficiency;
end if;
loop
for sub_rec in select product_id as parent_product_id from mrp_bom where id =now_bom_id loop
parent_product_id:=sub_rec.parent_product_id;
end loop; if not exists(select id from mrp_bom where bom_id is not null and product_id = parent_product_id ) then --(no record)-->root bom
if exists(select id from product_usage where bom_id = now_bom_id and product_id = rec.product_id) then
update product_usage set product_qty = product_qty + total_product_qty where bom_id = now_bom_id and product_id = rec.product_id;
else
insert into product_usage(bom_id,product_id,product_qty) values(now_bom_id, rec.product_id, total_product_qty);
end if;
exit;
else
for sub_rec in select bom_id,product_qty,product_efficiency from mrp_bom where bom_id is not null and product_id = parent_product_id limit 1 loop
now_bom_id:=sub_rec.bom_id;
total_product_qty = total_product_qty* sub_rec.product_qty;
if cinsider_efficiency then
total_product_qty = total_product_qty/sub_rec.product_efficiency;
end if;
end loop;
end if; end loop; end loop;
end;
但奇怪的是会报很多莫名其妙的语法错误:
貌似无法识别很多诸如 record / open 之类的关键字。
郁闷之下写了个函数。
postgresql 用于sql debug输出可以用:raise notice 'your_message;%s'%your_message_var
然后游标的概念弱化了,与其用cursor,不如直接用 for rec in select .... loop .... end loop;
有点小遗憾没有找到从结果集里直接赋值的方法。
动态执行sql语句使用DO/EXECUTE
postgresql 函数demo的更多相关文章
- postgresql 函数&存储过程 ; 递归查询
函数:http://my.oschina.net/Kenyon/blog/108303 紧接上述,补充一下: 输入/输出参数的函数demo(输入作为变量影响sql结果,输出作为结果返回) create ...
- 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 ...
- PostgreSQL函数如何返回数据集 [转]
PostgreSQL函数如何返回数据集 以下主要介绍PostgreSQL函数/存储过程返回数据集,或者也叫结果集的示例. 背景: PostgreSQL里面没有存储过程,只有函数,其他数据库里的这两个对 ...
- postgresql 函数返回结果集(zz)
pgsql function 系列之一:返回结果集--------------------------------------------------------------------------- ...
- Oracle 与 PostgreSQL 函数行为的差异引发性能差异
对于Oracle,对于数据修改的操作通过存储过程处理,而对于函数一般不进行数据修改操作.同时,函数可以通过 Select 进行调用,而存储过程则不行. 一.对于volatile 函数的行为 1.Ora ...
- 组件嵌套+Mixin函数demo
非DOM属性:1.dangerouslysetInnerHTML(xxs跨站攻击) 2.key 3.ref 编写组件嵌套 <!DOCTYPE html><html> < ...
随机推荐
- android学习——Android Studio下创建menu布局文件
一.问题: android studio项目中没有看到menu文件夹: 在android studio项目中想要添加menu布局文件,一开始我的做法是:直接在res文件夹右键选择xml文件来添加,如下 ...
- Andriod开发环境的发展演变
安卓早先只能使用JAVA开发应用程序,现在支持多种编程语言,方便了许多程序员,但主流的应该还是JAVA语言.早先安卓开发经常用到eclipse,但自从google 发布了Android studio后 ...
- zabbix 客户端的安装
这里我们在客户端安装就是用rpm的安装方式了: 在我使用RPM安装的时候遇到了一个错误 [root@git src]# rpm -ivh http://repo.zabbix.com/zabbix/3 ...
- oracle-2-sql数据操作和查询
主要内容: >oracle 数据类型 >sql建表和约束 >sql对数九的增删改 >sql查询 >oracle伪例 1.oracle的数据类型 oracle数据库的核心是 ...
- .net架构设计读书笔记--第二章 设计体系结构
第五节 探索领域架构 一.领域驱动设计的价值与意义 最初在java中使用,.net要晚些才引入.领域驱动设计出现之初的争议.一个向导,少走弯路 1. 我们真的需要DDD吗? DDD并不适用于每个软 ...
- sort+awk+uniq三者结合使用
(1)统计文件中出现次数最多的前10个单词 #ps -ef > ps.file #cat ps.file | awk ‘{print $1}’ | sort | uniq -c | sort - ...
- ARP协议格式、ARP运行机制入门学习
相关学习资料 http://baike.baidu.com/view/149421.htm?fromtitle=ARP%E5%8D%8F%E8%AE%AE&fromid=1742212& ...
- 新建的 web 工程 有红色的惊叹号
新建的 web 工程 有红色的感叹号问题: 在eclipse 中新建一个web工程,但是工程上有红色的感叹号.解决: 1.右键工程,选择Build Path-->Configur ...
- iOS 知识点梳理
OC的理解与特性 OC作为一门面向对象的语言,自然具有面向对象的语言特性:封装.继承.多态.它既具有静态语言的特性(如C++),又有动态语言的效率(动态绑定.动态加载等).总体来讲,OC确实是一门不错 ...
- nginx配置ssl双向验证 nginx https ssl证书配置
1.安装nginx 参考<nginx安装>:http://www.ttlsa.com/nginx/nginx-install-on-linux/ 如果你想在单IP/服务器上配置多个http ...