본문 바로가기

나는개발자다/기타

postgresql procedure에서 다른 procedure 호출하기.

procedure 1.   - call_procedure

CREATE OR REPLACE FUNCTION call_procedure(bigint, bigint, integer) RETURNS integer AS $$ declare input_1 alias for $1; input_2 alias for $2; result smallint default 0; exeResult integer default 0; BEGIN select called_procedure(compensation_point, input_user_no ) into exeResult; RAISE NOTICE 'result = %',exeResult; return result; end; $$ LANGUAGE plpgsql






procedure 2.   - called_procedure
create or replace function called_procedure(integer, bigint) returns integer as  $$
declare 
	input_point alias for $1;
	input_user_no alias for $2;
	errorResult smallint default 100;
	
begin
	update strict user_table  set  point=point+input_point 
		 where user_no = 112 ; -- 일부로 없는 user_no를 입력했다. exception 처리 할 수 있도록.
	return errorResult;
	exception when others then
		 errorResult = 111;
		return errorResult;
	
end;
$$ language plpgsql
예외처리와 같이 테스트를 한 소스이다.

procedure에서 다른 procedure를 호출하는 방법이 2가지 있다.

1. execute called_procedure()
2. select called_procedure into result

1의 방법은 단순히 procedure를 호출하는것이다.
2의 방법은 호출한 procedure의 결과값을 알기위한 방법이다.


실행방법
select call_procedure(1 ,1 ,1 )를 실행하면
pgadminTool의 출력상에서 메세지 NOTICE: result = 111 라고 나올것이다.
strick의 의미는 반드시 결과값이 1개만 나와야 한다.  
exception when condidion then 의 구문은 에러가 발생했는데 condition에 따라 로직을 처리한다. 하지만 condition의 종류는 은 아직 잘 모르겄다...  





** 다른 프로지저의 결과값을 가져온 이유는  transction처리가 되지 않기 때문이다.
** 다른 프로지저의 결과값이 정상이 아니면 rollback을 해야 한다.