【问题标题】:Assign Stored Procedure's OUTPUT param within dynamic query在动态查询中分配存储过程的 OUTPUT 参数
【发布时间】:2020-07-13 22:48:23
【问题描述】:

关于这个存储过程:

CREATE PROCEDURE Test
    (@outparam int OUTPUT)
AS
BEGIN
    DECLARE @SQL VARCHAR(1000)

    SET @SQL = '
    DECLARE @outparam int
    IF (1=0)
        PRINT ''do something here. I use dynamic stuff in IF and in here ''
    ELSE IF (1=0)
        PRINT ''do something here. I use dynamic stuff in IF and in here ''
    ELSE if (1=1)
    BEGIN
        SET @outparam = 5
    END
    '

    EXEC (@SQL)
END

我需要在动态查询中分配@outparam,因为所有逻辑都在其中。

它不起作用。我明白为什么,但我不知道如何解决它

DECLARE @out int
EXEC Test @outparam = @out OUTPUT

SELECT @out

【问题讨论】:

标签: sql-server tsql sql-server-2012


【解决方案1】:

感谢@DaleK 这有效

CREATE procedure Test

(
    @outparam int OUTPUT
)
as

BEGIN
    DECLARE @SQL NVARCHAR(1000)

SET @SQL = '
IF (1=0)
    print ''do something here. i use dynamic stuff in IF and in here ''
ELSE IF (1=0)
    print ''do something here. i use dynamic stuff in IF and in here ''
ELSE if (1=1)
BEGIN
    SET @outparam = 5
END
'

EXEC sp_executesql @sql,
N'@outparam VARCHAR(MAX) OUTPUT',
@outparam OUTPUT;
END

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    • 2014-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-11
    相关资源
    最近更新 更多