Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Bypassing Triggers Expand / Collapse
Author
Message
Posted Sunday, November 2, 2003 12:00 AM
SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: Moderators
Last Login: Monday, November 17, 2014 2:20 PM
Points: 6,800, Visits: 1,914
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
Post #17829
Posted Monday, November 3, 2003 6:44 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 10:02 AM
Points: 6,997, Visits: 7,155
Andy, cannot find the content, is there a problem?




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

Anon.

Post #85617
Posted Monday, November 3, 2003 9:12 AM
SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: Moderators
Last Login: Monday, November 17, 2014 2:20 PM
Points: 6,800, Visits: 1,914
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
Post #85618
Posted Tuesday, November 4, 2003 6:44 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, July 16, 2004 6:35 AM
Points: 1, Visits: 1
Still not showing. Also showing publish date of 11/24 -- doesn't sound right.


Post #85619
Posted Tuesday, November 4, 2003 9:24 AM
SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: Moderators
Last Login: Monday, November 17, 2014 2:20 PM
Points: 6,800, Visits: 1,914
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
Post #85620
Posted Monday, November 24, 2003 1:29 AM
Say Hey Kid

Say Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey Kid

Group: General Forum Members
Last Login: Friday, September 6, 2013 2:16 AM
Points: 693, Visits: 124
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




Post #85621
Posted Monday, November 24, 2003 2:45 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Friday, May 21, 2010 7:15 AM
Points: 77, 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.




Post #85622
Posted Monday, November 24, 2003 6:19 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Wednesday, July 16, 2014 9:43 AM
Points: 98, Visits: 264
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





Post #85623
Posted Monday, November 24, 2003 4:47 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, November 29, 2005 3:55 PM
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



Post #85624
Posted Tuesday, November 25, 2003 3:22 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Wednesday, April 1, 2009 8:49 AM
Points: 371, 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
Post #85625
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse