Solved

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

Posted on 2003-11-18
9
264 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:Scott Pletcher
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
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 
LVL 15

Assisted Solution

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

HTH
0
 
LVL 69

Accepted Solution

by:
Scott Pletcher 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

726 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