Conversion failed when converting the varchar valu

2020-04-20 03:22发布

I get this error in this simple SQL statement when trying to retrieve a string from a table.

Msg 245, Level 16, State 1, Procedure prViewRequirements, Line 18 Conversion failed when converting the varchar value 'Cardiac Assessment Questionnaire by Dr.' to data type 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 

Does anyone know why?

3条回答
▲ chillily
2楼-- · 2020-04-20 03:40

Try this one -

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
查看更多
Fickle 薄情
3楼-- · 2020-04-20 03:42

You are trying to return a varchar instead of int.

RETURN @DocumentDescription

Please either do

select @DocumentDescription

or use an output parameter (Recommended)

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

UPDATE - Here is the whole procedure:

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 
*/
查看更多
Deceive 欺骗
4楼-- · 2020-04-20 03:47

Try This:

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

Execute the proc as shown below

DECLARE @res varchar(100)
exec dbo.prViewRequirements @WFRouteID,@DocumentDescription=@res OUTPUT
select @res
查看更多
登录 后发表回答