SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Bypassing Triggers


Bypassing Triggers

Author
Message
Andy Warren
Andy Warren
SSCoach
SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)

Group: Moderators
Points: 15291 Visits: 2730
Comments posted to this topic are about the content posted at http://www.sqlservercentral.com/columnists/awarren/bypassingtriggers.asp>http://www.sqlservercentral.com/columnists/awarren/bypassingtriggers.asp

Andy
SQLAndy - My Blog!
Connect with me on LinkedIn
Follow me on Twitter
David Burrows
David Burrows
SSChampion
SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)

Group: General Forum Members
Points: 11425 Visits: 9888
Andy, cannot find the content, is there a problem?


Far away is close at hand in the images of elsewhere.

Anon.


Andy Warren
Andy Warren
SSCoach
SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)

Group: Moderators
Points: 15291 Visits: 2730
Hasnt been compiled yet, probably tonight when I get home!

Andy
http://www.sqlservercentral.com/columnists/awarren/

Andy
SQLAndy - My Blog!
Connect with me on LinkedIn
Follow me on Twitter
smcdaniel
smcdaniel
Forum Newbie
Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)

Group: General Forum Members
Points: 1 Visits: 1
Still not showing. Also showing publish date of 11/24 -- doesn't sound right.



Andy Warren
Andy Warren
SSCoach
SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)

Group: Moderators
Points: 15291 Visits: 2730
It's future content, we load ahead of time. You're right, the link is still down, I'll check.

Andy
http://www.sqlservercentral.com/columnists/awarren/

Andy
SQLAndy - My Blog!
Connect with me on LinkedIn
Follow me on Twitter
Razvan Socol
Razvan Socol
SSC Eights!
SSC Eights! (993 reputation)SSC Eights! (993 reputation)SSC Eights! (993 reputation)SSC Eights! (993 reputation)SSC Eights! (993 reputation)SSC Eights! (993 reputation)SSC Eights! (993 reputation)SSC Eights! (993 reputation)

Group: General Forum Members
Points: 993 Visits: 129
The downside of using some place like context_info is that it is unique (per session). Are we sure that our trigger is the only one that uses it ? Are we sure that context_info is used (by other developers in our organization) only for bypassing triggers ? If there are other triggers that use it, should all triggers be bypassed (would we never like to bypass only some triggers) ? If answer to all these questions is yes, then we can use context_info (and it seems better than other methods, because sysprocesses is always in memory); if not, then we should use one (or more) table(s) for configuring trigger bypassing.

Another interesting (not necesarily bad) thing: although context_info is stored in a table, it ignores transactions (it is not affected by rollback-s).

Razvan



jcool
jcool
SSC-Enthusiastic
SSC-Enthusiastic (135 reputation)SSC-Enthusiastic (135 reputation)SSC-Enthusiastic (135 reputation)SSC-Enthusiastic (135 reputation)SSC-Enthusiastic (135 reputation)SSC-Enthusiastic (135 reputation)SSC-Enthusiastic (135 reputation)SSC-Enthusiastic (135 reputation)

Group: General Forum Members
Points: 135 Visits: 10
Interesting article.
I just started using context_info a while ago when I found about it in the forum. I developed a UDF that I use in trigger to check for bypass. I actually check if trigger name is saved inside context_info (convert to varchar, works like a charm).
When I want to disable triggers, I just put their names (as long as cumulative length is <128) inside context_info (converted to binary). Triggers then checks for bypass with a simple UDF call (if dbo.TriggerBypass = 1 return).
This concept should clarify most questions Razvan pointed out. If context_info was used for anything else (I can't think of such need at the moment) then some type of sharing would have to be developed. Varbinary data type may scare some people away, but you can easily convert between varchar and varbinary. 128 bytes is not much, but a lot of signals can be put in with good coding.



chudman
chudman
SSC-Addicted
SSC-Addicted (441 reputation)SSC-Addicted (441 reputation)SSC-Addicted (441 reputation)SSC-Addicted (441 reputation)SSC-Addicted (441 reputation)SSC-Addicted (441 reputation)SSC-Addicted (441 reputation)SSC-Addicted (441 reputation)

Group: General Forum Members
Points: 441 Visits: 406
Andy,

The trigger will fire once and once only and it could be an insert trigger or an instead-of trigger. In sql server 7, this will happen after the insert occurs, and there is no possibility of the instead-of option.

It will only work for the first record in the recordset becaused triggers fire once only.

Jeff Bennett



slider
slider
SSC Rookie
SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)

Group: General Forum Members
Points: 28 Visits: 1
quote:
If we execute the following statement, what trigger(s) will fire and how many times will they fire? Assume that the syntax is valid.

insert into testtable (rowid, somevalue) select top 10 rowid, somevalue from someothertable

Post your answer in the attached discussion forum. The best/most thorough answer gets a free copy of 'The Best of SQLServerCentral.com 2002'! Contest ends 1 week after this article is published.


This doesn't give any background to the structure already in place on this table/database, nor the version of SQL Server in use. Did I miss something or is this just a rather open-ended question?

For SQL Server 2000 you can have multiple triggers set to fire on an INSERT operation. Every one of these triggers would fire on testtable when this statement is executed. If there is an INSTEAD OF INSERT trigger configured on testtable then only that trigger would fire.

NESTED and RECURSIVE TRIGGERS options will also affect what triggers will fire. If any of the initial triggers affect testtable with a new operation (INSERT, UPDATE, DELETE) then that will cause more triggers to fire if NESTED TRIGGERS is turned on. If the initial trigger(s) affect another table with an I/U/D operation then any triggers in place on that table will fire. This is a rather complex scenario if there are multiple triggers. RECURSIVE TRIGGERS is really only an effective setting in those instances where an operation on a table only has one trigger set to fire. If there is more than one trigger set to fire on an operation, then they can call each other in circular reference (not recursive) and it will depend on the NESTED TRIGGERS option and the configured nesting level for the database.

If the NESTED TRIGGERS server-wide setting is set to off then no other triggers on this table will fire if the initial triggers affect testtable again, but triggers on other tables will fire (with the same stoppage if they affect testtable in return, or their own table).



Edited by - slider on 11/24/2003 4:47:29 PM

Edited by - slider on 11/24/2003 4:48:04 PM



georgedo
georgedo
Old Hand
Old Hand (399 reputation)Old Hand (399 reputation)Old Hand (399 reputation)Old Hand (399 reputation)Old Hand (399 reputation)Old Hand (399 reputation)Old Hand (399 reputation)Old Hand (399 reputation)

Group: General Forum Members
Points: 399 Visits: 5
Andy,

very good article on trigger bypass, indeed. One thing that I wanted to mention - comes from a bitter personal experience with CONTEXT_INFO. As you're aware, it is an extra information associated with the connection and I wrongly assumed that once connection is closed, the value is reset, i.e. new connections will get 0 value by default. It turns out that connection pooling mechanism does not actually close the connection when returning it to the pool and CONTEST_INFO does not get reset at all. In other words, if connection pooling is enabled and working (as it should :-), you will most likely inherit CONTEXT_INFO from some previous connection without realising it.

The solution, obviously, to be very diligent in cleaning up CONTEXT_INFO after you've done.

Cheers
Georged


--
georged


--
George
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search