postgresql 函數demo

來源:互聯網
上載者:User

標籤:blog   io   os   ar   使用   for   sp   div   on   

create or replace function refresh_product_usage() returns void as  $$declarerec 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 loopnow_bom_id:=rec.bom_id;total_product_qty:= rec.product_qty;if cinsider_efficiency thentotal_product_qty = total_product_qty/rec.product_efficiency;end if;loopfor sub_rec in select product_id as parent_product_id from mrp_bom where id =now_bom_id loopparent_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 bomif exists(select id from product_usage where bom_id = now_bom_id and product_id = rec.product_id) thenupdate product_usage set product_qty = product_qty + total_product_qty where  bom_id = now_bom_id and product_id = rec.product_id;elseinsert into product_usage(bom_id,product_id,product_qty) values(now_bom_id, rec.product_id, total_product_qty);end if;exit;elsefor 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 loopnow_bom_id:=sub_rec.bom_id;total_product_qty = total_product_qty* sub_rec.product_qty;if cinsider_efficiency thentotal_product_qty = total_product_qty/sub_rec.product_efficiency;end if;end loop;end if;end loop;end loop;end;$$ LANGUAGE plpgsql;

 

實際上,本來打算唯寫一個sql代碼塊,也就是只要以下部分:

declarerec 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 loopnow_bom_id:=rec.bom_id;total_product_qty:= rec.product_qty;if cinsider_efficiency thentotal_product_qty = total_product_qty/rec.product_efficiency;end if;loopfor sub_rec in select product_id as parent_product_id from mrp_bom where id =now_bom_id loopparent_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 bomif exists(select id from product_usage where bom_id = now_bom_id and product_id = rec.product_id) thenupdate product_usage set product_qty = product_qty + total_product_qty where  bom_id = now_bom_id and product_id = rec.product_id;elseinsert into product_usage(bom_id,product_id,product_qty) values(now_bom_id, rec.product_id, total_product_qty);end if;exit;elsefor 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 loopnow_bom_id:=sub_rec.bom_id;total_product_qty = total_product_qty* sub_rec.product_qty;if cinsider_efficiency thentotal_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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.