【问题标题】:How to return a value (by setting a variable) in dynamic SQL? [duplicate]如何在动态 SQL 中返回一个值(通过设置一个变量)? [复制]
【发布时间】:2016-10-27 16:53:41
【问题描述】:
DECLARE @sqlCommand nvarchar(1000)

DECLARE @countnumber int = 0

SET @sqlCommand = 'Set @countnumber = (SELECT count(*) from dsnv)'

EXEC (@sqlCommand)

print @countnumber ;

但我得到错误

【问题讨论】:

    标签: sql sql-server


    【解决方案1】:

    将 sp_executesql 与输出参数一起使用

    DECLARE @countnumber int = 0
    
    SET @sqlCommand = 'Set @countnumber = (SELECT count(*) from dsnv)'
    
    exec sp_executesql @sqlCommand, N'@countnumber int OUTPUT', @countnumber OUTPUT
    
    print @countnumber ;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-02
      • 1970-01-01
      • 2019-01-12
      • 2010-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-08
      相关资源
      最近更新 更多