需要幫助來處理以下要求。
我們需要處理可能出現在 pl sql 塊中的例外,并將 select 陳述句中的某些值記錄到定制的表 - audit_log 中。例如:
audit_log 表結構:col1、stored_procedure_name、error_code
CREATE OR REPLACE PROCEDURE SP_TEMP()
LANGUAGE plpgsql
AS $procedure$
declare
begin
/* loop through the data in table_a */
for sq in (select a.column1,a.column2..a.columnN from table_a a )
loop
/*Do some operations (not shown here) and select data from table_b */
(
select col1, col2, col3
from table_b b where
b.col1=sq.column1 )
/*insert into table_c*/
insert into table_c
values(sq.column1,sq.column2,b.col2,b.col3);
end loop;
EXCEPTION:
WHEN OTHERS THEN
/* Log the failure information to audit_log table */
insert into audit_log
values(column1, 'SP_TEMP',SQLERRM)
end
$procedure$
;
是否有可能做到這一點?如何將 column1 值傳遞給例外?
我們無法將 column1 值傳遞給例外。
uj5u.com熱心網友回復:
在游標回圈內創建一個嵌套(內部塊)。然后把你的exception處理放在這個塊里。
create or replace procedure sp_temp()
language plpgsql
as $$
declare
begin
/* loop through the data in table_a */
for sq in (select a.column1,a.column2..a.columnn from table_a a )
loop
begin -- inner block to allow processing the exception
/*do some operations (not shown here) and select data from table_b */
(
select col1, col2, col3
from table_b b where
b.col1=sq.column1 )
/*insert into table_c*/
insert into table_c
values(sq.column1,sq.column2,b.col2,b.col3);
exception
when others then
/* log the failure information to audit_log table */
insert into audit_log
values(sq.column1, 'sp_temp',sqlerrm);
end; -- inner block
end loop;
end;
$$;
注意:小心when others作為例外塊中的唯一謂詞。您可能希望處理某些情況并繼續,而其他情況則中止處理。when others僅作為最后的手段使用。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/525218.html
上一篇:為什么Visualizer.getfft激發java_vm_ext.cc:570]JNI檢測到應用程式錯誤:jarray為NULL
