views:

86

answers:

4

Hey guys i cant get this trigger to work, ive worked on it for an hour or so and cant see to figure out where im going wrong, any help would be appreciated

CREATE OR REPLACE TRIGGER allergy

 BEFORE INSERT ON   

 DECLARE 
 med VARCHAR2(20);

 BEGIN

 SELECT  v.medication RCD.specify
 INTO  med
 FROM  visit v, relcondetails RCD
 WHERE :new.medication = v.medication AND RCD.specifiy = 'allergies';

 IF med = allergies THEN
  RAISE_APPLICATION_ERROR(-20000, 'Patient Is alergic to this medication');
 END IF;
END allergy;

When put into oracle

ERROR at line 6: ORA-04079: invalid trigger specification

+1  A: 
BEFORE INSERT ON <TABLE NAME>

and why select both v.medication and RCD.specify when you're only selecting into one variable?

Mark Baker
thats what i had originally but i kept getting ORA-04082: NEW or OLD references not allowed in table level triggersso i wanst too sure if it was correct.
SImon
You need to create it as a row trigger by adding `FOR EACH ROW`
Martin Smith
@Martin Well spotted
Mark Baker
A: 

You've probably seen this but: http://msdn.microsoft.com/en-us/library/aa258254(SQL.80).aspx

CREATE TRIGGER TriggerName
ON MyTableName
FOR MyEvent
AS
     -- My Trigger Logic
Felipe Fiali
He's on Oracle!
Martin Smith
correct Martin, the logic still helps however Thanks felipe!
SImon
@SImon The rest of the syntax is quite a lot different though. e.g. SQL Server doesn't have row and statement triggers as Oracle does.
Martin Smith
Gotta remember checking tags... glad it still helped!
Felipe Fiali
+2  A: 

In addition to Mark's point that you are missing the table name, and Martin's point that you want this to be a row-level trigger, your actual body won't compile, for a couple of reasons.

  • You look like you're trying to select two columns, but you don't have a comma between them, and you only have one local variable in the INTO clause
  • You use an identifier allergies which is not declared anywhere.

I also doubt that your query is logically correct, but of course I don't know the database design so I can't say for sure.

Dave Costa
All my triggers have DECLARE
Stephanie Page
He needs a `DECLARE` only because he's declaring a local variable.
Jeffrey Kemp
My apologies on the DECLARE point -- I was thinking a trigger would use the same syntax as a procedure or function. Removed it.
Dave Costa
+7  A: 
CREATE OR REPLACE TRIGGER allergy BEFORE INSERT ON   

name of table here

FOR EACH ROW -- forgot this too

DECLARE 
    med VARCHAR2(20);

You should really be declaring this as %type.

    med visit.medication%type;

 BEGIN

 SELECT  v.medication RCD.specify

Requires a comma between columns

    INTO  med

Two columns need two variables

    FROM  visit v, relcondetails RCD
    WHERE :new.medication = v.medication AND RCD.specifiy = 'allergies';

You have no join condition between your two tables, that's very bad. This query will perform a Cartesian between the two tables and then return all of them that have 'allergies' and :new.medication in their respective columns.

you also probably need a filter condition to limit the query to a particular patient or a particular visit. This query will do it for all patients and all their visits squared.

  IF med = allergies THEN

I don't know what /allergies/ is in this IF. There's no variable that's defined as that and without quotes it's not a string.

    RAISE_APPLICATION_ERROR(-20000, 'Patient Is alergic to this medication');

This error message reinforces what I said about your query. You think you're querying for a single patient but you're not.

  END IF;
END allergy;

Seriously, if you're writing software to save a person from getting potentially life threatening medication then please consider some other line of work. I swear I'm not saying this to be rude, but your code sample shows almost no understanding of the pl/sql language or sql or any scrap of programming background. I think you started with some sample code and tried to modify it into something something. But you're really left with gibberish. I'm starting to think this is homework.

Stephanie Page
I hope it's homework :)
Jeffrey Kemp