Solved

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

Posted on 2003-11-18
9
265 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

I have a large data set and a SSIS package. How can I load this file in multi threading?
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

729 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