Solved

Possible to bypass a trigger when doing a query on a triggered table?

Posted on 2003-11-18
9
260 Views
Last Modified: 2012-08-14
Hello,

I have a trigger on a table.  Users coming in from the web may modify the record, which automatically sets the LastModified date to the current date and time.  This field is used on user searches.  However, there are some fields in the record that only Administrators of the application can see.  But when the administrators change the record, the LastModified date is also updated; but I don't want this to happen.  So I was wondering if there is some command I can append to my update query that would cause the trigger on the table not to happen.  Hopefully there is something.  It would seem logical if there was.  Like:

UPDATE tblBlah
SET Blah = 'test'
DEACTIVATE TRIGGER

or something like that.  Thanks.
0
Comment
Question by:dentyne
  • 4
  • 3
  • 2
9 Comments
 
LVL 15

Expert Comment

by:namasi_navaretnam
ID: 9774378
You could do something like this. But may not be a good thing.

create trigger tu_trigtest on trigtest
for update
As
BEGIN
  If (SELECT USER_NAME())  <> 'dbo'
  BEGIN
   
    -- Your Code
  END
END
0
 
LVL 69

Expert Comment

by:ScottPletcher
ID: 9774451
Yes, you need some way to identify Admin users.  Any of these values may be what you need to check in your trigger, depending on your environment:

APP_NAME()
HOST_NAME() -- risky to rely on
USER_NAME()
SUSER_SNAME()

Since you want to identify the Admin, you could also check if the Admin role is active for the current user: if so, exit the trigger.

Finally, in the worst case, you could have the Admin appl code create a temp table and check if that table exists in the trigger; if so, exit.
0
 
LVL 1

Author Comment

by:dentyne
ID: 9774588
Hmm...the application administrator is not necessarily the DBO.  But I was thinking. It's only a few "hidden" columns in the record that when modified, I don't want the trigger to run.   Is it possible to list the fields in the table I want the trigger to fire on?  And eliminate the 3 "hidden/special admin" fields?  
0
 
LVL 15

Assisted Solution

by:namasi_navaretnam
namasi_navaretnam earned 50 total points
ID: 9774606
If update(columnname)
begin
 -- Your code
end

HTH
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 69

Accepted Solution

by:
ScottPletcher earned 75 total points
ID: 9774613
Yes:

CREATE TRIGGER ...
...
AS
IF UPDATE(adminCol1) OR UPDATE(adminCol2) OR UPDATE(adminCol3)
    RETURN  -- exit trigger
UPDATE ...
0
 
LVL 1

Author Comment

by:dentyne
ID: 9774642
Hmm I wish there was an easier way.  It seems that the UPDATE(columnname) only holds one name and I can't put the list of columns (all minus the three special ones) in there.  There are about 15 columns in the table. Is there a way to put the trigger on certain columns in the definition.  It's unfortunate that these triggers aren't more flexible.  It seems like others would besides me would have a great need for this too.
0
 
LVL 1

Author Comment

by:dentyne
ID: 9774644
Ahh okay Scott. That seems like gold there
0
 
LVL 1

Author Comment

by:dentyne
ID: 9774650
Is there a way to split the points between you two guys?  Both of you gave great answers
0
 
LVL 15

Expert Comment

by:namasi_navaretnam
ID: 9774663
Yes. There is an option to  split points.

Also look at
IF (COLUMNS_UPDATED()) clause.

COLUMNS_UPDATED returns a varbinary bit pattern that indicates which columns in the table were inserted or updated.

For example, table t1 contains columns C1, C2, C3, C4, and C5. To check whether columns C2, C3, and C4 are all updated (with table t1 having an UPDATE trigger), specify a value of 14. To check whether only column C2 is updated, specify a value of 2.

HTH

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how the fundamental information of how to create a table.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now