November 2, 2003 at 12:00 am
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
November 3, 2003 at 6:44 am
Andy, cannot find the content, is there a problem?
Far away is close at hand in the images of elsewhere.
Anon.
November 3, 2003 at 9:12 am
Hasnt been compiled yet, probably tonight when I get home!
Andy
November 4, 2003 at 6:44 am
Still not showing. Also showing publish date of 11/24 -- doesn't sound right.
November 4, 2003 at 9:24 am
It's future content, we load ahead of time. You're right, the link is still down, I'll check.
Andy
November 24, 2003 at 1:29 am
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
November 24, 2003 at 2:45 am
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.
November 24, 2003 at 6:19 am
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
November 24, 2003 at 4:47 pm
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
November 25, 2003 at 3:22 am
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
November 25, 2003 at 3:33 am
It will fire once.
November 25, 2003 at 7:31 am
Good article, learn something new every day.
quote:
If we execute the following statement, what trigger(s) will fire and how many times will they fire?
quote:
Assume that the syntax is valid
Which means that the insert woul be successful.
If you are using SQL2K and a 'INSTEAD OF INSERT' trigger exists for the table (there can be only one) then it would be fired first and it may or may not cause 'FOR INSERT' triggers to fire.
Subject to above, each FOR INSERT trigger (aka AFTER trigger) created for the table will be fired once only, for the insert operation, in no specific order (unless using SQL2K and the system stored procedure sp_settriggerorder is used to set order).
These 'FOR INSERT' triggers will contain a single 'inserted' table consisting of all the row(s) to be inserted as a result of the insert operation.
Each row will have all the columns (including IDENTITY) of the table being 'inserted into'.
Far away is close at hand in the images of elsewhere.
Anon.
November 26, 2003 at 7:58 am
Good comments and good answers. Yes, it is open ended. The point Im trying to make is that answering the question is not simple by any means. Instead of triggers, recursive triggers, nested triggers, etc - these and more can affect what happens. I'll let the thread run a while longer before picking a winner!
Andy
November 27, 2003 at 4:05 am
Hi Andy,
Assumptions:
1.Trigger is Enabled while inserts take place.
2.We are not discussing "Instead of " triggers as there is no view in the context.
Here are all the Possibilities.
create table someothertable
(rowid int, SOMEVALUE INT)
insert someothertable values (1,1)
insert someothertable values (2,2)
insert someothertable values (3,3)
insert someothertable values (4,4)
insert someothertable values (5,5)
insert someothertable values (6,6)
insert someothertable values (7,7)
insert someothertable values (8,8)
insert someothertable values (9,9)
insert someothertable values (10,10)
go
select rowid,somevalue
from someothertable
go
create table testtable
(rowid int, SOMEVALUE INT)
go
create table trig_count
([description] varchar(50), cnt int)
insert trig_count values ('no of inserts',0)
-- this trigger will fire only once whether single/multi inserts take place
create trigger insert_trig
on testtable
after insert as
update trig_count
set cnt = cnt+1
-- insert single record
insert into testtable (rowid, somevalue) select top 1 rowid, somevalue from someothertable
-- verify the trigger functionality
select cnt from trig_count
-- trigger fired only once
--now,insert multi record
insert into testtable (rowid, somevalue) select top 10 rowid, somevalue from someothertable
-- verify the trigger functionality after multi inserts
select cnt from trig_count
-- Again,trigger fired only once (2-1)
-- now alter the trigger to fire as many times as multiple inserts take place.
ALTER trigger insert_trig
on testtable
after insert as
UPDATE trig_count
SET cnt = cnt +
(SELECT COUNT(SOMEVALUE)
FROM inserted
)
-- insert single record
insert into testtable (rowid, somevalue) select top 1 rowid, somevalue from someothertable
-- verify the trigger functionality
select cnt from trig_count
-- trigger fired only once (3-2)
--now,insert multi record
insert into testtable (rowid, somevalue) select top 10 rowid, somevalue from someothertable
-- verify the trigger functionality after multi inserts
select cnt from trig_count
--trigger fired THAT many times insert takes place (13-3)
Praveen Kumar Peddi
Praveen Kumar Peddi
November 24, 2004 at 6:16 am
Do you agree on adding the following (in red):
"There are two downsides to this approach.
Viewing 15 posts - 1 through 15 (of 16 total)
You must be logged in to reply to this topic. Login to reply