postgresql 함수 demo

3931 단어 PostgreSQL
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;


 
그런데 이상하게도 엉뚱한 문법 오류가 많이 나옵니다.
레코드/open 같은 많은 키워드를 식별할 수 없을 것 같습니다.
답답해서 함수를 썼다.
 
postgresql sql 디버그 출력에 사용가능:raise notice'your_message;%s'%your_message_var
그리고 커서의 개념이 약화되었습니다.cursor를 사용하는 것보다 forrec in select를 사용하는 것이 낫습니다...loop  ....  end loop;
결과집에서 직접 값을 부여할 방법을 찾지 못한 것이 조금 아쉽다.
동적 실행 sql 문장에서 DO/EXECUTE 사용

좋은 웹페이지 즐겨찾기