0

I have written a case statement which gets the current status of an application which a user has made; this statement fires in a trigger whenever a users application status changes. When their status becomes 'Accepted' I want my students table to have a row updated to indicate they are on placement.

I know the method works as I tested it by returning 1 and 0 on a previous version, but since incorporating an UPDATE method my trigger keeps failing, how would I go about incorporating an UPDATE call on my case statement?

Case statement code:

SELECT DISTINCT CASE 
   WHEN get_status(:NEW.status_id) = LOWER('Applicant Accepted Offer')
THEN
UPDATE students 
   SET students.student_on_placement = 1 
   WHERE applications.student_id = :OLD.student_id;   
END AS status_result 
FROM status;

get_status method (too avoid questions about my method)

CREATE OR REPLACE FUNCTION get_status( this_id NUMBER ) 
RETURN VARCHAR2 
AS this_type status.status_type%TYPE;
BEGIN
   SELECT status_type
      INTO this_type
   FROM status
   WHERE status_id = this_id;

   RETURN LOWER(this_type);
END get_status;
Halfpint
  • 3,967
  • 9
  • 50
  • 92

1 Answers1

2

It is not possible to perform a DML operation inside a SELECT query, or otherwise, you get ORA-14551 error:

ORA-14551: cannot perform a DML operation inside a query

Cause: DML operation like insert, update, delete or select-for-update cannot be performed inside a query or under a PDML slave.

Action: Ensure that the offending DML operation is not performed or use an autonomous transaction to perform the DML operation within the query or PDML slave.

http://ora-14551.ora-code.com/

Thus, either the DML and the query must be separated OR the update statement must be put in a function which executes in an autonomous transaction. Please check this Oracle documentation here:

http://docs.oracle.com/cd/B19306_01/appdev.102/b14261/autonotransaction_pragma.htm

RGO
  • 4,586
  • 3
  • 26
  • 40
  • Thanks Reza, I will try to think of a way to work around this. – Halfpint Nov 29 '13 at 14:28
  • Also, similar problem on SO: http://stackoverflow.com/questions/8729236/solution-to-cannot-perform-a-dml-operation-inside-a-query – RGO Nov 29 '13 at 14:33
  • I think I will try and create a variable and then set it to 1 or 0 in the CASE statement, then perform the DML operation based on its value, thanks for your help :) – Halfpint Nov 29 '13 at 14:33