ORACLE NOCOPY的用法
DECLARE
l_1 NUMBER := 10;
l_2 NUMBER := 20;
l_3 NUMBER := 30;
PROCEDURE test_out(p1 IN NUMBER,
x1 IN OUT NUMBER,
x2 IN OUT NOCOPY NUMBER) IS
BEGIN
x1 := p1;
dbms_output.put_line('inside test_out, x1=' || x1);
x2 := p1;
dbms_output.put_line('inside test_out, x2=' || x2);
(--)raise_application_error(-20005, 'test NOCOPY');
END;
BEGIN
dbms_output.put_line('before, l_1=' || l_1 || ', l_2=' || l_2 ||
', l_3=' || l_3);
BEGIN
--the OUT parameter has no value at all until the program terminates successfully,
--unless you have requested use of the NOCOPY hint
test_out(l_1, l_2, l_3);
EXCEPTION
WHEN OTHERS THEN
dbms_output.put_line('SQLCODE => ' || SQLCODE || ', SQLERRM => ' ||
SQLERRM);
END;
dbms_output.put_line('after, l_1=' || l_1 || ', l_2=' || l_2 || ', l_3=' || l_3);
END;
如果不mark掉 raise_application_error,輸出結果:
before, l_1=10, l_2=20, l_3=30
inside test_out, x1=10
inside test_out, x2=10
SQLCODE => -20005, SQLERRM => ORA-20005: test NOCOPY
after, l_1=10, l_2=20, l_3=10
如果mark掉 raise_application_error,輸出結果:
before, l_1=10, l_2=20, l_3=30
inside test_out, x1=10
inside test_out, x2=10
after, l_1=10, l_2=10, l_3=10
得出結論:
out 參數在程序正常下,是要返回賦予其值,而出現錯誤時,則不作任何處理,即使在賦值之後,仍然還是原值。
out nocopy 則是在程序出現錯誤情況下,仍然返回賦值之後的值。