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 7430413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:08:12+00:00 2026-05-29T09:08:12+00:00

When making a SQL script to create a trigger on a table, I wanted

  • 0

When making a SQL script to create a trigger on a table, I wanted to check that the trigger doesn’t already exist before I create it. Otherwise the script cannot be run multiple times.

So I added a statement to first check whether the trigger exists. After adding that statement, the CREATE TRIGGER statement no longer works.

IF NOT EXISTS (SELECT name FROM sysobjects
               WHERE name = 'tr_MyTable1_INSERT' AND type = 'TR')
BEGIN
    CREATE TRIGGER tr_MyTable1_INSERT
        ON MyTable1
        AFTER INSERT
    AS
    BEGIN
        ...
    END
END
GO

This gives:

Msg 156, Level 15, State 1, Line 5
Incorrect syntax near the keyword 'TRIGGER'.

The solution would be to drop the existing trigger and then create the new one:

IF EXISTS (SELECT name FROM sysobjects
           WHERE name = 'tr_MyTable1_INSERT' AND type = 'TR')
    DROP TRIGGER tr_MyTable1_INSERT
GO
CREATE TRIGGER tr_MyTable1_INSERT
    ON MyTable1
    AFTER INSERT
AS
BEGIN
    ...
END
GO

My question is: why is the first example failing? What is so wrong with checking the trigger exists?

  • 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-05-29T09:08:13+00:00Added an answer on May 29, 2026 at 9:08 am

    Certain statements need to be the first in a batch (as in, group of statements separated by GO ).

    Quote:

    CREATE DEFAULT, CREATE FUNCTION, CREATE PROCEDURE, CREATE RULE, CREATE SCHEMA, CREATE TRIGGER, and CREATE VIEW statements cannot be combined with other statements in a batch. The CREATE statement must start the batch. All other statements that follow in that batch will be interpreted as part of the definition of the first CREATE statement.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am making a script that deletes all tables before recreating them with the
I have a SQL script that is being executed in TOAD. Currently, I have
I have some sql script files where i am making some DDL changes as
I am often making changes to some large sql script files containing hundreds of
I am making a SQL script editor / executor component for my application, very
I need to display a SQL Script in my application I am making. It
In my T-SQL script, I refer to same long table name several times. I
I'm making a SQL database with MySQL. I need a column that has only
In SQL Server 2005, are there any disadvantages to making all character fields nvarchar(MAX)
I am sure making a silly mistake but I can't figure what: In SQL

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.