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> < ...
随机推荐
- EntityFramework_MVC4中EF5 新手入门教程之七 ---7.通过 Entity Framework 处理并发
在以前的两个教程你对关联数据进行了操作.本教程展示如何处理并发性.您将创建工作与各Department实体的 web 页和页,编辑和删除Department实体将处理并发错误.下面的插图显示索引和删除 ...
- Spring 事务配置管理,简单易懂,详细 [声明式]
Spring 事务配置说明 Spring 如果没有特殊说明,一般指是跟数据存储有关的数据操作事务操作:对于数据持久操作的事务配置,一般有三个对象,数据源,事务管理器,以及事务代理机制: Spring ...
- 第五章:javascript:队列
队列是一种列表,不同的是队列只能在末尾插入元素,在队首删除元素.队列用于存储按顺序排列的数据.先进先出.这点和栈不一样,在栈中,最后入栈的元素反被优先处理.可以将队列想象成银行排队办理业务的人,排队在 ...
- searchBar控件
那就先了解一下UISearchBar控件吧! UISearchBar控件就是要为你完成搜索功能的一个专用控件.它集成了很多你意想不到的功能和特点! 首先,还是来普及一下UISearchBar控件API ...
- 使用git管理代码的心得
一.简易使用流程 首先下载安装git,点击Git Bash进入编辑界面,之后如下图进入目录并通过命令 git init 把这个目录变成git可以管理的仓库 接下来使用git add .命令将所有文件添 ...
- C# 中的多线程
参考网站http://blog.gkarch.com/topic/threading.html
- 腾讯云ubuntu下mysqli服务的开启
腾讯云ubuntu下mysqli服务的开启 今天晚上搞了好久,在本地操作系统deepin下操作完全无需开启mysqli模块,自动就开启了.这次介绍一下服务器ubuntu下mysqli模块的开启. 首先 ...
- Spring学习8- SSH需要的jar包
struts2 commons-logging-1.0.4.jar 主要用于日志处理 freemarker-2.3.8.jar 模板相关操作需要包 ognl-2.6.11.jar ognl表达示所需包 ...
- 初学JDBC,获取插入记录的主键、执行批量操作
一.获取插入记录主键值 在创建语句的地方使用Statement.RETURN_GENERATED_KEYS标识一下,然后通过getGeneratedKeys方法获得 preparedStatement ...
- 菲涅尔反射(Fresnel Reflection)
离线渲染中,通常可以用kd,ks,kt(分别代表物体的漫反射系数,镜面反射系数,透射系数)来简单地描述一个物体的基本材质,例如,我们将一个物体设置为:kd=0,ks=0.1,kt=0.9,即代表一束光 ...