Solved

can we declare variables and use in trigger body?

Posted on 2003-10-28
6
1,811 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 12

Expert Comment

by:catchmeifuwant
ID: 9632488
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
ID: 9633708


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
ID: 9633730
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
ID: 9635112
Yes you can declare a variable say var1 and set it as 'U' or 'D' based on the DML.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and the…
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

696 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