删除并创建 SQL Server 过程 [英] 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
但我收到一个错误:
'GO' 附近的语法不正确.
Incorrect syntax near 'GO'.
如果我删除 GO
,我会收到一个错误:
If I remove GO
, I get an error:
'CREATE/ALTER PROCEDURE' 必须是查询批处理中的第一条语句
'CREATE/ALTER PROCEDURE' must be the first statement in a query batch
更新
这些脚本由 Java 构建工具 (Maven) 执行
Update
These scripts are being executed by a Java build tool (Maven)
推荐答案
GO
实际上不是 T-SQL 中的有效术语,它只是 Microsoft 管理工具用来分隔查询批次的分隔符.
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.
你用什么来运行脚本?如果您尝试在代码中执行此操作,则需要将其拆分为两个语句,也许使用正则表达式在 ^GO$
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$
这篇关于删除并创建 SQL Server 过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!