Oracle——无法在查询中执行 DML 操作
今天在调用Oracle Function遇到一个异常
这是我的函数(为了再现随手写的)
create or replace function test_f(id varchar2) return varchar2 is
Result varchar2(100);
begin
insert into sfcs_temp_17109
(sn)values(id);
commit;
return result;
end test_f;
解决方法:增加PRAGMA AUTONOMOUS_TRANSACTION(自治事务介绍)
create or replace function test_f(id varchar2) return varchar2 is
Result varchar2(100);
PRAGMA AUTONOMOUS_TRANSACTION;
begin
insert into sfcs_temp_17109
(sn)values(id);
commit;
return result;
end test_f;
然后又遇到第二个问题
function如下:
create or replace function test_f(id varchar2) return varchar2 is
Result varchar2(100);
PRAGMA AUTONOMOUS_TRANSACTION;
MSD_PN VARCHAR2(50);
begin
BEGIN
select didbar into MSD_PN from t_did where diddid = ID;
IF MSD_PN IS NOT NULL THEN
RESULT := 'ERROR:' || ID || '在系統中不存在!';
RETURN RESULT;
END IF;
END;
insert into sfcs_temp_17109 (sn) values (id);
commit;
return result;
end;
Return Result commit或者Rollback
create or replace function test_f(id varchar2) return varchar2 is
Result varchar2(100);
PRAGMA AUTONOMOUS_TRANSACTION;
MSD_PN VARCHAR2(50);
begin
BEGIN
select didbar into MSD_PN from t_did where diddid = ID;
IF MSD_PN IS NOT NULL THEN
RESULT := 'ERROR:' || ID || '在系統中不存在!';
commit;
RETURN RESULT;
END IF;
END;
insert into sfcs_temp_17109 (sn) values (id);
commit;
return result;
end;
增加commit后即可正常使用。