Solved

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

Posted on 2003-11-18
9
262 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
DTS Connection Failed 7 70
T-SQL: Do I need CLUSTERED here? 13 45
VB.NET 2008 - SQL Timeout 9 24
syntax sql error 2 13
When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
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 shrink a transaction log file down to a reasonable size.
Viewers will learn how the fundamental information of how to create a table.

777 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