Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

drop and create SQL Server procedure

I'm trying to drop and create a procedure in a single script. I tried this:

IF EXISTS (SELECT * FROM sys.objects WHERE type = 'P' AND name = 'Foo')
  DROP PROCEDURE Foo
  GO

CREATE PROCEDURE dbo.Foo
-- procedure body here

But I get an error:

Incorrect syntax near 'GO'.

If I remove GO, I get an error:

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

Update

These scripts are being executed by a Java build tool (Maven)

like image 590
Dónal Avatar asked Jun 22 '11 16:06

Dónal


People also ask

How do I drop a procedure in SQL Server?

Use SQL Server Management Studio Expand Databases, expand the database in which the procedure belongs, and then expand Programmability. Expand Stored Procedures, right-click the procedure to remove, and then select Delete.

How do you create a drop procedure?

CREATE OR ALTER DDL Statement in Sql Server 2016 SP1 Basically, if we are using this statement with a module like Stored Procedure, then if the Stored Procedure doesn't exists it will create it and if it already exists then it will alter the the Stored Procedure.

How do I create a new procedure in SQL Server?

Using SQL Server Management StudioRight-click Stored Procedures, and then click New Stored Procedure. On the Query menu, click Specify Values for Template Parameters. In the Specify Values for Template Parameters dialog box, enter the following values for the parameters shown. Returns employee data.

What is drop procedure in SQL?

DROP PROCEDURE removes the definition of one or more existing procedures. To execute this command the user must be the owner of the procedure(s). The argument types to the procedure(s) usually must be specified, since several different procedures can exist with the same name and different argument lists.


3 Answers

GO is not actually a valid term in T-SQL, it's merely the separator that the Microsoft management tools use to delimit query batches.

What are you using to run the script? If you're trying to do it in code then you'll need to split it into two statements, perhaps using a regex to split on ^GO$

like image 119
Jon Grant Avatar answered Oct 07 '22 00:10

Jon Grant


Try

IF OBJECT_ID ('idhere') IS NOT NULL
   DROP PROCEDURE idhere
GO
CREATE PROCEDURE idhere
@paramsHere PARAMTYPE
AS
BEGIN
     //...code here...
END
GO

This is how I do it, I'm not sure what version of SQL SERVER my work uses, I believe its 2005.

like image 29
Damon Avatar answered Oct 07 '22 01:10

Damon


The easiest way I've found for executing a large scripts outside SSMS from a tool is to use the SQLCMD. (iSQL pre sql 2005) This will work with any environment that allows you to run a shell command.

From the MSDN article

The sqlcmd utility lets you enter Transact-SQL statements, system procedures, and script files at the command prompt, in Query Editor in SQLCMD mode, in a Windows script file or in an operating system (Cmd.exe) job step of a SQL Server Agent job. This utility uses OLE DB to execute Transact-SQL batches.

like image 44
Conrad Frix Avatar answered Oct 07 '22 01:10

Conrad Frix