PostgresSQL

[PostgresSQL] plsql(plpgsql) procedure/function 결과 파일로 output

박진만 2022. 7. 5. 10:33
반응형

procedure 수행 시 오류 발생 내용을 파일로 생성해서 확인하고 싶었다.

CREATE OR REPLACE PROCEDURE cloud.check_insert_error()
 LANGUAGE plpgsql
AS $procedure$
declare 

/***************************************************************************/
/* 임시변수 선언                                                              */
/***************************************************************************/
n_insert_cnt numeric := 0; -- 작업 건수
n_error_cnt numeric := 0; -- 오류 건수
n_re_insert_cnt numeric := 0; -- 오류 발생 후 재작업 건수
n_re_error_cnt numeric := 0; -- 재작업 오류 건수
n_error_cn text; -- 오류 내용
n_result_cn text; -- 작업 결과 내용

n_sqlstate text;
n_message_text text;

/********************************************************************************/
/* CURSOR 선언                                                                                                                                                       */ 
/********************************************************************************/
l_cursor RECORD;

/********************************************************************************/
/* CURSOR 문 선언                                                                                                                                                    */
/********************************************************************************/
c_cursor CURSOR FOR
SELECT a
	, b
	, c
	, d
	, e
	, f
	, g
	, h 
FROM table_a;

/***************************************************************************/
/* BEGIN PROCEDURE                                                         */
/***************************************************************************/
BEGIN

	FOR l_cursor IN c_cursor loop
		
		BEGIN
			
			INSERT INTO table_b
			(a, b, c, d, e, f, g, h)
			VALUES(
			l_cursor.a
			, l_cursor.b
			, l_cursor.c
			, l_cursor.d
			, l_cursor.e
			, l_cursor.f
			, l_cursor.g
            , l_cursor.h
			);
            
            n_insert_cnt := n_insert_cnt + 1;
			
			EXCEPTION WHEN OTHERS THEN  
				GET STACKED diagnostics 
			   	n_sqlstate := RETURNED_SQLSTATE,
				n_message_text := MESSAGE_TEXT;
				n_error_cn := concat(n_error_cn, (chr(13)||'오류 데이터 a : '||l_cursor.a||' ['||n_sqlstate||'] '||n_message_text));
			
				n_error_cnt := n_error_cnt + 1;
                
                BEGIN
                	INSERT INTO table_b
                    (a, b, c, d, e, f, g, h)
                    VALUES(
                    l_cursor.a
                    , l_cursor.b
                    , l_cursor.c
                    , l_cursor.d
                    , l_cursor.e
                    , l_cursor.f
                    , l_cursor.g
                    , l_cursor.h
                    );
                    
                    n_re_insert_cnt := n_re_insert_cnt + 1;

                    EXCEPTION WHEN OTHERS THEN  
                        GET STACKED diagnostics 
                        n_sqlstate := RETURNED_SQLSTATE,
                        n_message_text := MESSAGE_TEXT;
                        n_error_cn := concat(n_error_cn, (chr(13)||'재작업 오류 데이터 a : '||l_cursor.a||' ['||n_sqlstate||'] '||n_message_text));

                        n_re_error_cnt := n_re_error_cnt + 1;
                
                END;
		
		END;
		
	END LOOP;	
	
	n_result_cn := '작업 건수 : ' || n_insert_cnt || ', 오류 건수 : ' || n_error_cnt || ', 오류 발생 후 재작업 건수 : ' || n_re_insert_cnt || ', 재작업 오류 건수 : ' || n_re_error_cnt || n_error_cn;

	execute format($fmt$
		copy (select '%s') to 'D:\check_insert_error_result.txt' (format csv)
		$fmt$, n_result_cn);

	COMMIT;

END $procedure$
;

 

반응형