Getting Started with ASP.NET MVC - Part 6: ASP.NET MVC and Entity Framework
First Time? You can support us by signing up. It takes only 5 seconds. Click here to sign up. If you already have an account, click here to login.


Upload Image Close it
Select File

Interview questions and answers on various SQL Server areas such as Database Administration, TSQL Development, BI etc.

Moderators

Browse by Levels · View All
Beginner 59
Intermediate 26
Advanced 3

Browse by Job Position · View All
Developer 44
DBA 13
Sql developer 11
Software Engineer 2
SQL Server Administrator 1

Browse by Tag · View All
SQL Server Interview Question 16
SQL Server interview questions 9
TSQL 6
SQL SERVER 5
T-SQL 4

SQL Server - Is it possible to create a trigger across databases?

Feb 22 2012 12:00AM by Ramireddy   

Question

Recently i came across this questions, "Is it possible to create a trigger on a table, which is in another database"

Answer

No. We can't create triggers on different database. If we try to create, we will receive an error like below.

Cannot create trigger on 'tablename' as the target is not in the current database.

Read More...  [5005 clicks]


Ramireddy
3 · 39% · 8882
11
 
3
 
0
Move



Submit

5  Comments  

  • Do you mean create a trigger on a table which is not in the current database?

    Actually, this is possible, but you can't use three-part naming. You have to do something like this:

    EXEC [targetdb].[sys].[sp_ExecuteSQL] 
    N'
       CREATE TRIGGER trg_Testing 
       ON [dbo].[foobar]
       FOR INSERT, UPDATE, DELETE
       AS
          BEGIN
             PRINT ''Ta Da!''
          END
    '
    
    commented on Feb 22 2012 7:42AM  .  Report Abuse This post is not formatted correctly
    Marc Jellinek
    119 · 2% · 359
  • I never thought this could be a serious question. But I am seeing 3 testimonials already :-) and it looks interesting.

    commented on Feb 23 2012 12:07PM  .  Report Abuse This post is not formatted correctly
    Jacob Sebastian
    1 · 100% · 22504
  • Hi Marc,

    Question was different. trigger should be based on of table in different database..

    commented on Feb 24 2012 7:52AM  .  Report Abuse This post is not formatted correctly
    Ramireddy
    3 · 39% · 8882
  • @Ramireddy, the question wasn't very clear on that point. The question appeared to ask if a trigger could be created from a different database, not can a trigger in one database be applied to table in another.

    To clarify, a trigger on a table must exist within the same database. The trigger must also exist within the same schema as the table.

    commented on Feb 24 2012 12:13PM  .  Report Abuse This post is not formatted correctly
    Marc Jellinek
    119 · 2% · 359
  • I also misunderstood the original question.

    I thought the question was asking if it was possible to be logged into a database and execute code to create a trigger on a table in a different database.

    Yes that can be accomplished.

    If the question is if you can create a trigger that will exist inside the current database that will be applied against a table that exists in a different database, the answer is no.

    And hey look, I'm a 1%er.

    commented on Feb 27 2012 8:39AM  .  Report Abuse This post is not formatted correctly
    Cris
    167 · 1% · 219

Your Comment


Sign Up or Login to post a comment.

"SQL Server - Is it possible to create a trigger across databases?" rated 5 out of 5 by 11 readers
SQL Server - Is it possible to create a trigger across databases? , 5.0 out of 5 based on 11 ratings
    Copyright © Beyondrelational.com Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising