Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Disabling a trigger within a trigger?? 1

Status
Not open for further replies.

aljubicic

Programmer
Nov 7, 2002
82
0
0
AU
Hi all,

I would like to disable a trigger within a trigger using....

ALTER TABLE RESIDENTIAL DISABLE TRIGGER UPDATE_SYSTEMS_RES

However it tells me that I cant disable a trigger within one. How can I get around this or is there another way of disabling a trigger.

Thanks
Anthony
 
Have you tried creating a stored proc which disables the second trigger and just executing the SP from your first trigger? --James
 
Another approach is to create a table that stores trigger names and flags. Example: trigger_name, flag_value. Data would be "my_trigger", 0. When you are in the trigger, simply read from the table and put an "if" condition to see if the trigger should execute based on the flag value. Ex:
Create trigger my_trigger on tablename for insert
AS
declare @flag int
select @flag = flag_value from trigger_table where trigger_name = 'my_trigger'
if @flag = 1
begin
'trigger code here
end

You can update the table from anywhere - to set the flag value depending on a particular condition and/or reset it.
 
I don't know if a TRIGGER can be disabled from another TRIGGER. Remember that most users will not be able to disable a TRIGGER. Permissions to ALTER a table default to the table owner, DB owner or members of the db_owner or system admin roles.

jmorgan's idea has a lot of merit. All users could be given permission to update the trigger_table under control of a program. I recommend one change to speed the TRIGGER execution.

Create trigger my_trigger on tablename for insert
AS
If Exists
(Select * from trigger_table
Where trigger_name = 'my_trigger'
And flag_value=1)
begin
'trigger code here
end If you want to get the best answer for your question read faq183-874 and thread183-468158.
Terry L. Broadbent - DBA
SQL Server Page:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top