Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL error: 'CREATE/ALTER PROCEDURE' must be the first statement in a query batch

I am running a SQL script but getting an error:

'CREATE/ALTER PROCEDURE' must be the first statement in a query batch

Here's my code:

IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'myproc') AND type in (N'P', N'PC')) DROP PROCEDURE [dbo].[myproc]  create PROCEDURE [dbo].[myproc]  AS BEGIN     select * from mytable END GO 

How can I solve it?

like image 882
user603007 Avatar asked Jan 29 '12 04:01

user603007


People also ask

How do you resolve Create trigger must be the first statement in a query batch?

Just add a GO on the line before the CREATE TRIGGER. 'CREATE TRIGGER' must be the first statement in a query batch.

Can we alter a already created stored procedure?

Expand Stored Procedures, right-click the procedure to modify, and then select Modify. Modify the text of the stored procedure.


1 Answers

Run your statement in the following form:

IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'myproc') AND type in (N'P', N'PC'))   DROP PROCEDURE [dbo].[myproc] GO create PROCEDURE [dbo].[myproc] AS BEGIN     select * from mytable END GO 

Note the GO batch separator after DROP PROCEDURE

like image 177
Oleg Dok Avatar answered Oct 06 '22 05:10

Oleg Dok