postgresql 函数demo
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了postgresql 函数demo,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3455字,纯文字阅读大概需要5分钟。
内容图文
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
原文:http://www.cnblogs.com/Tommy-Yu/p/4061685.html
内容总结
以上是互联网集市为您收集整理的postgresql 函数demo全部内容,希望文章能够帮你解决postgresql 函数demo所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。