Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 将varchar值“my returned value”转换为数据类型int时,转换失败_Sql_Sql Server_Sql Server 2008 - Fatal编程技术网

Sql 将varchar值“my returned value”转换为数据类型int时,转换失败

Sql 将varchar值“my returned value”转换为数据类型int时,转换失败,sql,sql-server,sql-server-2008,Sql,Sql Server,Sql Server 2008,在尝试从表中检索字符串时,我在这个简单的SQL语句中遇到了这个错误 Msg 245,16级,状态1,程序要求,第18行 将varchar值“医生心脏评估问卷”转换为数据类型int时,转换失败 /****** Object: StoredProcedure [dbo].[prViewRequirements] Script Date: 04/24/2013 15:44:49 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO AL

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

Msg 245,16级,状态1,程序要求,第18行 将varchar值“医生心脏评估问卷”转换为数据类型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 

有人知道为什么吗?

您正在尝试返回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 
*/
试试这个-

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
试试这个:

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
执行如下所示的过程

DECLARE @res varchar(100)
exec dbo.prViewRequirements @WFRouteID,@DocumentDescription=@res OUTPUT
select @res

哪一行是第18行?第18行是-其中WFRouteID=@WFRouteID不断为每个结果返回NULL。这对我来说很有效。请仔细复制并粘贴关键字输出。