我是存儲過程的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'。*
謝謝,
的確。 @Yasser,值得注意的是[GO實際上不是SQL語句](http://msdn.microsoft.com/en-us/library/ms188037.aspx)。它用於*分離* SQL語句,並由客戶端工具解釋,而不是服務器。基本上,當您在Management Studio等處運行SQL頁面時,客戶端將GO中的SQL分解爲批處理,並將每個批次分別發送到服務器以便一個接一個地運行。因此,在SQL語句的BEGIN和END對之間的任何位置都會導致出現問題。 –
感謝podiluska爲您的答案,它修復了錯誤,但我得到了另一個,接近第一個ELSE,幸運的是,在Gidil答案中的代碼工作。 謝謝Matt提供的信息。 – Yasser