Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CREATE TRIGGER must be the first statement in a batch

Tags:

I have the below trigger:

 CREATE Trigger enroll_limit on Enrollments  Instead of Insert  As  Declare @Count int  Declare @Capacity int  Select @Count = COUNT(*) From Enrollments  Select @Capacity = Capacity From CourseSections  If @Count < @Capacity  Begin        Insert Into Enrollments Select * From Inserted  End  GO 

I'm getting an error msg saying:

'CREATE TRIGGER' must be the first statement in a query batch.

like image 599
zoe Avatar asked Nov 13 '12 23:11

zoe


People also ask

How do you create a trigger?

To create a trigger in your own schema on a table in your own schema or on your own schema ( SCHEMA ), you must have the CREATE TRIGGER system privilege. To create a trigger in any schema on a table in any schema, or on another user's schema ( schema . SCHEMA ), you must have the CREATE ANY TRIGGER system privilege.

What are 3 types of SQL triggers?

SQL Server has three types of triggers: DML (Data Manipulation Language) Triggers. DDL (Data Definition Language) Triggers. Logon Triggers.

Can trigger be created on views?

You can use the trigger action to update the tables underlying the view, in some cases updating an otherwise “non-updatable” view. You can also use INSTEAD OF triggers to substitute other actions when INSERT, DELETE, or UPDATE statements reference specific columns within the database.

What is a trigger statement?

Any SQL statement that is an instance of the trigger event is called a triggering statement. When the event occurs, triggers defined on tables and triggers defined on views differ in whether the triggering statement is executed: For tables, the trigger event and the trigger action both execute.


1 Answers

The error message "'CREATE TRIGGER' must be the first statement in a query batch." usually occurs when a preceding group (batch) of statements does not have a terminating GO

So, I would suggest adding add a GO to the end of the preceding batch's statements.

like image 130
Mitch Wheat Avatar answered Oct 24 '22 17:10

Mitch Wheat