?
Solved

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

Posted on 2003-11-18
9
Medium Priority
?
267 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
Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

 
LVL 15

Assisted Solution

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

HTH
0
 
LVL 69

Accepted Solution

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

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.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
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.
Suggested Courses

741 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