Help on delete trigger

Hi all,
I have a situation like this
I have a table like
SQL> desc system_user
Name Null? Type
ID NOT NULL NUMBER(12)
USER_ID NOT NULL VARCHAR2(30)
SQL> SELECT * FROM SYSTEM_USER
2 ORDER BY CTUT_ID;
CTUT_ID CURRENT_USER_ID
1 KTYLOR
2 EXAMPLE
3 SCOTT
SQL> DESC ALL_USERS;
Name Null? Type
USERNAME NOT NULL VARCHAR2(30)
USER_ID NOT NULL NUMBER
CREATED NOT NULL DATE
What i need to do is i need to have before delete trigger to check the following condition when user try to delete a record in table
SYSTEM_USER
1) suppose user is trying to delete a record "SCOTT" IN TABLE
SYSTEM_USER
CONDITION SHOULD CHECK THAT IF USER_NAME EXITS IN TABLE "ALL_USER"
1)IF HE EXITS THEN RAISE ERROR
2)IF HE IS NOT EXITS THEN DELETE THE RECORD IN TABLE "SYSTEM_USER"
SO I WROTE A CODE FOR TRIGGER
SQL> CREATE OR REPLACE TRIGGER TIBD_USER
2 BEFORE DELETE ON SYSTEM_USER FOR EACH ROW
3 DECLARE
4 FOUND_USER NUMBER(2);
5 BEGIN
6 SELECT COUNT(*) FROM ALL_USERS
7 WHERE USERNAME = :OLD.USER_ID;
8 IF FOUND_USER = 0 THEN
9 DELETE FROM SYSTEM_USER
10 WHERE ROWID := :NEW.ROWID;
11 ELSE
12 RAISE_APPLICATION_ERROR(-20000,'USER FOUND IN ALL_USERS TABLE');
13 END IF;
14 END TIBD_USER;
15 /
Warning: Trigger created with compilation errors.
SQL> SHOW ERRORS
Errors for TRIGGER TIBD_USER:
LINE/COL ERROR
7/1 PL/SQL: SQL Statement ignored
8/13 PL/SQL: ORA-00920: invalid relational operator
ANY HELP PLEASE

1 CREATE OR REPLACE TRIGGER TIBD_USER
2 BEFORE DELETE ON SYSTEM_USER FOR EACH ROW
3 DECLARE
4 FOUND_USER NUMBER(2);
5 BEGIN
6 SELECT COUNT(*) INTO FOUND_USER FROM ALL_USERS
7 WHERE USERNAME = :OLD.USER_ID;
8 IF FOUND_USER = 0 THEN
9 DELETE FROM SYSTEM_USER
10 WHERE ROWID = :OLD.ROWID;
11 ELSE
12 RAISE_APPLICATION_ERROR(-20000,'USER FOUND IN ALL_USERS TABLE');
13 END IF;
14* END TIBD_USER;
SQL> /
Trigger created.
WHEN I DELETE A RECORD IT IS GIVING ME THAT
SQL> DELETE FROM SYSTEM_USER
2 WHERE CTUT_ID = 3;
DELETE FROM SYSTEM_USER
ERROR at line 1:
ORA-04091: table SYSTEM_USER is mutating, trigger/function may not see it
ORA-06512: at "TIBD_USER", line 7
ORA-04088: error during execution of trigger 'TIBD_USER'

