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


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'

or something like that.  Thanks.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

You could do something like this. But may not be a good thing.

create trigger tu_trigtest on trigtest
for update
  If (SELECT USER_NAME())  <> 'dbo'
    -- Your Code
Scott PletcherSenior DBACommented:
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:

HOST_NAME() -- risky to rely on

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.
dentyneAuthor Commented:
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?  
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

If update(columnname)
 -- Your code

Scott PletcherSenior DBACommented:

IF UPDATE(adminCol1) OR UPDATE(adminCol2) OR UPDATE(adminCol3)
    RETURN  -- exit trigger

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dentyneAuthor Commented:
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.
dentyneAuthor Commented:
Ahh okay Scott. That seems like gold there
dentyneAuthor Commented:
Is there a way to split the points between you two guys?  Both of you gave great answers
Yes. There is an option to  split points.

Also look at

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.


It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.