Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
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 . – Super Peon Jun 18 '13 at 14:58
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. – Nick Chammas Jun 25 '13 at 9:08
up vote 10 down vote accepted

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... 
    If app_name() = 'something' 
        put code here 
share|improve this answer
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. – Aaron Bertrand Jun 18 '13 at 13:21
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. – db2 Jun 18 '13 at 15:25
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. – mrdenny Jun 19 '13 at 0:29

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.