我有一个SQL脚本,每次客户端执行“数据库管理”功能时都必须运行。该脚本包括在客户端数据库上创建存储过程。其中一些客户端在运行脚本时可能已经拥有存储过程,而另一些客户端可能没有。我需要将丢失的存储过程添加到客户端数据库中,但无论我如何尝试改变T-SQL语法,我都得到了这个结果

CREATE/ALTER PROCEDURE'必须是查询批处理中的第一个语句

我在创作作品之前读到过这种说法,但我不喜欢这样做。

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

CREATE PROCEDURE MyProc
...

如何添加检查存储过程是否存在,如果存储过程不存在则创建它,如果存储过程存在则修改它?


当前回答

我也犯了同样的错误。我知道这个线程已经死了,但我想设置另一个选项除了“匿名过程”。

我是这样解决的:

Check if the stored procedure exist: IF NOT EXISTS (SELECT * FROM sysobjects WHERE name='my_procedure') BEGIN print 'exists' -- or watever you want END ELSE BEGIN print 'doesn''texists' -- or watever you want END However the "CREATE/ALTER PROCEDURE' must be the first statement in a query batch" is still there. I solved it like this: SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE -- view procedure function or anything you want ... I end up with this code: IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID('my_procedure')) BEGIN DROP PROCEDURE my_procedure END SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE PROCEDURE [dbo].my_procedure ...

其他回答

如果存在则删除 是SQL Server 2016的新特性吗

https://blogs.msdn.microsoft.com/sqlserverstorageengine/2015/11/03/drop-if-exists-new-thing-in-sql-server-2016/

DROP  PROCEDURE IF EXISTS dbo.[procname]

你为什么不简单点

    IF EXISTS(SELECT * FROM sys.procedures WHERE NAME LIKE 'uspBlackListGetAll')
    BEGIN
         DROP PROCEDURE uspBlackListGetAll
    END
    GO

    CREATE Procedure uspBlackListGetAll

..........

我知道你想“修改一个存在的过程,创建一个不存在的过程”,但我相信这样更简单:

删除该过程(如果它已经存在),然后 重新创建它。

是这样的:

IF OBJECT_ID('MyProcedure', 'P') IS NOT NULL
    DROP PROCEDURE MyProcedure
GO

CREATE PROCEDURE MyProcedure AS
BEGIN
    /* ..... */
END
GO

第二个参数告诉OBJECT_ID只查找object_type = 'P'的对象,这些对象是存储过程:

AF = Aggregate function (CLR) C = CHECK constraint D = DEFAULT (constraint or stand-alone) F = FOREIGN KEY constraint FN = SQL scalar function FS = Assembly (CLR) scalar-function FT = Assembly (CLR) table-valued function IF = SQL inline table-valued function IT = Internal table P = SQL Stored Procedure PC = Assembly (CLR) stored-procedure PG = Plan guide PK = PRIMARY KEY constraint R = Rule (old-style, stand-alone) RF = Replication-filter-procedure S = System base table SN = Synonym SO = Sequence object TF = SQL table-valued-function TR = Trigger

你可以通过以下途径获得完整的选项列表:

SELECT name 
FROM master..spt_values
WHERE type = 'O9T'

创建程序(如果不存在)结束

我也犯了同样的错误。我知道这个线程已经死了,但我想设置另一个选项除了“匿名过程”。

我是这样解决的:

Check if the stored procedure exist: IF NOT EXISTS (SELECT * FROM sysobjects WHERE name='my_procedure') BEGIN print 'exists' -- or watever you want END ELSE BEGIN print 'doesn''texists' -- or watever you want END However the "CREATE/ALTER PROCEDURE' must be the first statement in a query batch" is still there. I solved it like this: SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE -- view procedure function or anything you want ... I end up with this code: IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID('my_procedure')) BEGIN DROP PROCEDURE my_procedure END SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE PROCEDURE [dbo].my_procedure ...