我是存储过程的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