Conversion failed when converting the varchar value 'my returned value' to data type int

前端 未结 3 1321
终归单人心
终归单人心 2021-01-27 00:41

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

相关标签:
3条回答
  • 2021-01-27 01:02

    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
    
    0 讨论(0)
  • 2021-01-27 01:13

    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 
    */
    
    0 讨论(0)
  • 2021-01-27 01:14

    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
    
    0 讨论(0)
提交回复
热议问题