SQL Proc“转换失败”从varchar到int。为什么要转换?

时间:2019-01-25 17:43:20

标签: sql-server tsql procedure

我的问题是...为什么将其从varchar转换为int?我不确定它要做什么

CREATE PROCEDURE #myTestProcedure 
(
    @TransId VARCHAR(15)
) 
AS
BEGIN
    DECLARE @Result VARCHAR(15);

WITH TestCTE (TransId, AdjRefTransId) AS
(
    SELECT TRANSID, ADJREFTRANSID
    FROM dbo.MyTable
    WHERE TRANSID = @TransId

    UNION ALL

    SELECT pet.TRANSID, pet.ADJREFTRANSID
    FROM dbo.MyTable AS pet
    JOIN TestCTE
        ON TestCTE.ADJREFTRANSID = pet.TRANSID
)

SELECT @Result = 
(
    SELECT MAX(MyResult)
    FROM dbo.MyOtherTable
    WHERE TRANSID = TestCTE.TRANSID
)        
FROM TestCTE
WHERE TestCTE.ADJREFTRANSID = ''

RETURN @Result
END

EXEC dbo.#myTestProcedure @TransId = 'MyTransId'

错误:

Msg 245, Level 16, State 1, Procedure #myTestProcedure  0004C61A, Line 32
Conversion failed when converting the varchar value 'MyResult' to data type int.

我看不到它在哪里进行转换。第32行是空白行。那里没有代码。

1 个答案:

答案 0 :(得分:7)

这是您的退货。存储过程返回一个整数来指示执行状态,而不返回值。您可能需要选择@Result或将@Result作为输出参数。