Can an Oracle trigger be disabled for the current session?

前端 未结 4 1163
日久生厌
日久生厌 2021-02-19 16:28

I\'d like to disable a specific trigger on a table prior to inserting data into it, but without affecting other users that may be changing data in that same tab

4条回答
  •  无人及你
    2021-02-19 17:06

    Use dbms_application_info.set_client_info (link to oracle documentation) to set_client_info in the procedure and read it in the trigger.

    Simple example:

    SET SERVEROUTPUT ON   
    declare
    CI_Status VARCHAR2(25 BYTE):='';
    begin
    --set the value
    dbms_application_info.set_client_info('qwerty');
    -- the value is sent an out to CI_Status when you want to read it
    DBMS_APPLICATION_INFO.READ_CLIENT_INFO (CI_Status);
    --Output the value in the console 
    dbms_output.put_line('Value of CI_Status is: ' || CI_Status); 
    end;
    

    In your procedure:

    procedure spname is
        begin
          dbms_application_info.set_client_info('qwerty');
          --Do your update
          UPDATE tableName set a=b;
          --in case you still have the sesion opened, set it to null after
          dbms_application_info.set_client_info(null);
    end;
    

    In you trigger:

    create or replace TRIGGER Triggername
     BEFORE INSERT OR UPDATE
     ON tablename
     FOR EACH ROW
    declare
    CI_Status VARCHAR2(25 BYTE):='';
    begin
    --Retrieve the value into CI_Status the variable
    DBMS_APPLICATION_INFO.READ_CLIENT_INFO(CI_Status); 
        IF INSERTING THEN
           null;
        ELSIF UPDATING THEN
              IF CI_Status = 'qwerty' then
                 --Do nothing since you dont want the trigger to fire
                  null;
              ELSIF CI_Status is null then
                :new.tablefield:= SYSDATE;
              END IF;
        END IF;
    end Triggername;
    

    To disable completely without worrying about concurrences

     procedure spname is
        begin    
          EXECUTE IMMEDIATE 'ALTER TRIGGER Triggername DISABLE';
          UPDATE tableName set a=b;
          EXECUTE IMMEDIATE 'ALTER TRIGGER Triggername ENABLE';
       end;
    

提交回复
热议问题