Selective use of a delete trigger

Is there a way for me to use a delete trigger selectively?
I have a table that on certain instances, when a record is deleted the 'deleted' record needs to be inserted into an archive table. On other times, the delete must just delete and no archive created.
because I have a DELETE trigger, it is firing everytime a delete happens.
Is there a way I can 'suspend' the trigger for those times?
for example:
DELETE /*+ No trg_del_price */ from PRICE where <where clause here>

We have a price table that in some situations, multiple prices come in for the same account. We have an SP that does a clean-up routine and if it finds records that needs to be cleaned up, it updates those and then deletes the other records (as they are now duplcate). In these cases, I don't want the 'extra' records inserted into the archive table.
(Note that this was not my code - but the original code written by a former DBA that has since left. It would be too complicated and time-consuming to rewrite the whole logic to not have this clean-up routine in the first place).
If the record is truely deleted in all other cases, then I do want the record inserted. For that I call a trigger that calls a SP to do the insert.
If I cannot do a selective trigger, can I put logic in the trigger that can check the source of the trigger - if from this clean-up SP, then don't fire....
Sean

Similar Messages

  • :NEW cannot be used in After Delete Trigger ?

    Hi,
    Is there any way to get the :NW.value in the After delete trigger for each row. My requirement is audit log of the end user DML operations along with user Name (HERE THE USER IS NOT THE ORACLE USER, BECAUSE OF THE LARGE NUMBER OF END USERS WE ARE MAINTAINING ONE TABLE TO CREATE USER NAME & PASSWORD, WHEN THE USER LOGIN TO ORACLE FORM SCREEN, ASSIGN THE USER NAME TO GLOBAL VARIABLE) & Action Date.
    Here is my code for trigger - It is working fine with INSER & UPDATE but for DELETE User is NULL
    CREATE OR REPLACE TRIGGER Tgr_stud_det
    AFTER INSERT OR UPDATE OR DELETE ON student_details
    FOR EACH ROW
    DECLARE
    BEGIN
    IF Inserting THEN
    -------------INSERT VALUE---------------
    INSERT INTO Log_student_details
    (Seq,
    App_User,
    Action,
    Action_Date,
    stud_name,
    stud_age,
    stud_sex)
    VALUES
    (stud_sequence.NEXTVAL,
    :NEW.App_User,
    'INSERT',
    SYSDATE,
    :NEW.stud_name,
    :NEW.stud_age,
    :NEW.stud_sex);
    -------------DELETE VALUE---------------
    ELSIF Deleting THEN
    INSERT INTO Log_student_details
    (Seq,
    App_User,
    Action,
    Action_Date,
    Comment_Up,
    stud_name,
    stud_age,
    stud_sex)
    VALUES
    (stud_sequence.NEXTVAL,
    :OLD.App_User,
    'DELETE',
    SYSDATE,
    NULL,
    :OLD.stud_name,
    :OLD.stud_age,
    :OLD.stud_sex);
    ELSIF Updating THEN
    -------------UPDATE VALUE---------------
    INSERT INTO Log_student_details
    (Seq,
    App_User,
    Action,
    Action_Date,
    Comment_Up,
    stud_name,
    stud_age,
    stud_sex)
    VALUES
    (stud_sequence.NEXTVAL,
    :NEW.App_User,
    'UPDATE',
    SYSDATE,
    'NEW VALUE',
    :NEW.stud_name,
    :NEW.stud_age,
    :NEW.stud_sex);
    INSERT INTO Log_student_details
    (Seq,
    App_User,
    Action,
    Action_Date,
    Comment_Up,
    stud_name,
    stud_age,
    stud_sex)
    VALUES
    (stud_sequence.CURRVAL,
    :NEW.App_User,
    'UPDATE',
    SYSDATE,
    'OLD VALUE',
    :OLD.stud_name,
    :OLD.stud_age,
    :OLD.stud_sex);
    END IF;
    EXCEPTION
    WHEN OTHERS THEN
    NULL;
    END Tgr_stud_det;
    Thanks in advance.

    Rizly,
    As i mentioned in the above post, you should remove the references of :old and :new when you are trying to use the global variables. These values are only significant when you the talk about the record in the table.
    For the scenario, you explained, your trigger would insert two records....The trigger would be fired twice.. once during the insert and once during the delete. The audit table will have two records indicating both the actions..
    Take a look at this example below...I am artificially manufacturing a user id in the package test_pkg and using that in the insert trigger. As i explained above, you dont need the :old and :new references because the user id is not a column in the table . hence the :old and :new references have no relevance.
    Also note that, for the delete, I use the :old value and for the insert, I use the :new value.
    for update, I assume you want to store the old record and hence used :old (you can of course use :new too..technically.).
    I don't have access to a forms environement, but the user id logic should be similar to what I described below.
    sql> create table t(
      2     id number,
      3     name varchar2(20)
      4  );
    Table created.
    sql> create table t_audit
      2     ( id number,
      3       name varchar2(20),
      4       action varchar2(20),
      5       user_id varchar2(20)
      6  );
    Table created.
    sql> create or replace package test_pkg as
      2      function get_user_id return varchar2;
      3  end test_pkg;
      4  /
    Package created.
    sql> create or replace package body test_pkg as
      2      function get_user_id return varchar2 is
      3      begin
      4          return 'USER' || to_char(sysdate,'HH24:MI');
      5      end get_user_id;
      6  end test_pkg;
      7  /
    Package body created.
      1  create or replace trigger trg_biud_t
      2     before insert or update or delete on t
      3     for each row
      4  begin
      5     if INSERTING then
      6        insert into t_audit values (:new.id, :new.name, 'INSERT',test_pkg.get_user_i
      7     elsif UPDATING then
      8        insert into t_audit values (:old.id, :old.name, 'UPDATE',test_pkg.get_user_i
      9     elsif DELETING then
    10        insert into t_audit values (:old.id, :old.name, 'DELETE',test_pkg.get_user_i
    11     end if;
    12* end;
    sql> /
    Trigger created.
    sql> select * from t;
    no rows selected
    sql> select * from t_audit;
    no rows selected
    sql> insert into t values (100, 'Rajesh');
    1 row created.
    sql> insert into t values (200,'Kumar');
    1 row created.
    sql> delete from t where id = 200;
    1 row deleted.
    sql> commit;
    Commit complete.
    sql> select * from t
      2  /
            ID NAME
           100 Rajesh
    sql> select * from t_audit;
            ID NAME                 ACTION               USER_ID
           100 Rajesh               INSERT               USER15:36
           200 Kumar                INSERT               USER15:36
           200 Kumar                DELETE               USER15:37

  • I am trying to delete pages from a PDF file. I opened the bookmarks, selected the pages to delete and choose Edit Delete. The selected pages are not deleted. Note: I have to open the file using a passport provided by an external party.

    I am trying to delete pages from a PDF file. I opened the bookmarks in the PDF file, selected the pages to delete and choose Edit > Delete. The selected pages are not deleted. Note: I have to open the file using a passport provided by an external party.

    Resolved

  • Using a link to trigger a delete action on form page?

    Hello,
    I have been unable to find an explanation about how to do this in the documentation, these forums or the old interakt forums.
    Is it possible to use a link to trigger the delete action on a form page?
    If so how is it done?
    Thanks in advance!

    Sorry Purple.. I know that there is a delete link that triggers the delete transaction on the form page..
    The question was if anybody knew if I am able to use a link to trigger that delete transaction (on the form page), much like a dynamic list has a link that will trigger the delete transaction on the form page.
    I don't want to use a dynamic list in this case though. :)
    Thanks anyways,
    Drew

  • Have a question I do not use mail in my computer I use yahoo and I have all these emails and selected all and then delete then delete trash and they always come back how do I stop these old email in apple mail? I never send anything from there but I have

    have a question I do not use mail in my computer I use yahoo and I have all these emails and selected all and then delete then delete trash and they always come back how do I stop these old email in apple mail? I never send anything from there but I have the same email in yahoo but delete them and they still show up in apple mail from day one how do I get rid of these once and for all.Please help
    <Edited by Host>

    I am soooooooooooo confused now.  Ok, if you are having problems with Yahoo Mail and Gmail but you do not use Apple's Mail you need to do the following:
    1 - Post in Yahoo's Mail forums and/or contact their tech support.
    2 - Post in Gmails forums and/or contact their tech support.
    Neither of the above are Apple's products.

  • How to select a record and delete using sereen painter in alv

    Hi experts i have displayed a table in selection sereen
    in alv by using table control(sereen painter).
    My request is to display the database table records
    when i press F8.
    I also have two buttons one is 'DELETE'
    and another one is 'INSERT'.
    My request is i have to select a particular record in
    the output and if i press the 'delete' button means
    then the selected record should be deleted in that table
    and also in database table.
    Like if i insert a new record means then the record
    should be inserted in that table and database table...
    Kindly plz send the coding immediately............

    Hi,
    create a module in ur  PROCESS AFTER INPUT fro eg
    MODULE select.
    select * from db into table itab.
    module delete.
    db-field = wa-field(this is the field u r going to delete in ur  table control and in database table).
    append wa to itab.
    delete <db table> from itab.
    module insert.
    db-field = wa-field(this is the field u r going to insert in ur table control and database table )
    append wa to itab.
    insert <db table> from itab.
    regards,
    dhaya..

  • I'm using an iPhone 4S on IOS 7 and I am unable to select a message to delete can anyone help?

    Even when I try to select multiple messages to delete under the edit function the selected messages unselect themselves.   How can I resolve this issue?
    Many thanks

    I have the same issues... It also occures with my emails. This all started 2 days ago. Another thing I noted was that when tring to "copy" something from a message, it will show you the "copy" option but when you click it, it crashes the messaging application... I called Verizon support and they couldnt understand what I was talking about.

  • How to select all columns in a trigger?

    I add a "before delete" trigger on a table, in order to insert the records to a backup table before they are deleted.
    but I cannot write like this : insert into t_backup select * from :old;
    The table has more than 30 columns so I don't want to select them one by one, how can I select all columns?

    Yes, it is possible by writing something like this :
    where col1 = :old.col1; But it is not directly supported. First, we need a package to remember all the OLD records:.... and please see below link for complete code in action :
    http://www.dbforums.com/oracle/925729-trigger-back-up-data.html
    What I am doing here, just copying the code and replacing "emp" with "test" (The table name on which I am going to apply this using notepad find and replace):
    SQL> select * from test_backup;
    no rows selected
    SQL> select * from test;
             A
             1
             2
             4
             5
    SQL> select * from test_backup;
    no rows selected
    SQL>
    create or replace package test_trg_pkg as
      type test_type is table of test%ROWTYPE index by binary_integer;
      test_tab test_type;
    end;
    create or replace trigger test_bds before delete on test
    begin
      test_trg_pkg.test_tab.delete;
    end;
    create or replace trigger test_adr after delete on test
    for each row
    declare
      -- To allow us to select the old values
      pragma autonomous_transaction;
    begin
      select *
      into   test_trg_pkg.test_tab(test_trg_pkg.test_tab.COUNT+1)
      from   test
      where  a = :old.a;  <----- Here you have to give your column name.
    end;
    create or replace trigger test_ads after delete on test
    begin
      for i in 1..test_trg_pkg.test_tab.COUNT loop
        insert into test_backup values test_trg_pkg.test_tab(i);
      end loop;
    end;
    SQL> delete from test where a=1;
    1 row deleted.
    SQL> select * from test_backup;
             A
             1
    SQL> delete from test where a=2;
    1 row deleted.
    SQL> select * from test_backup;
             A
             1
             2
    SQL>Regards
    Girish Sharma

  • Get Current SQL in Before delete trigger

    Hi,
    I have created a before delete trigger to track which records are deleted from a table I need to know what statements fires the trigger. can someone please describe how I can retrieve the current SQL.
    using sys_context('userenv','CURRENT_SQL') returns null for CURRENT SQL
    Note:
    For me the easier is to enable auditing and audit delete on the table however at the moment I cant get a downtime from the business to bounce the database to enable auditing.
    CREATE OR REPLACE TRIGGER before_delete BEFORE DELETE
    ON AUDIT_TEST_TAB
    FOR EACH ROW
    DECLARE
    v_username varchar2(50);
    v_stmt varchar2(255);
    v_client_info varchar2(200);
    v_os_user varchar2(50);
    v_machine varchar2(50);
    v_program varchar2(50);
    v_module varchar2(50);
    v_auth_type varchar2(200);
    v_ip_addr varchar2(200);
    v_sql_statement VARCHAR2(4000);
    BEGIN
    SELECT sys_context ('USERENV', 'CURRENT_SQL')
    INTO v_sql_statement
    FROM dual;
    -----------insert into logging table statment ----
    end;

    A few comments.
    Lets assume you run a delete statement that deletes 550 rows from your table. If your trigger is working then the very same statement would be stored 550 times. I think an BEFORE or better an AFTER delete STATEMENT level trigger would be the better choice. Why AFTER? Because if the delete operation fails, for example because of existing child records, then everything is rolled back anyway to the implicit savepoint just before the delete.
    So to store the SQL statement the correct trigger would be an AFTER STATEMENT DELETE trigger.
    Now what to store: You want the sql statement. You could try to find the cursor/currently running SQL. It might be tricky to separate that from the SQLs that you run to find this out.
    It could even be possible to simply save all commands that are in your PGA/Cached cursor area. First find out yur session, then store the SQL_text (first 60 chars) for all the cursors in this session by using v$open_cursor or the first 1000 chars by using v$sql.
    Here are a few views that might be helpful. v$session , v$open_cursor, v$sql, v$sqltext, v$sql_bind_data, v$sql_bind_capture, dba_hist_sqltext

  • Instead of delete trigger rows deleted

    Hi All.
    Im using a few instead of triggers over a view. The view selects data from other tables and merges them together. This view essentially forms the parent of a few one2one relationships with other tables.
    However my application doesnt know its a view (im using hibernate orm framework) - so when I delete this object which is mapped to a view, I get an error saying that no records where deleted. Also, if i test in sqlplus, i get "0 rows deleted" in response to a delete - which of course is true, but i want it to say 1 or some number of my choice.
    How can i modify my "instead of delete" trigger so that the number for records deleted count returned to caller is 1, or some number i can define?
    Here is my current trigger...
    create or replace
    trigger product_view_delete_trigger
    instead of delete
    on product_view1
    for each row
    begin
    null;
    end;
    Thanks.

    In regards to why Im using a view - its discussed here...
    Crazy Union across 3 tables
    The view im using is not updatable - it uses a union. I use "instead of triggers" to trick my application into thinking its a real table which is insertable. And this works fine.
    Im having trouble tricking my application into think that the delete was successful, because the "instead of delete" trigger doesnt actually delete anything - which is what i wnat - but i want it to report back to the caller that a delete occurred.
    I want the SQL%ROWCOUNT to be set to 1 or whatever. Perhaps this is not hte variable, but im hoping there is some variable I can set which is used to report back to the client as to how many records were deleted.
    thx.

  • Using xmlgen in a trigger -HELP

    We are trying to use xmlgen in a trigger, actually a insteadOf on a view but that is really secondary.
    The problem we have is that xmlgen doesnt seem to work from a trigger or any other background process. There is no reall erro messgae it just fails.
    Has anyone used xmlgen from a trgigger??
    Rob

    Can't this be done.....
    create or replace trigger audit_EMP_DTL
    before update or delete or insert
    on EMP_DTL
    for each row
    declare
    pragma autonomous_transaction;
    v_blockexists number := 0;
    no_lock exception;
    ReturnExp EXCEPTION;
    begin
    select     count(a.sid) into v_blockexists
    from     v$session a
    where     a.sid=(select     tab1.sid
              from     v$lock tab1, v$lock tab2
              where     tab1.block =1
              and     tab2.request > 0
              and     tab1.id1=tab2.id1
              and     tab1.id2=tab2.id2
    if v_blockexists = 0 then
    insert into audit_blocks(program_name) values ('No block yet');
    commit;
    RAISE ReturnExp;
    end if;
    EXCEPTION
    WHEN ReturnExp THEN
    NULL;
    END;

  • Using user_tab_columns on the trigger

    dear all
    i need to use user_tab_columns on the trigger before delete
    like ex
    SELECT a.column_name
    INTO l_col_name
    FROM user_tab_columns a
    WHERE a.table_name = 'EMPLOYEE' AND a.nullable = 'N';
    this is not work because the query not retrieve column name but emp_no is not null , if any suggestion to solve this issue?

    i need to use user_tab_columns on the trigger before
    delete And what business case you are trying to solve?
    Gints Plivna
    http://www.gplivna.eu

  • Delete Trigger

    Hi,
    I got a question using triggers. I have a statement level delete trigger that calls a package function used to insert data into various archiving tables. In order to keep track of the deleted rows I have a row level delete trigger that keeps the deleted Ids of my objects in a special table that I later query in my statement level trigger.
    My main problem is that while all triggers are called properly and data is removed from desired tables, the data that should be inserted into my archiving tables does not appear until the trigger is fired again it appears. I don't really see what could cause data to be deleted and effectively removed while the data i want to insert does not appear until the next time the trigger is fired.
    could it be that i need to explicitely commit the inserted data ?
    If it's not clear, I could always post some of the code used.
    Thanks,
    Greg

    Sure (I hope it won't be too messy though).
    ---- PACKAGE DEF.
    I have a package variable defined as follows:
    TYPE idTable IS TABLE OF NUMBER(19) INDEX BY BINARY_INTEGER;
    And my variables:
    del_oldRows idTable;
    del_emptyRows idTable;
    ---- PACKAGE BODY
    In the body I have calls like this:
    PROCEDURE delete_transaction(aID NUMBER) IS
    BEGIN
    -- I) Add New Data into appropriate archiving tables
    -- 1) Many-to-Many Table for first object
    INSERT ALL INTO X_OBJ1_OBJ2_DLTD
    DLTD_ID,
    OBJ1_ID,
    OBJ2_ID
    SELECT
    ID,
    OBJ1_ID,
    OBJ2_ID
    FROM X_OBJ1_OBJ2
    WHERE (X_OBJ1_OBJ2.ID = aID )
    -- 2) Second object <SNIP>
    -- 1) Many-to-Many Table for first object
    DELETE FROM X_OBJ1_OBJ2
    WHERE (X_OBJ1_OBJ2.ID = aID );
    END delete_transaction;
    ---- Before Delete Trigger (Statement Level)
    This one resets the package in a consistent state:
    BEGIN
    mypackage.del_oldRows := mypackage.del_gatetrans_emptyRows;
    END;
    ---- After Delete Trigger (Row Level)
    This one adds Ids in my package variable:
    BEGIN
    mypackage.del_oldRows(mypackage.del_oldRows.COUNT + 1) := :OLD.ID;
    END;
    ---- After Delete Trigger (Statement)
    This one goes through each row in my package "table" and calls the function to add the related data in my archiving table:
    BEGIN
    FOR i IN 1 .. mypackage.del_oldRows.COUNT LOOP
    mypackage.delete_transaction(mypackage.del_oldRows(i));
    END LOOP;
    mypackage.del_oldRows := mypackage.del_emptyRows;
    END;
    ---- END
    Anyways that's the basic logic. Kinda what is suggested for mutating tables when playing with UPDATE triggers I guess. Here it's just because I want my triggers to be simple and focus on inserting the archiving data for the object triggering the... trigger while related objects that need to be archived should be taken care of in my package function. My problem is that the data is removed properly but not appears as inserted until the next time the trigger is fired, as if i was always "one" trigger late :(.
    If it's not clear enough, please let me know.
    Thanks,
    Greg

  • Bulk insert using stored procedure or trigger

    Hi ,
    I have to insert around 40,000 rows in a table querying other database using database link.
    Please advice whether I do using stored procedure or trigger.
    Thanks.

    Here is a basic benchmark that illustrates the difference between maximising SQL, or doing it in PL/SQL instead.
    Care needs to be taken with such a benchmark in order for physical I/O not to negatively impact a test, and then have no impact in the second test as that data read from disk now sits in the cache.
    So I ran it a couple of times in order to "warm up" the cache. I also put the maximise-SQL test first in order to show that it is still faster, despite any physical I/O it may do (which will likely be faster logical I/O with the second test).
    Run on Oracle XE 10.2.0.1.
    SQL> drop table my_objects_copy purge;
    Table dropped.
    SQL> create table my_objects_copy as select * from all_objects;
    Table created.
    SQL>
    SQL> set timing on
    SQL> begin
    2 delete from my_objects_copy;
    3
    4 insert into my_objects_copy
    5 select * from all_objects;
    6
    7 commit;
    8 end;
    9 /
    PL/SQL procedure successfully completed.
    Elapsed: 00:00:01.11
    SQL> set timing off
    SQL>
    SQL> drop table my_objects_copy purge;
    Table dropped.
    SQL> create table my_objects_copy as select * from all_objects;
    Table created.
    SQL>
    SQL> set timing on
    SQL> declare
    2 cursor c is select * from all_objects;
    3 objRow ALL_OBJECTS%ROWTYPE;
    4 begin
    5 delete from my_objects_copy;
    6
    7 open c;
    8 loop
    9 fetch c into objRow;
    10 exit when c%NOTFOUND;
    11
    12 insert into my_objects_copy
    13 values objRow;
    14
    15 end loop;
    16 commit;
    17 end;
    18 /
    PL/SQL procedure successfully completed.
    Elapsed: 00:00:03.21
    SQL>
    The cursor-fetch-loop is almost 3 times slower. The more rows there are to process, the slower the cursor-fetch-loop will become, as it will create more and more context switching and copying data between the SQL and PL engines and back.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

  • On 10.4.11 Mac Mail I get this: Mail cannot update your mailboxes because your home directory is full. You must free up space in your home folder before using Mail. Delete unnedded documents or move documents to another volume. I can't open mail.

    On 10.4.11 iMac Mac Mail I get this message: "Mail cannot update your mailboxes because your home directory is full. You must free up space in your home folder before using Mail. Delete unneeded documents or move documents to another volume." I can't open mail to do this. I have reinstalled software but no effect. How do I get into Mail to delete?

    Found this on the "more like this" Worked like a charm!
    With the Mail.app quit and using the Finder, go to Home > Library > Mail. Copy the Mail folder and place the copy on the Desktop for backup purposes.
    Go to Home > Library > Mail > Envelope Index. Move the Envelope Index file to the Desktop.
    Launch Mail and you will be prompted to import mailboxes. Select OK and allow the import process to complete.
    After confirming all mailboxes were successfully imported and available, you can delete the copy of the Mail folder and old Envelope Index file from the Desktop and this should resolve the problem.

Maybe you are looking for

  • Error while extracting data from ODS using Infospokes - URGENT

    Hi all, I am extracting data from an ODS using infospokes and nothing fancy, no selection criteria but I am getting an error message saying Syntax error in row3(-> long text) Msg id = "RG" and msgno is 102 Any ideas on how I can see what is the exact

  • Question on db_unique_name in init.ora for Data Guard

    I need to set up only one physical standby on a different box (at a different location) for the primary db in production. OS: Sun Sparc Solaris 10 Oracle: 10.2.0.3 Can I use the same db_unique_name in init.ora for both primary and standby DBs? What a

  • XML mapping inheritance problem; missing class indicator field

    Hi! I am currently working on a project which involves mapping a large domain model on a XSD schema. For this we use Toplink 10.1.3.1 which is mostly great. But now I have a problem while wanting to use class inheritance. In my XSD I have the followi

  • Chart Control Error

    Dear all I have connected chart control with SQL database but while loading the chart it displays following error please help me in finding  a solution for the error. Thanks in advance                                         Conversion from string "I

  • Oracle 10.2.0.3 End of Life

    Hi guys, When is the eol for 10.2.0.3? Where do I find such information?