3

我收到此错误:

cannot perform a DML operation inside a query

当我尝试执行查询时

select st_atten_up(1,7) from dual;

代码在下面给出。

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;

提前致谢。

4

2 回答 2

5

如果调用的函数已声明为PRAGMA AUTONOMOUS_TRANSACTION( Link ),则从技术上讲,您可以在 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;
于 2013-07-26T19:10:59.110 回答
-1

如果函数本身具有 DML 操作,则不能调用 DML 中的函数。

在这里,您将尝试在 SELECT/UPDATE 中调用该函数。这就是它给出错误的原因。如果你想这样做,请创建一个程序。

于 2013-07-26T18:59:17.353 回答