1

尝试从表中检索字符串时,我在这个简单的 SQL 语句中收到此错误。

消息 245,级别 16,状态 1,过程 prViewRequirements,第 18 行转换 varchar 值“Dr. 的心脏评估问卷”时转换失败 为数据类型 int。

/****** Object:  StoredProcedure [dbo].[prViewRequirements]   Script Date: 04/24/2013 15:44:49 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[prViewRequirements]
    @WFRouteID int

AS

DECLARE 
@DocumentDescription VARCHAR(100)

SELECT @DocumentDescription = DocumentDescription
            FROM tbFollowOnTracking
            WHERE WFRouteID = @WFRouteID
            AND IsActive = 1


            IF (@@ERROR <> 0)
                GOTO ERRSP      

        RETURN @DocumentDescription

ERRSP:
    RETURN -1 

有谁知道为什么?

4

3 回答 3

3

您正在尝试返回 varchar 而不是 int。

RETURN @DocumentDescription

请做

select @DocumentDescription

或使用输出参数(推荐)

ALTER PROCEDURE [dbo].[prViewRequirements]
    @WFRouteID int
    , @DocumentDescription varchar(100) = null output

更新 - 这是整个过程:

alter procedure dbo.prViewRequirements
    @WFRouteID int
    , @DocumentDescription varchar(100) = null output
AS

select @DocumentDescription = '' -- Init

select @DocumentDescription = DocumentDescription
from tbFollowOnTracking
where WFRouteID = @WFRouteID
and IsActive = 1

return 0
go

/* Examples
declare @DocumentDescription varchar(100) = ''

exec dbo.prViewRequirements @WFRouteID = 10, @DocumentDescription  = @DocumentDescription output

select @DocumentDescription 
*/
于 2013-04-29T06:11:58.293 回答
0

尝试这个:

alter procedure dbo.prViewRequirements
    @WFRouteID int
    , @DocumentDescription varchar(100) = null output
AS

BEGIN
select @DocumentDescription = '' -- Init

select @DocumentDescription = DocumentDescription
from tbFollowOnTracking
where WFRouteID = @WFRouteID
and IsActive = 1

END

执行如下所示的proc

DECLARE @res varchar(100)
exec dbo.prViewRequirements @WFRouteID,@DocumentDescription=@res OUTPUT
select @res
于 2013-04-29T06:29:59.827 回答
0

试试这个——

ALTER PROCEDURE [dbo].[prViewRequirements]

      @WFRouteID INT
    , @DocumentDescription VARCHAR(100) OUTPUT

AS BEGIN

    SELECT @DocumentDescription = t.DocumentDescription
    FROM dbo.tbFollowOnTracking t
    WHERE t.WFRouteID = @WFRouteID
        AND t.IsActive = 1

    IF @DocumentDescription IS NULL
        RETURN -1    

    RETURN 0

END
于 2013-04-29T06:13:48.050 回答