我正在运行SQL脚本,但出现错误:



这是我的代码:

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

我该如何解决?

最佳答案

以以下形式运行语句:

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

注意GO之后的DROP PROCEDURE批处理分隔符

关于sql-server-2008 - SQL错误: 'CREATE/ALTER PROCEDURE'必须是查询批处理中的第一条语句,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9051006/

10-11 20:39