CREATE TRIGGER must be the first statement in a batch

左心房为你撑大大i 提交于 2019-12-21 06:57:51

问题


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.


回答1:


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.




回答2:


If you are trying this from SQL Server Management Studio, here is another option which worked for me:

In the left pane, right-click on the database and select "New Query".

This connects you to the specific database. Now you can enter your create trigger statement as the first statement in the query window which opens. There is no need for a "use" command.



来源:https://stackoverflow.com/questions/13370505/create-trigger-must-be-the-first-statement-in-a-batch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!