zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    系统设计与架构笔记:对我新公司网站的技术架构初解
    与国内某知名互联网公司交流后的心得
    系统架构:Web应用架构的新趋势---前端和后端分离的一点想法
    为什么做java的web开发我们会使用struts2,springMVC和spring这样的框架?
    我设计的网站的分布式架构
    Python Day 46 前端 、HTML5介绍、HTML标签、标签的嵌套规则、CSS3介绍、CSS代码中书写位置(重点)、CSS基础选择器、
    Python Day 45 手撸ORM框架
    Python Day 44 Mysql数据库备份及优化(六)
    Python Day 43 Mysql基础语法(五)sqlalchemy、创建表、增删改查、高级查询操作、正向反向查询
    Python Day 42 Mysql基础语法(四)、存储引擎、索引、慢日志查询、普通日志记录(general log)、权限管理、explain工具
  • 原文地址:https://www.cnblogs.com/Tommy-Yu/p/4061685.html
Copyright © 2011-2022 走看看