Solved

can we declare variables and use in trigger body?

Posted on 2003-10-28
6
1,806 Views
Last Modified: 2012-08-14
Hi Experts,

      Can someone tell me if we can declare variables like how we do in normal PL/SQL block and use it in trigger? My problem is i need to write an Before Update/Delete trigger on Table 1. In the trigger body, I need to insert data in to another table Table 2. Table 2 has a field called Action code, If its an Update then the action code should be set as 'U' , if its a delete then action code should be set as 'D'.

      I know i need to use the 'INSERTING' or 'UPDATING' clause to find out the DML operation. I just wanted to know if I can declare a variable say var1 and set it as 'U' or 'D' based on the DML.

      If I cant do the above, then I need to repeat the entire Insert statement with just the action code differing. Pls help.

0
Comment
Question by:sajidh_unisa
6 Comments
 
LVL 12

Expert Comment

by:catchmeifuwant
Comment Utility
Yes you can declare a variable and use it...heres an example..

CREATE OR REPLACE TRIGGER trg_test
BEFORE update or delete on emp
BEGIN
declare
v_test varchar2(1) ;
begin

if UPDATING then
v_test := 'U';
else
v_test := 'D';
end if;
insert into temp values(v_test);
end;
END;
/
0
 
LVL 8

Accepted Solution

by:
Danielzt earned 250 total points
Comment Utility


just like this:

CREATE OR REPLACE TRIGGER trigger1
BEFORE update or delete on temp

declare

-- put all variable definition here

v_test varchar2(1) ;

-- trigger body begins
begin

if UPDATING then
v_test := 'U';
else
v_test := 'D';
end if;
insert into temp values(v_test);

end;
0
 
LVL 4

Expert Comment

by:Hassan_Ghanem
Comment Utility
of course you can, because the only difference between triggers and procedures is that triggers fire automatically  before/after a DML operation but procedure should be called explicitly..
0
 
LVL 4

Expert Comment

by:Vinay_dba
Comment Utility
Yes you can declare a variable say var1 and set it as 'U' or 'D' based on the DML.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

How to Create User-Defined Aggregates in Oracle Before we begin creating these things, what are user-defined aggregates?  They are a feature introduced in Oracle 9i that allows a developer to create his or her own functions like "SUM", "AVG", and…
Have you ever had to make fundamental changes to a table in Oracle, but haven't been able to get any downtime?  I'm talking things like: * Dropping columns * Shrinking allocated space * Removing chained blocks and restoring the PCTFREE * Re-or…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now