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的更多相关文章

  1. postgresql 函数&存储过程 ; 递归查询

    函数:http://my.oschina.net/Kenyon/blog/108303 紧接上述,补充一下: 输入/输出参数的函数demo(输入作为变量影响sql结果,输出作为结果返回) create ...

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

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

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

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

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

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

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

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

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

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

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

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

  8. Oracle 与 PostgreSQL 函数行为的差异引发性能差异

    对于Oracle,对于数据修改的操作通过存储过程处理,而对于函数一般不进行数据修改操作.同时,函数可以通过 Select 进行调用,而存储过程则不行. 一.对于volatile 函数的行为 1.Ora ...

  9. 组件嵌套+Mixin函数demo

    非DOM属性:1.dangerouslysetInnerHTML(xxs跨站攻击) 2.key 3.ref 编写组件嵌套 <!DOCTYPE html><html>  < ...

随机推荐

  1. angular实例教程(用来熟悉指令和过滤器的编写)

    angular的插件太少了,  所以很多指令和过滤器都要自己写,  所以对指令传进来的参数, 以及angular编译的流程更加熟悉才行写出好的插件, 有些简单的指令是参考angularUI里面, 作为 ...

  2. “耐撕”团队 2016.3.25 站立会议

    成员: Z 郑蕊 * 组长 (博客:http://www.cnblogs.com/zhengrui0452/), P 濮成林(博客:http://www.cnblogs.com/charliePU/) ...

  3. js 选项卡实现

    <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8&quo ...

  4. SQL Server之存储过程基础知识

    什么是存储过程呢?存储过程就是作为可执行对象存放在数据库中的一个或多个SQL命令. 通俗来讲:存储过程其实就是能完成一定操作的一组SQL语句. 那为什么要用存储过程呢?1.存储过程只在创造时进行编译, ...

  5. 【CodeForces 596A】E - 特别水的题5-Wilbur and Swimming Pool

    Description After making bad dives into swimming pools, Wilbur wants to build a swimming pool in the ...

  6. Android中实现自定义的拍照应用

    可以参考:http://www.android-doc.com/guide/topics/media/camera.html 一.添加相应的权限 <uses-permission android ...

  7. Java编程思想学习(十四) 枚举

    关键字enum可以将一组具名的值有限集合创建一种为新的类型,而这些具名的值可以作为常规的程序组件使用. 基本enum特性 调用enum的values()方法可以遍历enum实例,values()方法返 ...

  8. hihoCoder 1195 高斯消元.一

    传送门 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Ho:喂不得了啦,那边便利店的薯片半价了! 小Hi:啥?! 小Ho:那边的便利店在打折促销啊. 小Hi:走走走, ...

  9. 初识A*算法

    写这篇文章的初衷是应一个网友的要求,当然我也发现现在有关人工智能的中文站点实在太少,我在这里抛砖引玉,希望大家都来热心的参与. 还是说正题,我先拿A*算法开刀,是因为A*在游戏中有它很典型的用法,是人 ...

  10. 伪分布模式下执行wordcount实例时报错解决办法

    问题1.不能分配内存,错误提示如下: FAILEDjava.lang.RuntimeException: Error while running command to get file permiss ...