+ Reply to Thread + Post New Thread
Results 1 to 3 of 3
  1. #1
    sunsail's Avatar
    sunsail is offline Junior Member
    Join Date
    04 Dec 2008
    Posts
    1
    Say Thanks
    0
    Thanked 0 Times in 0 Posts
    Documents
    0
    Uploads
    0

    Default oracle trigger problem

    Hi,
    I am just trying to create a trigger to make a small test.however I cannot make it what is missing? I put the sql below
    Thanks
    Code :
     create table myaudit(
           id VARCHAR2(4 BYTE)         NOT NULL,
           old_value VARCHAR2(40 BYTE),
           new_value VARCHAR2(40 BYTE)
        );

    create table Employee(
          ID                 VARCHAR2(4 BYTE)         NOT NULL,
          First_Name         VARCHAR2(10 BYTE),
          Last_Name          VARCHAR2(10 BYTE),
          Start_Date         DATE,
          End_Date           DATE,
          Salary             Number(8,2),
          City               VARCHAR2(10 BYTE),
          Description        VARCHAR2(15 BYTE)
       );


    CREATE OR REPLACE TRIGGER before_employee_salary_update
    BEFORE UPDATE OF salary
    ON employee
    FOR EACH ROW WHEN (new.salary < old.salary )
     BEGIN
       --dbms_output.put_line('id = ' || : old.id);
        --dbms_output.put_line('Old salary = ' || : old.salary);
       --dbms_output.put_line('New salary = ' || :new.salary);

      INSERT INTO myaudit (
        id, old_value, new_value
      ) VALUES (
       : old.id, : old.salary, :new.salary
      );
      END before_employee_salary_update;


     
    update employee set salary = 4
    where id=02;

    select * from Employee;

    select * from myaudit;

  2.    Club-Oracle Complementary E-Books and Magazines
    Get your free Complementary Copy of Oracle Magazine

    You can also browse the Free Magazines and E-Books section to see the complete list of free magazines, e-books and Whitepapers.

  3. #2
    rajavu's Avatar
    rajavu is offline Forum Genius
    Join Date
    13 Oct 2008
    Location
    @ Bangalore , India
    Posts
    471
    Say Thanks
    0
    Thanked 20 Times in 19 Posts
    Documents
    0
    Uploads
    0

    Default Re: oracle trigger problem

    You didn't mention what is wrong with the trigger.

    What were the previous values in Employee ?
    What were the values after updation in Myaudit ?

    Trigger looks fine .And I guess you committed the update transaction in Employee to reflect the changes in Myaudit . Please show us the test case to piint out where exactly went wrong .

    Raj.

  4. #3
    tyro's Avatar
    tyro is offline Forum Genius
    Join Date
    20 Aug 2008
    Location
    India
    Posts
    363
    Say Thanks
    0
    Thanked 17 Times in 17 Posts
    Documents
    0
    Uploads
    0

    Default Re: oracle trigger problem

    Trigger is absolutely fine. Have a look

    Code :
    SQL> create table myaudit(
      2         id VARCHAR2(4 BYTE)         NOT NULL,
      3         old_value VARCHAR2(40 BYTE),
      4         new_value VARCHAR2(40 BYTE)
      5      );

    Table created.

    SQL> create table Employee(
      2        ID                 VARCHAR2(4 BYTE)         NOT NULL,
      3        First_Name         VARCHAR2(10 BYTE),
      4        Last_Name          VARCHAR2(10 BYTE),
      5        Start_Date         DATE,
      6        End_Date           DATE,
      7        Salary             Number(8,2),
      8        City               VARCHAR2(10 BYTE),
      9        Description        VARCHAR2(15 BYTE)
     10     );

    Table created.

    SQL> CREATE OR REPLACE TRIGGER before_employee_salary_update
      2  BEFORE UPDATE OF salary
      3  ON employee
      4  FOR EACH ROW WHEN (new.salary < old.salary )
      5   BEGIN
      6     --dbms_output.put_line('id = ' || : old.id);
      7      --dbms_output.put_line('Old salary = ' || : old.salary);
      8     --dbms_output.put_line('New salary = ' || :new.salary);
      9  
     10    INSERT INTO myaudit (
     11      id, old_value, new_value
     12    ) VALUES (
     13     :old.id, :old.salary, :new.salary
     14    );
     15    END before_employee_salary_update;
     16  /

    Trigger created.

    SQL> INSERT INTO Employee (ID,First_Name,Salary) VALUES ('1','TYRO',10000);

    1 row created.

    SQL> update employee set salary = 5000
      2  where id=1;

    1 row updated.


    SQL> select * from myaudit;

    ID   OLD_VALUE
    ---- ----------------------------------------
    NEW_VALUE
    ----------------------------------------
    1    20000
    5000

    SQL>

Similar Threads

  1. Problem with TOAD on oracle database 10.2.0.4.0
    By assmb01 in forum Other Development Tools
    Replies: 9
    Last Post: 03-24-2009, 12:44 AM
  2. oracle after instert trigger mutation problem
    By ora_student in forum SQL PL/SQL
    Replies: 3
    Last Post: 11-05-2008, 04:15 PM

Tags for this Thread