Results 1 to 10 of 10

Thread: Oracle triggers problem

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Feb 2007
    Posts
    153

    Oracle triggers problem

    hi to all,
    i am creating the trigger for insert and update in oracle. the query is


    create trigger insertandupdate
    after update
    on testtable
    for each row
    begin
    insert into testtable1 values(:new.name,:new.age,:new.mailid,:new.datetime)

    end;



    the problem in that query is , when i am inserting one row , it is getting all the rows in the table including the newly inserted row. i want to get only the newly inserted row only not all the rows. how can i get the value. and how can i solve the problem. plz anybody know , help me.
    thank u

    with thanks and regards
    mmary

  2. #2
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Oracle triggers problem

    Try this:

    create trigger insertandupdate
    after update
    on testtable
    for each row
    begin
    Select name,age,mailid,datetime Into
    :new.NName,
    :new.NAge,
    :new.NMailid,
    :new.NDatetime
    From testtable
    Where PK = ld.PK
    insert into testtable1 values(:NName,NAge,:NMailid,:NDatetime);

    end;
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Feb 2007
    Posts
    153

    Re: Oracle triggers problem

    thank u very much for ur kind reply. but i am getting the following error



    ERROR at line 2: PL/SQL: SQL Statement ignored


    how can i solve the problem. plz help me


    thank u
    with thanks and regards
    mmary

  4. #4
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Oracle triggers problem

    What are you actually tring to do here? The code looks like on an Update of the tabe you are going to update the same table with the same data?
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Feb 2007
    Posts
    153

    Re: Oracle triggers problem

    thank u very much for ur help. i want to get the updated records from the testtable and i want to insert that updated records in the table1. and also i want to get the inserted records also. how can i do that. plz help me


    thank u
    with thanks and regards
    mmary

  6. #6
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Oracle triggers problem

    You need two triggers on on insert the other on Update.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Feb 2007
    Posts
    153

    Re: Oracle triggers problem

    thank u very for ur help. i need the two trigges , one for insert and another one for update.

    plz help me.
    thank u

    with thanks and regards
    mmary

  8. #8
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Oracle triggers problem

    Here is the trigger
    Code:
    create or replace trigger testtableinsertupdate 
    after insert or update on testtabe
    referencing New as n
    For Each Row
    Begin
      Insert Into Table1 (Name,Age,MailID,DateTime) Values (
          :n.Name,
          :n.Age,
          :n.MailID,
          :n.DateTime);
    End;
    I have tested this as follows:

    1 created two tables:
    Code:
      Create Table Testtable (
          Name varchar2(20),
          Age number,
          MailID varchar2(20),
          DateTime Date);
      Create Table Table1 (
          Name varchar2(20),
          Age number,
          MailID varchar2(20),
          DateTime Date);
    2. Insert a row into testtable
    Code:
    Insert Into TestTable Values ('Gary',10,'gary','20-May-07');
    3. Checked the data in table Table1 and saw the entered new row.
    4. Inserted a new row into testtable
    Code:
    Insert Into TestTable Values ('Sam',11,'sam','21-May-07');
    5. Check data in Table1
    Saw two rows Gay and Sam
    6. Updated a row in testtable
    Code:
    Update TestTable Set age = 50 Where Name = 'Gary";
    7. Checked data in Table1
    Saw Three rows 'Gay' at age 10, Sam and Gary at age 50
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Feb 2007
    Posts
    153

    Resolved Re: Oracle triggers problem

    thank u. thank u very much .now its working fine. thank very much.


    with thanks and regards
    mmary

  10. #10
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Oracle triggers problem

    If this answers the question could you mark the thread as resolved.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width