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> < ...
随机推荐
- 每天一个linux命令(29):date命令
在linux环境中,不管是编程还是其他维护,时间是必不可少的,也经常会用到时间的运算,熟练运用date命令来表示自己想要表示的时间,肯定可以给自己的工作带来诸多方便. 1.命令格式: date [参数 ...
- 第四次个人作业--必应词典(PC端)分析
part.1 调研.评测 ·bug简述: 1.初始使用时,我觉得划译功能很方便,可是在QQ和浏览器上多次使用划译功能后,我发现并不是每次划译总会做出翻译,而是显示原文而已.如下图: 2.敲代码是出现b ...
- 图片百分百问题 z-index问题
遇到的问题: 1.图片设置宽高都为100%,为什么高度没有100%呢? 我日了狗了! 答:因为图片默认高度大于包含框, 此时元素的高度100%将不会参照父元素? 继承出现了问 ...
- HTML DOM 基础
$. HTML DOM 定义了访问和操作 HTML 文档的标准方法. DOM 是 W3C(万维网联盟)的标准. $. DOM树. $. W3C 文档对象模型 (DOM) 是中立于平台和语言的接口,它 ...
- JavaWeb项目前端规范(采用命名空间使js深度解耦合)
没有规矩不成方圆,一个优秀的代码架构不仅易于开发和维护,而且是一门管理与执行的艺术. 这几年来经历了很多项目,对代码之间的强耦合及书写不规范,维护性差等问题深恶痛绝.在这里,通过仔细分析后,结合自己的 ...
- BZOJ-1491 社交网络 FLoyd+乱搞
感觉这两天一直在做乱搞的题... 1491: [NOI2007]社交网络 Time Limit: 10 Sec Memory Limit: 64 MB Submit: 1279 Solved: 732 ...
- java时间库Joda-Time
虽然在java8里面有内置的最新的时间库,但是在java8之前的版本所有的时间操作都得自己写,未免有些繁琐,如果我们不自己封装的话可以用Joda-Time这个时间库,下面写下这个库的具体用法. git ...
- FFTW中文参考
据说FFTW(Fastest Fourier Transform in the West)是世界上最快的FFT.为了详细了解FFTW以及为编程方便,特将用户手册看了一下,并结合手册制作了以下FFTW中 ...
- Linux cscope命令
一.简介 Cscope 是一款开源免费的 C/C++浏览工具,自带一个基于文本的用户界面,通过cscope可以很方便地找到某个函数或变量的定义位置.被调用的位置等信息.Cscope对 C /C++支持 ...
- codeforce 626E(二分)
E. Simple Skewness time limit per test 3 seconds memory limit per test 256 megabytes input standard ...