Oracle FAQ Your Portal to the Oracle Knowledge Grid
HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US
 

Home -> Community -> Usenet -> c.d.o.misc -> Re: Problem with Trigger

Re: Problem with Trigger

From: Eugene Brodskiy <eug_at_svt.unity.net>
Date: Mon, 18 Dec 2000 12:59:27 +0200
Message-ID: <3a3deaf9.0@gw.svt.unity.net>

Somebody named "Sebastian Scholz" <sebastian_scholz_at_tuwien.ac.at> wrote in news:91j8ri$4fe$1_at_news.tuwien.ac.at...
> Hello,
>
> I'm new to Oracle and need some help with a trigger. I need one which
 places
> the first two character of one field of a Table into a seperate one.
>
> Columns of the Table: (called CustomerData.CTBR)
>
> Code (6 Character)
> FirstTwoCharacter (2 Character)
> CTBROI (Primary Key, Number)
>
> Whenever I enter some data into the Field "Code", the trigger should fill
 in
> the first two Characters of this Code in the Field "FirstTwoCharacter".
> I did it with this Trigger:
>
> CREATE OR REPLACE TRIGGER "CUSTOMERDATA"."SETTWO"
> AFTER INSERT OR UPDATE OF "CODE" ON "CUSTOMERDATA"."CTBR"
> BEGIN
> UPDATE CUSTOMERDATA.CTBR SET FirstTwoCharacter=LEFT(CODE, 2) WHERE
> CTBROI = :new.CTBROI
> END;
>
> The Problem is, when I insert data with SQLPlus, it generates an error:
> INSERT INTO CUSTOMERDATA.CTBR (CODE, CTBROI) VALUES (998877, 9);
> ORA-04098: Trigger 'SETTWO' is invalid.
>
> Why can't I use the row :new directly? (just say, :new.FirstTwoCharacter =
> LEFT(:new.Code, 2) )
>
> Thanks in Advance,
> Sebastian
>

1st. You can use "new" and "old" _only_ for "For Each row" trigger 2nd You can't use "update" for the table in its trigger. 3rd. AFAIK, you can use "left" only for columns in SQL*Plus. Use Substr() here
So, IMHO, you need

 CREATE OR REPLACE TRIGGER "CUSTOMERDATA"."SETTWO"  AFTER INSERT OR UPDATE OF "CODE" ON "CUSTOMERDATA"."CTBR" FOR EACH ROW
BEGIN
  :new.FirstTwoCharacter=Substr(:new.CODE, 1,2) ;  END; BR,
  Eugene Received on Mon Dec 18 2000 - 04:59:27 CST

Original text of this message

HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US