Similar Messages

  • Help needed writing trigger for deleting records from multipul tables

    i am trying to write a trigger which would help me delete the record from 3 different tables
    lets say i have table a , b and c
    i an trying to write a trigger which would help me delete the same record from table a and c.
    drop trigger az_zzz_trigger;
    create trigger az_zzz_trigger
    before INSERT or UPDATE or DELETE ON az_employ
    FOR EACH ROW
    BEGIN
    IF DELETING then
    delete from za_payroll
    delete from az_salary_audit
    end if;
    end;
    while executing this trigger all data of table za_payroll is delete.
    what should i do so that only the record which i delete from az_employ gets deleted from az_payroll and az_salary_audit

    872959 wrote:
    i am trying to write a trigger which would help me delete the record from 3 different tables
    lets say i have table a , b and c
    i an trying to write a trigger which would help me delete the same record from table a and c.
    drop trigger az_zzz_trigger;
    create trigger az_zzz_trigger
    before INSERT or UPDATE or DELETE ON az_employ
    FOR EACH ROW
    BEGIN
    IF DELETING then
    delete from za_payroll
    delete from az_salary_audit
    end if;
    end;
    while executing this trigger all data of table za_payroll is delete.
    what should i do so that only the record which i delete from az_employ gets deleted from az_payroll and az_salary_auditutilize appropriate WHERE clause

  • 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

  • PRE-DELETE TRIGGER ERROR

    I am debugging a program written by Developer/2000 forms 4.5
    I use 3 to 4 base tables and they all have parent/child relatioship.
    When i try to delete one of them , the error is : pre-delete trigger error.
    any idea ?

    Hi, Lesley
    Beware:
    your form_success test is not working.
    The form_success, form_failure and form_fatal functions should only be used to test the outcome of Forms Built-ins.
    If you want to test the result of DELETE statements, try:
    DELETE FROM TABLE
    WHERE CONDITION;
    IF SQL%FOUND THEN
    MESSAGE('Some rows were deleted');
    ELSE
    MESSAGE('No rows were deleted');
    END IF;You can also use SQL%ROWCOUNT to know how many rows were deleted.
    Hope this helps,

  • Can anyone help me deleted this existing photos in my photo library.i already did to sync it up on itunes ang remove all photos ang sync.but still there is 5 photos in my general about eventhough my camera roll is empty.plsss help.thanks.

    can anyone help me deleted this existing photos in my photo library.i already did to sync it up on itunes ang remove all photos ang sync.but still there is 5 photos in my general about eventhough my camera roll is empty.PLEASE HELP!Thanks.

    i mean "and" not "ang" btw.

  • EMail on my iPhone 4s is no longer synching right with my Mac. Things I delete on my Mac are still on my iPhone. HELP!  Deleting in both places is so time consuming. It has worked until recently...not sure what changed!

    Things I delete on my Mac are still on my iPhone. HELP!  Deleting in both places is so time consuming. It has worked until recently...not sure what changed!

    Your email provider may have changed certain settings to do with deleting from server when deleting from client so that it no longer works. Are you using pop or imap? If pop then recreate the account using imap.

  • Can you help me delete photo library & duplicate photo library

    Hi,
    I don't know what I did, but I somehow copied 44 of my daughter's music images into my photo library and then created a duplicate with 43 images.  These 87 images are taking up room and there is no obvious way to delete them. 
    Here's what happens when I touch camera roll it brings me to the photo library -- where 43 out of 44 images are hers:
    I click edit and the only options are share via email, faceboook, twitter, etc. or add to which leads to create a new album.  I don't want to do any of this.  I just want to delete them to make room.
    The same options apply for the Duplicate album as well.
    Can you help me delete the photo library and the duplicate library on my phone?  I only want to keep one video in the camera roll and all of the rest of the images can go.
    Thanks!

    Thanks for your response.  I think you just isolated my issue -- I was trying to upload music and somehow wound up syncing my daughter's photos to my iphone.  I don't know how to delete them because not all of the album/folders are appearing there.  Do you have any suggestions on how to find them?

  • :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

  • HELP - I deleted an adobe file that I saved to a memory stick and I need it back really badly :(

    HELP - I deleted an adobe file that I saved to a memory stick and I need it back really badly

    Try this Google search:
    http://www.google.cl/#hl=es&tbo=d&sclient=psy-ab&q=free+data+recovery+software+for+flash+d rives&oq=recovery+software+for+flash+drives&gs_l=hp.1.3.0i30j0i5i30l3.9708.24161.0.34539.3 4.26.0.8.8.0.378.7320.2-21j5.26.0...0.0...1c.1.lb0o4diaEEU&pbx=1&bav=on.2,or.r_gc.r_pw.r_q f.&bvm=bv.1355534169,d.eWU&fp=ff7a879957bf69ba&bpcl=40096503&biw=1112&bih=865
    It gave me plenty of hits, but be a little careful. I would suggest using the free trial of one of the paid products. As I have never used one, I cnnot recommand any.
    And remeber: every time you write something on that flash drive, you risk overwriting your lost file, thus decreasing your chances of recovering it.

  • Help! deleted file on dropbox can i recover it on time machine?

    Help I deleted my senior project! So i was responsible and synced my project with drobox and "regularly' backed up my computer. Well, after I turned it in I needed some space in my drobox and deleted my synced folder which deleted it off my computer. I backed up my computer the day after and I guess I didnt realize that I had deleted this IMPORTANT folder for awhile... So its 3 months from when I did that so too late to recover the lost files from dropbox. Is there a way I can recover the folder from time machine? The last backup was way too old before I deleted the file and I only have a backup from the day AFTER I deleted it. Please help!

    Read https://discussions.apple.com/thread/3822378?start=0&tstart=0
    Allan

  • 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.

  • Need help on deleting after exporting to IDML

    Hi Forum,
    I have a script to export all the "indesign files" found inside the folder... and then remove all the indesign files...
    Instead, Can i have a help to delete....
    the indesign file after exporting to IDML and then
    continue opening the next indesign file, exporting to IDML and deleting it...
    then continue file3....
    The below is the script open indesign files and export to idml and then remove all the files inside the folder Desktop/GENERAL_CS6-IDML/IN_CS6
    var mySourceFolder = Folder("Desktop/GENERAL_CS6-IDML/IN_CS6");
    var myOutFolder = Folder("Desktop/Completed");
    //var Indd_files = get_Files(mySourceFolder,[], ".indd");
    var Indd_files = mySourceFolder.getFiles (/\.indd|\.INDD/i);
    //var Indd_files = get_Files(mySourceFolder,[], ".indd");
    if(Indd_files.length>0)
        for(var w=0;w<Indd_files.length;w++)
            try{
                 app.scriptPreferences.userInteractionLevel = UserInteractionLevels.neverInteract;
                myDoc = app.open(File(Indd_files[w]));
                 app.scriptPreferences.userInteractionLevel = UserInteractionLevels.neverInteract;
                 var myDocNam = stripExt((Indd_files[w].name).replace(/%20/g," "))+"idml";
                 myDoc.exportFile(ExportFormat.INDESIGN_MARKUP, (File(myOutFolder+"/"+myDocNam)));           
    //          myDoc.save(File(myOutFolder+"/"+myDocNam));
                app.activeDocument.close(SaveOptions.no);
            }catch(E){}
    try {
    var copyFolder1 = Folder("Desktop/GENERAL_CS6-IDML/IN_CS6");
    var myFiles2 = copyFolder1.getFiles(/\.indd|\.INDD/i);
    for ( i = myFiles2.length-1; i >= 0 ; i-- ){
    myFiles2[i].remove();
    } catch (e) {}
    Many thanks for the support..

    I hope this is what you expected.
    You can try the following code:
    var mySourceFolder = Folder("Desktop/GENERAL_CS6-IDML/IN_CS6");
    var myOutFolder = Folder("Desktop/Completed");
    //var Indd_files = get_Files(mySourceFolder,[], ".indd");
    var Indd_files = mySourceFolder.getFiles (/\.indd|\.INDD/i);
    //var Indd_files = get_Files(mySourceFolder,[], ".indd");
    if(Indd_files.length>0)
        for(var w=0;w<Indd_files.length;w++)
            try{
                 app.scriptPreferences.userInteractionLevel = UserInteractionLevels.neverInteract;
                 var myFile = File(Indd_files[w]);
                myDoc = app.open(myFile);
                 app.scriptPreferences.userInteractionLevel = UserInteractionLevels.neverInteract;
                 var myDocNam = stripExt((Indd_files[w].name).replace(/%20/g," "))+"idml";
                 myDoc.exportFile(ExportFormat.INDESIGN_MARKUP, (File(myOutFolder+"/"+myDocNam)));          
                app.activeDocument.close(SaveOptions.no);
                myFile.remove();
            }catch(E){}
    Green4ever

  • 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

  • Help in DELETE script

    Hi, I have duplicate records and want to delete any 2nd duplicate row. Please help in DELETE script.
    Select * from cmhrec
    Sr_no  code    date     charge   Charge2 Code_ID
    1        AAA   1-1-2014  24.0      24.0      5
    1        AAA   1-1-2014  24.0      24.0      9
    2        AAA   1-1-2014  23.0      23.0      4
    2        AAA   1-1-2014  23.0      23.0      11
    3        AAA   1-1-2014   26.0      28.0     12
    3        AAA   1-1-2014   26.0      28.0     19
    --Desired output after deleting any 2nd duplicate row.
    Sr_no  code    date     charge   Charge2 Code_ID
    1        AAA   1-1-2014  24.0      24.0      5
    2        AAA   1-1-2014  23.0      23.0      4
    3        AAA   1-1-2014   26.0      28.0     12

    Thank you
    You can simply do this
    DELETE t
    FROM cmhrec t
    WHERE EXISTS
    SELECT 1
    FROM cmhrec
    WHERE SrNo = t.SrNo
    AND Code = t.Code
    AND Code_ID < t.Code_ID
    Please Mark This As Answer if it solved your issue
    Please Mark This As Helpful if it helps to solve your issue
    Visakh
    My MSDN Page
    My Personal Blog
    My Facebook Page

  • How to make before delete trigger

    Hi all
    I want make before delete trigger
    I have 2 tables hr_api_transactions and new_table
    when I delete records from hr_api_transations table
    then that deleted record should be stored/insert in new_table table
    for that purpose I need before delete trigger
    How can I make It?

    Hi
    I have written following code but it gives an error
    CREATE OR REPLACE TRIGGER before_delete_trigger
        BEFORE DELETE
            ON HR_API_TRANSACTIONS
            FOR EACH ROW
        DECLARE
        BEGIN
            delete from new_table where new_table.name = OLD.hr_api_transactions.api_addtnl_info;
        END;
    TRIGGER BEFORE_DELETE_TRIGGER compiled
    Errors: check compiler logafter show errors it shows
    4/54 PL/SQL: ORA-00904: "OLD"."HR_API_TRANSACTIONS"."API_ADDTNL_INFO": invalid identifier
    4/9 PL/SQL: SQL Statement ignoredhere hr_api_transaction is table which contain API_ADDTNL_INFO column
    and name column of new_table and API_ADDTNL_INFO column of
    HR_API_TRANSACTIONS table are same

Maybe you are looking for