Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8919919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:08:36+00:00 2026-06-15T06:08:36+00:00

I am trying to dynamically create a number of triggers across two databases but

  • 0

I am trying to dynamically create a number of triggers across two databases but I am having problems with the Create Trigger statement when switching databases

SET @SQL = 'SELECT Name FROM ' + @DB + '.sys.triggers WHERE Name = ' + '''' 
           + @Table + '_DELTATrigger' + ''''
EXEC(@SQL)

IF @@RowCount > 0   
BEGIN
  SET @SQL = 'USE ' + @DB + '; DROP TRIGGER [dbo].[' + @Table + '_DELTATrigger]'
  EXEC(@SQL)
END

SET @SQL = 'CREATE TRIGGER [dbo].[' + @Table + '_DELTATrigger] 
                ON [dbo].[' + @Table + '] 
                AFTER INSERT, UPDATE 
                AS 
                BEGIN 
                  SET NOCOUNT ON 
                  UPDATE [' + @Table + '] 
                  SET [Delta] = 1 
                  FROM inserted 
                  WHERE inserted.[ID] = [' + @Table + '].[ID] 
                END;'               
EXEC (@SQL)

If I run this statement I get the following issue

Error 8197, Level 16, State 4, Procedure tblBuild_DataPlate_DELTATrigger, Line 1
Message: The object ‘dbo.tblBuild_DataPlate’ does not exist or is
invalid for this operation.

Changing the dynamic create trigger to include the USE statement does not work as the Create Trigger has to be the first statement

SET @SQL = 'USE ' + @DB + '; CREATE TRIGGER [dbo].[' + @Table + '_DELTATrigger]
            ON [dbo].[' + @Table + '] 
            AFTER INSERT, UPDATE 
            AS 
            BEGIN 
              SET NOCOUNT ON 
              UPDATE [' + @Table + '] 
              SET [Delta] = 1 
              FROM inserted 
              WHERE inserted.[ID] = [' + @Table + '].[ID] 
            END;'               
EXEC (@SQL)

Error 111, Level 15, State 1, Procedure -, 
Line 1, Message: 'CREATE TRIGGER' must be the first 
statement in a query batch.

You can not fully qualify the object with the database in this case as you get the following error

Error 166, Level 15, State 1, Procedure -, Line 1,
Message: ‘CREATE/ALTER TRIGGER’ does not allow
specifying the database name as a prefix to the object name.

Is there a dynamic way around this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T06:08:38+00:00Added an answer on June 15, 2026 at 6:08 am

    This code below will run the sp_executesql on the selected database (@DB), so with this, you can create the trigger on the rigth place:

    SET @SQL = 'EXEC ' + @DB + '..sp_executesql N''
    CREATE TRIGGER [dbo].[' + @Table + '_DELTATrigger] 
                                ON [dbo].[' + @Table + '] 
                                AFTER INSERT, UPDATE 
                            AS 
                            BEGIN 
                                SET NOCOUNT ON 
    
                            END;'''               
                EXEC (@SQL)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to dynamically create a set of labels in my XUL Runner application.
I am trying to dynamically create a XML for a webservice but when I
I am trying to dynamically create COM object, call COM method and set COM
I am trying to dynamically create pagination links based on the total number of
I'm trying to dynamically create a navbar from data i fetch from a web
I'm trying to dynamically create alert messages using the jQuery plugin for Bootstrap CSS.
I'm trying to dynamically create a link from a Windows Form to our website
I am trying to dynamically create an XML file using php like this: header
I'm trying to dynamically create a connection string during compile time: private static string
I'm trying to dynamically create buttons at runtime with PyQT4.7 However, this being my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.