Home » SQL & PL/SQL » SQL & PL/SQL » Reg: Update Triggers........
Reg: Update Triggers........ [message #8554] Fri, 29 August 2003 10:12 Go to next message
BABU SRSB
Messages: 42
Registered: June 2002
Member
Hi All,

I am new to Triggers.
I need to fire a Triggers after update the specific field(which should not be NULL) in table1 then the information in table2 should be update. both table have one foreign key relation.

I am giving the sample data here.

Table 1:-
---------------
col1 col2
-----------
1 230
2
3 400
4 300
5 700

Table 2:-
col1 col3
-----------
1 2
2
3 4
4 3
5 7

If i am update in col2 field in Table1 then the corresponding information in Table2 of Col3 should update to spefied value.
If the col2 in Table1 is update with NULL no need to update the col3 in Table2.

Can any one please help on code.

awaiting ur valuble response.

Thanks & Regards,

Babu.
Re: Reg: Update Triggers........ [message #8555 is a reply to message #8554] Fri, 29 August 2003 10:32 Go to previous messageGo to next message
Art Metzer
Messages: 2480
Registered: December 2002
Senior Member
Babu,

This example may not exactly meet your requirements, but it should at least get you started.
SQL> CREATE TABLE t1 (col1 NUMBER, col2 NUMBER);
SQL> INSERT INTO t1 VALUES (1,230);
SQL> INSERT INTO t1 VALUES (2,TO_NUMBER(NULL));
SQL> INSERT INTO t1 VALUES (3,400);
SQL> INSERT INTO t1 VALUES (4,300);
SQL> INSERT INTO t1 VALUES (5,700);
  
SQL> CREATE TABLE t2 (col1 NUMBER, col3 NUMBER);
SQL> INSERT INTO t2 VALUES (1,2);
SQL> INSERT INTO t2 VALUES (2,TO_NUMBER(NULL));
SQL> INSERT INTO t2 VALUES (3,4);
SQL> INSERT INTO t2 VALUES (4,3);
SQL> INSERT INTO t2 VALUES (5,7);
SQL> COMMIT;
   
SQL> CREATE OR REPLACE TRIGGER babu_fk_trigger
  2      AFTER UPDATE ON scott.t1
  3      FOR EACH ROW
  4  BEGIN
  5      IF :new.col2 IS NOT NULL
  6      AND (:old.col2 IS NULL OR :new.col2 != :old.col2) THEN
  7          UPDATE t2
  8          SET    t2.col3 = :new.col2
  9          WHERE  t2.col1 = :new.col1;
 10      END IF;
 11  END;
 12  /
  
Trigger created.
  
SQL> UPDATE t1 SET col2 = 999 WHERE col1 = 2;
  
1 row updated.
  
SQL> COMMIT;
  
Commit complete.
  
SQL> SELECT * FROM t2;
  
      COL1       COL3
---------- ----------
         1          2
         2        999
         3          4
         4          3
         5          7
  
SQL> 
From the Application Developer's Guide - Fundamentals, here's the documentation on creating triggers. (Requires you to sign up for Oracle Technical Network, which is not only free, but also spam-free.)

HTH, Babu.

A.
Re: Reg: Update Triggers........ [message #8556 is a reply to message #8555] Fri, 29 August 2003 10:45 Go to previous message
BABU SRSB
Messages: 42
Registered: June 2002
Member
Thanks Art for Quick Reply.....
Previous Topic: Loading data from flat file into table without SQL Loader
Next Topic: best way to....
Goto Forum:
  


Current Time: Fri Apr 26 17:30:45 CDT 2024