Sql 创建存储过程时出错
我是存储过程的noob,我创建了一个存储过程,从C页面获取一些参数,然后将结果作为输出参数发送回 我需要做一些计算来得到结束日期,所以我最终使用了很多IF语句。然而,当我创建存储过程时,我得到了一个我不知道如何解决的错误,所有的事情似乎都是正确的 以下是存储过程代码:Sql 创建存储过程时出错,sql,sql-server-2008,stored-procedures,Sql,Sql Server 2008,Stored Procedures,我是存储过程的noob,我创建了一个存储过程,从C页面获取一些参数,然后将结果作为输出参数发送回 我需要做一些计算来得到结束日期,所以我最终使用了很多IF语句。然而,当我创建存储过程时,我得到了一个我不知道如何解决的错误,所有的事情似乎都是正确的 以下是存储过程代码: CREATE PROCEDURE sp_RenewSubscription -- Add the parameters for the stored procedure here @Ref
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
这是我得到的错误:
Msg 102,15级,状态1,程序sp_,第44行
靠近“END”的语法不正确。
Msg 102,15级,状态1,第2行
靠近“END”的语法不正确*
谢谢,在程序结束前从中取出go
即
在程序结束前,从中取出go
即
试试这个:
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
试试这个:
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
的确如此@Yasser,值得注意的是。它用于分隔SQL语句,并由客户机工具而不是服务器进行解释。基本上,当您在ManagementStudio等中运行SQL页面时,客户机会在GO语句中将SQL分为多个批,并将每个批分别发送到要运行的服务器。因此,在SQL语句的开始和结束对之间的任意位置都会引起问题。感谢podiluska的回答,它修复了错误,但我得到了另一个错误,接近第一个错误,幸运的是,Gidil answer中的代码工作正常。谢谢马特提供的信息,真的@Yasser,值得注意的是。它用于分隔SQL语句,并由客户机工具而不是服务器进行解释。基本上,当您在ManagementStudio等中运行SQL页面时,客户机会在GO语句中将SQL分为多个批,并将每个批分别发送到要运行的服务器。因此,在SQL语句的开始和结束对之间的任意位置都会引起问题。感谢podiluska的回答,它修复了错误,但我得到了另一个错误,接近第一个错误,幸运的是,Gidil answer中的代码工作正常。谢谢马特提供的信息。