Triggerzzzz (like a tiger) [message #364283] |
Sun, 07 December 2008 18:56 |
lolly
Messages: 5 Registered: December 2008 Location: Montreal
|
Junior Member |
|
|
Hello everyone! I am very happy to see there are lots of pple here!! I am actually trying to build a trigger, but it's invalid.
Im 99% sure about the select part, seems like I got a problem with some ;
here is what I did up to now, if anybody could help me, this would be really nice! thanks
------------------------------
CREATE TRIGGER TRIGVENTES
BEFORE UPDATE OR INSERT ON VENTE
FOR EACH ROW
DECLARE
resultat number;
BEGIN
SELECT COUNT(*) INTO resultat
FROM VENTE V, CROISIERE CR, VOYAGE VO, NAVIRE N, CABINE CA
WHERE V.CODE_CROISIERE = CR.CODE_CROISIERE
AND CR.CODE_VOYAGE = VO.CODE_VOYAGE
AND VO.NO_NAVIRE = N.NO_NAVIRE
AND N.NO_NAVIRE = CA.NO_NAVIRE
AND V.CODE_CROISIERE = :NEW.CODE_CROISIERE
AND V.NO_CABINE = :NEW.NO_CABINE;
IF (resultat := 0)
THEN raise_application_error(-20001, 'NO_CABINE APPARTIENT PAS AU NAVIRE');
END IF;
END ;
|
|
|
Re: Triggerzzzz (like a tiger) [message #364288 is a reply to message #364283] |
Sun, 07 December 2008 20:07 |
rleishman
Messages: 3728 Registered: October 2005 Location: Melbourne, Australia
|
Senior Member |
|
|
Since you decided not to tell us what the error was, I'll use my super-spidey-senses to work it out for myself.
HHHHHHHHHMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM??????????
Got it! You got a Mutating Table error, right?
A FOR EACH ROW trigger cannot access the table that it is firing on. Take VENTE out of the SQL and replace the V.<> columns in the SQL with :NEW.<>
Ross Leishman
|
|
|
|
|
|
|
|
|
|
|
Re: Triggerzzzz (like a tiger) [message #364300 is a reply to message #364297] |
Sun, 07 December 2008 21:57 |
|
Barbara Boehmer
Messages: 9101 Registered: November 2002 Location: California, USA
|
Senior Member |
|
|
Here is an example for you, using the emp and dept demo tables, showing descriptions of the tables, compilation of the trigger, test of valid insert, and test of invalid insert, all from SQL*Plus. Perhaps you can compare this to what you are trying to do.
SCOTT@orcl_11g> DESCRIBE emp
Name Null? Type
----------------------------------------- -------- ----------------------------
EMPNO NOT NULL NUMBER(4)
ENAME VARCHAR2(10)
JOB VARCHAR2(9)
MGR NUMBER(4)
HIREDATE DATE
SAL NUMBER(7,2)
COMM NUMBER(7,2)
DEPTNO NUMBER(2)
SCOTT@orcl_11g> DESCRIBE dept
Name Null? Type
----------------------------------------- -------- ----------------------------
DEPTNO NUMBER(2)
DNAME VARCHAR2(14)
LOC VARCHAR2(13)
SCOTT@orcl_11g> CREATE OR REPLACE TRIGGER example_trigger
2 BEFORE UPDATE OR INSERT ON emp
3 FOR EACH ROW
4 DECLARE
5 resultat NUMBER;
6 BEGIN
7 SELECT COUNT(*)
8 INTO resultat
9 FROM dept d
10 WHERE d.deptno = :NEW.deptno;
11 --
12 IF resultat = 0
13 THEN raise_application_error(-20001, 'NO SUCH DEPARTMENT');
14 END IF;
15 END example_trigger;
16 /
Trigger created.
SCOTT@orcl_11g> SHOW ERRORS
No errors.
SCOTT@orcl_11g> -- test of valid insert:
SCOTT@orcl_11g> INSERT INTO emp (deptno, empno) VALUES (40, 99)
2 /
1 row created.
SCOTT@orcl_11g> -- test of invalid insert:
SCOTT@orcl_11g> INSERT INTO emp (deptno, empno) VALUES (50, 88)
2 /
INSERT INTO emp (deptno, empno) VALUES (50, 88)
*
ERROR at line 1:
ORA-20001: NO SUCH DEPARTMENT
ORA-06512: at "SCOTT.EXAMPLE_TRIGGER", line 10
ORA-04088: error during execution of trigger 'SCOTT.EXAMPLE_TRIGGER'
SCOTT@orcl_11g>
|
|
|
Re: Triggerzzzz (like a tiger) [message #364302 is a reply to message #364300] |
Sun, 07 December 2008 22:18 |
lolly
Messages: 5 Registered: December 2008 Location: Montreal
|
Junior Member |
|
|
Oh mmmmmy. I've been on this for 12 hours. compiler was retarded: I hate to alter trigger and save there, and it worked #1.
I have to tank all of you for your help
and for info, this was while creating trigger!
it was stopping at every ";"
joyful joyful loooord
|
|
|