SQL Server:stored proc尝试将varchar OUTPUT转换为int @Return_Value

时间:2015-10-26 18:22:06

标签: sql-server stored-procedures

我有这个存储过程:

create proc Sponsors.GetLightBoxAd
(
@SponsorID varchar(30),
@ADIDOut varchar(30) OUTPUT,
@UserID varchar(30),
@ProjectID varchar(50),
@PlatformID int
)
as 
begin
  SELECT TOP 1 @ADIDOut = AD.ADID --my output. AD.ADID is a varchar(30) column
  FROM Sponsors.AD
  WHERE AD.Active = 1 and AD.SponsorID = @SponsorID
  ORDER by NEWID() -- I want the first one picked at random
  IF ISNULL(@ADIDOut,-1) != -1 --if the result set wasn't null, run some update queries with the result
  BEGIN --These queries do not have output variables.
    EXEC Sponsors.proc1 @ADIDOut, @SponsorID
    EXEC Projects.proc2 @ProjectID,@ADIDOut,@UserID,@PlatformID 
  END --end the if
end --end the proc
go

这应该返回null值或varchar。 但是,当我尝试执行查询时,SSMS会自动生成此代码以供我运行查询:

USE [MyDB]
GO

DECLARE @return_value int, --Why is this here???
        @ADIDOut varchar(30) -- this should be my only output

EXEC    @return_value = [Sponsors].[GetLightBoxAd]
        @SponsorID = N'Alienware',
        @ADIDOut = @ADIDOut OUTPUT,
        @UserID = N'127.0.0.1',
        @ProjectID = N'TestProject',
        @PlatformID = 1

SELECT  @ADIDOut as N'@ADIDOut'

SELECT  'Return Value' = @return_value --why is this here?

GO

然后它给了我一个错误,因为它似乎试图将ADIDOut的结果转换为int ...为什么有一个int @Return_Value变量,为什么它试图将我的实际OUTPUT结果放入呢? / p>

1 个答案:

答案 0 :(得分:0)

错误在这一行:

IF ISNULL(@ADIDOut,-1) != -1 

似乎因为我只将-1没有引号作为比较,sql将其读作整数并且无法将其与可空字符串进行比较,因此会引发错误。将-1&#s放入引号可以修复错误并运行查询。谢谢你的帮助!