Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can an Oracle trigger be disabled for the current session?

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 table. I can't find any documented way to do this. This is Oracle 11g.

The best solution I've been able to come up with is to create a session variable and have my application set that to some value that the trigger checks for prior to doing its work.

Obligatory anti-trigger comment: I hate triggers.

like image 494
kenchilada Avatar asked Mar 27 '13 12:03

kenchilada


People also ask

Can we disable trigger in Oracle?

When this trigger is created, Oracle Database enables it automatically. You can subsequently disable the trigger with the following statement: ALTER TRIGGER update_job_history DISABLE; When the trigger is disabled, the database does not fire the trigger when an UPDATE statement changes an employee's job.

Can a trigger be disabled?

Triggers can be re-enabled by using ENABLE TRIGGER. DML triggers defined on tables can be also be disabled or enabled by using ALTER TABLE. Changing the trigger by using the ALTER TRIGGER statement enables the trigger.

How do I temporarily disable a trigger?

In Object Explorer, connect to an instance of Database Engine and then expand that instance. Expand the database that you want, expand Tables, and then expand the table that contains the trigger that you want to disable. Expand Triggers, right-click the trigger to disable, and then click Disable.

In which of these cases you might temporarily disable a trigger?

Consider temporarily disabling a trigger if one of the following conditions is true: An object that the trigger references is not available. You must perform a large data load and want it to proceed quickly without firing triggers. You are loading data into the table to which the trigger applies.


2 Answers

Add a variable to an existing package spec (or create a new package):

enable_trigger boolean := true;

Surround the code in the trigger with:

if enable_trigger then

end if;

When you want to "disable" the trigger set the variable to false.

A Best Practice would be to put the variable in the body and write a set procedure and a get function.

like image 163
redcayuga Avatar answered Oct 26 '22 23:10

redcayuga


I dont think that disabling trigger is possible for a particular session is possible in oracle or other rdbms .

My solution is that ,if you know the CURRENT_USER or the session_id from which you login ,than you can put a condition in the trigger .

  IF SYS_CONTEXT ('USERENV', 'CURRENT_USER') <> '<XYZ>' THEN
    --do the operation

  END IF; 

This condition you need to put in your trigger

like image 22
Gaurav Soni Avatar answered Oct 27 '22 00:10

Gaurav Soni