8

I was wondering if it is possible to enable a trigger just for my current application and not for any sql executed against the table.

The situation:

There are two applications working on the same database. App1 and App2.

I have a trigger on 'MyTable' which should only be fired when App1 is executing a query, but not when App2 is doing so.

Aaron Bertrand
182k28 gold badges407 silver badges626 bronze badges
asked Jun 18, 2013 at 11:38
2
  • Well its logical . isn't it ? where would you have the app name ? If you do have access to it in the trigger ( inserted ) then its easy . Commented Jun 18, 2013 at 14:58
  • 1
    Patric, as an alternative to using APP_NAME(), you can also use SET CONTEXT_INFO. SET CONTEXT_INFO is commonly used to send info to triggers like this. Commented Jun 25, 2013 at 9:08

1 Answer 1

10

While not a good design, it is doable. You'll need to put logic in the trigger so it causes the code to only be executed when the correct application is connected.

Create trigger... 
As
 If app_name() = 'something' 
 begin 
 put code here 
 end
Jon Seigel
16.9k6 gold badges46 silver badges86 bronze badges
answered Jun 18, 2013 at 12:08
3
  • 8
    It may not be a concern in this specific case, but other potential readers should note that app_name() is very easy to spoof via connection string properties. Commented Jun 18, 2013 at 13:21
  • 4
    Yup, very easy to spoof. Don't do this for security purposes, but for working around minor software deficiencies, or other convenient tricks, you're probably okay. We've got a few that do this. Commented Jun 18, 2013 at 15:25
  • 1
    Very true, this would be VERY easy to get around. But assuming that there's no naught intentions of the application users (I can't believe I just put that) it'll work. Not the best idea, but it'll do the job. Jon, thanks for formatting. Wasn't going to try doing that on my phone. Commented Jun 19, 2013 at 0:29

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.