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)
GOis 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$