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.
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.
SQL Server has three types of triggers: DML (Data Manipulation Language) Triggers. DDL (Data Definition Language) Triggers. Logon Triggers.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With