I have to check that the update of a note is not inferior to 10% with a trigger. if it is greater than 10% I have to adjust it to 10% and write an error message , and if the new note is under 0 i have to set it to 0
when i try raise_application error, the update is just cancel. and my order option DBMS does not work the update is done with the good value but without message
CREATE OR REPLACE TRIGGER C3_update
BEFORE UPDATE OF note on Inscription
FOR EACH ROW
WHEN (NEW.note < OLD.note*0.9)
begin
if(:NEW.note > 0) then
:NEW.note := :OLD.note*0.9;
dbms_output.enable;
dbms_output.put_line ('la note ne peut descendre de plus de 10%');
--or
--RAISE_APPLICATION_ERROR(-20111,'the note can not be less than 10%');
end if;
if(:NEW.note < 0) then
:NEW.note := 0;
RAISE_APPLICATION_ERROR(-20011,'the note can not be under 0');
end if;
end;
/
i would like to set the note and print message error rigth now i just cancel the update or i update the note in the good way but the message do not print
From my point of view, this is a wrong approach. Business rule shouldn't be enforced by a database trigger. If I were you, I'd put it into a front-end application (the same place where a new note value is being entered).
Doing so, you'd even be able to notify user what's happening and let them decide whether they want to accept a new value (which you'd suggest, based on those conditions), or not (and, possibly, enter a new, valid value).
As you've noticed,
DBMS_OUTPUT.PUT_LINEdoesn't work. It would, if tool you use to update the note value is capable of displaying it. (By the way, did youSET SERVEROUTPUT ON?) If you're using, for example, Oracle Forms, then you'd see nothing.RAISE_APPLICATION_ERROR, when called, ends the subprogram and returns a user-defined error number and message. However, it also nullifies the update you make. Even if you set the trigger to be an autonomous transaction, it won't work. For example:See? Instead of
0, note keeps its original value (2). It never even got value 0 because trigger didn't end successfully. How to check that? By commenting theRAISEcommand:It does the job, but - the message can't be displayed (I already told you why).
If you meant to include
UPDATE inscription set note = its_new_valueinto the trigger itself, don't - you'll get a deadlock as the initial update fires a trigger which performs update which calls a trigger which performs update etc.Therefore, as far as I can tell, you can't do what you want, not with a trigger.