nix*_*333 3 oracle plsql oracle10g plsqldeveloper
我收到这个错误:
cannot perform a DML operation inside a query
Run Code Online (Sandbox Code Playgroud)
当我试图执行查询
select st_atten_up(1,7) from dual;
Run Code Online (Sandbox Code Playgroud)
代码如下.
create or replace FUNCTION st_atten_up(stu_id IN student_info.id%type,app_mon IN student_attendence.month%type)
RETURN NUMBER
IS
att1 NUMBER;
BEGIN SELECT ATTENDANCE into att1 FROM student_attendence
WHERE student_attendence.id = stu_id and student_attendence.month = app_mon;
att1 := att1 + 1;
UPDATE student_attendence SET ATTENDANCE = att1
where id = stu_id and month = app_mon;
return att1;
END;
Run Code Online (Sandbox Code Playgroud)
提前致谢.
如果调用的函数声明为PRAGMA AUTONOMOUS_TRANSACTION(链接),则可以在select中在技术上执行DML .但是,出于一些好的理由(包括改变表,性能下降),从语句中执行DML 很少是个好主意SELECT.但是,要回答你的问题,你可以用以下函数编写函数PRAGMA:
create or replace FUNCTION st_atten_up(stu_id IN student_info.id%type,app_mon IN student_attendence.month%type)
RETURN NUMBER
IS
PRAGMA AUTONOMOUS_TRANSACTION;
att1 NUMBER;
BEGIN SELECT ATTENDANCE into att1 FROM student_attendence
WHERE student_attendence.id = stu_id and student_attendence.month = app_mon;
att1 := att1 + 1;
UPDATE student_attendence SET ATTENDANCE = att1
where id = stu_id and month = app_mon;
return att1;
END;
Run Code Online (Sandbox Code Playgroud)