创建存储过程时出错

Posted

技术标签:

【中文标题】创建存储过程时出错【英文标题】:Error when creating stored procedure 【发布时间】:2012-10-17 12:42:59 【问题描述】:

我不熟悉存储过程,我创建了一个存储过程,它从 C# 页面获取一些参数,然后将结果作为 OUTPUT 参数发送回。

我需要做一些计算才能得到结束日期,所以我最终使用了很多 IF 语句。但是,当我创建存储过程时,我遇到了我不知道如何解决的错误,一切似乎都是正确的!。

这是存储过程代码:

    CREATE PROCEDURE sp_RenewSubscription 
        -- Add the parameters for the stored procedure here
        @Reference nvarchar(100),
        @SubscribtionID nvarchar(100),
        @Months int,
        @Result nvarchar(200) OUTPUT
    AS

BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    DECLARE @EndDate as nvarchar;
    DECLARE @MonthCounts as int;

    IF NOT EXISTS(SELECT [Reference] FROM [Norton].[dbo].[SubscriptionStatus] WHERE [Reference] = @Reference)
    SET @Result = '0: Reference ID not found'
    ELSE
    IF NOT EXISTS(SELECT [Reference] FROM [Norton].[dbo].[SubscriptionStatus] WHERE [Reference] = @Reference AND [SubscribtionID] = @SubscribtionID)
    SET @Result = '0: Subscribtion ID not found'
    ELSE
    BEGIN 
        SELECT TOP 1 @EndDate = [EndDate], @MonthCounts = [SubscriptionMonthCount] FROM [Norton].[dbo].[SubscriptionStatus] WHERE [Reference] = @Reference AND [SubscribtionID] = @SubscribtionID
        IF @EndDate = '0'
        BEGIN 
        UPDATE [Norton].[dbo].[SubscriptionStatus]
        SET [SubscriptionMonthCount] = @Months + @MonthCounts
        WHERE [Reference] = @Reference AND [SubscribtionID] = @SubscribtionID
        END
        ELSE
        BEGIN 
        UPDATE [Norton].[dbo].[SubscriptionStatus]
        SET [SubscriptionMonthCount] = @Months
        WHERE [Reference] = @Reference AND [SubscribtionID] = @SubscribtionID
        END

    SET @Result = '1: Done Successfully'
    END
GO

END
GO

这是我遇到的错误:

消息 102,级别 15,状态 1,过程 sp_RenewSubscription,第 44 行 “END”附近的语法不正确。 消息 102,第 15 级,状态 1,第 2 行 'END' 附近的语法不正确。*

谢谢,

【问题讨论】:

【参考方案1】:

从程序结束前删除go

       END 

    SET @Result = '1: Done Successfully' 
    END 
GO  --- <-- get rid of this

END 

【讨论】:

确实如此。 @Yasser,值得注意的是GO is not actually a SQL statement。它用于分隔 SQL 语句,由客户端工具而非服务器解释。基本上,当您在 Management Studio 等中运行 SQL 页面时,客户端在 GO 语句处将 SQL 分成批处理,并将每个批处理单独发送到服务器以运行,一个接一个。因此,在 SQL 语句的 BEGIN 和 END 对之间的任何地方都有一个 GO 会导致问题。 感谢 podiluska 的回答,它修复了错误,但我得到了另一个错误,靠近第一个 ELSE,幸运的是,Gidil 回答中的代码有效。谢谢马特提供的信息。【参考方案2】:

试试这个:

CREATE PROCEDURE Sp_renewsubscription 
  -- Add the parameters for the stored procedure here 
  @Reference      NVARCHAR(100), 
  @SubscribtionID NVARCHAR(100), 
  @Months         INT, 
  @Result         NVARCHAR(200) output 
AS 
  BEGIN 
      -- SET NOCOUNT ON added to prevent extra result sets from 
      -- interfering with SELECT statements. 
      SET nocount ON; 

      DECLARE @EndDate AS NVARCHAR; 
      DECLARE @MonthCounts AS INT; 

      IF NOT EXISTS(SELECT [reference] 
                    FROM   [Norton].[dbo].[subscriptionstatus] 
                    WHERE  [reference] = @Reference) 
        SET @Result = '0: Reference ID not found' 
      ELSE IF NOT EXISTS(SELECT [reference] 
                    FROM   [Norton].[dbo].[subscriptionstatus] 
                    WHERE  [reference] = @Reference 
                           AND [subscribtionid] = @SubscribtionID) 
        SET @Result = '0: Subscribtion ID not found' 
      ELSE 
        BEGIN 
            SELECT TOP 1 @EndDate = [enddate], 
                         @MonthCounts = [subscriptionmonthcount] 
            FROM   [Norton].[dbo].[subscriptionstatus] 
            WHERE  [reference] = @Reference 
                   AND [subscribtionid] = @SubscribtionID 

            IF @EndDate = '0' 
              UPDATE [Norton].[dbo].[subscriptionstatus] 
              SET    [subscriptionmonthcount] = @Months + @MonthCounts 
              WHERE  [reference] = @Reference 
                     AND [subscribtionid] = @SubscribtionID 
            ELSE 
              UPDATE [Norton].[dbo].[subscriptionstatus] 
              SET    [subscriptionmonthcount] = @Months 
              WHERE  [reference] = @Reference 
                     AND [subscribtionid] = @SubscribtionID 

            SET @Result = '1: Done Successfully' 
        END 
  END 

GO

【讨论】:

以上是关于创建存储过程时出错的主要内容,如果未能解决你的问题,请参考以下文章

在 Oracle 下创建存储过程时出错 - PLS-00103

从另一个存储过程调用 sp_addextendedproperty 时出错

尝试在 phpmyadmin 中执行 MYSQL 存储过程时出错

SQL 存储过程 - 我哪里出错了?

使用输出参数调用oracle存储过程时Azure逻辑应用程序出错

连接到 Tableau 中的存储过程时出错