2012-10-17 80 views
1

我是存儲過程的noob,我創建了一個存儲過程,從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行
附近有語法錯誤結束'。
消息102,級別15,狀態1,行2
附近有語法錯誤 'END'。*

謝謝,

回答

4

從程序結束之前,取出go

 END 

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

END 
+3

的確。 @Yasser,值得注意的是[GO實際上不是SQL語句](http://msdn.microsoft.com/en-us/library/ms188037.aspx)。它用於*分離* SQL語句,並由客戶端工具解釋,而不是服務器。基本上,當您在Management Studio等處運行SQL頁面時,客戶端將GO中的SQL分解爲批處理,並將每個批次分別發送到服務器以便一個接一個地運行。因此,在SQL語句的BEGIN和END對之間的任何位置都會導致出現問題。 –

+0

感謝podiluska爲您的答案,它修復了錯誤,但我得到了另一個,接近第一個ELSE,幸運的是,在Gidil答案中的代碼工作。 謝謝Matt提供的信息。 – Yasser

1

試試這個:

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 
+0

感謝Gidil,代碼完美運行。 – Yasser