在一个项目中,时不时碰到ORA-14551: cannot perform a DML operation inside a query 错误,定位问题时,发现是在某一个函数的含有for update 的语句抛出异常,这个函数在多个地方调用,调试也没发现异常,后来做了个实验才知道原因,实验如下:
create or replace function f_test return number
is
l_rec number;
begin
select 1 into l_rec from dual for update nowait;
return l_rec;
Exception
when others then
Raise_application_error(-20000, '失败'||sqlerrm);
return -1;
End;
declare
l_num number;
begin
l_num := f_test; -------①
select f_test into l_num from dual; ----------②
exception
when others then
Raise_application_error(-20000, '失败'||sqlerrm);
end;
在①这步没有异常,在②这里就抛出异常了,原来for update 会给记录加锁(暗示你要修改记录),但是在select 同时你又要修改记录,这在oracle是不允许的,所以会抛出ORA-14551异常,其他调用就没有问题。
create or replace function f_test return number
is
l_rec number;
begin
select 1 into l_rec from dual for update nowait;
return l_rec;
Exception
when others then
Raise_application_error(-20000, '失败'||sqlerrm);
return -1;
End;
declare
l_num number;
begin
l_num := f_test; -------①
select f_test into l_num from dual; ----------②
exception
when others then
Raise_application_error(-20000, '失败'||sqlerrm);
end;
在①这步没有异常,在②这里就抛出异常了,原来for update 会给记录加锁(暗示你要修改记录),但是在select 同时你又要修改记录,这在oracle是不允许的,所以会抛出ORA-14551异常,其他调用就没有问题。
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/30741760/viewspace-2098847/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/30741760/viewspace-2098847/