EXEC执行动态SQL时取出变量值[EXEC函数只能是局部变量]

-- EXEC 输出变量值

DECLARE   @RowCount      int
DECLARE   @sqlstr          nvarchar ( 4000
DECLARE   @tableName      nvarchar ( 100 )

-- 表名,应用可能有更复杂的参数需要连接
SET   @tableName = ' TEST '

SET      @sqlstr = ' SELECT @iRowCount=COUNT(*) FROM  ' +   @tableName   
EXEC  sp_executesql  @sqlstr ,N ' @iRowCount int output ' , @RowCount  output     
SELECT   @RowCount   

你可能感兴趣的:(动态sql)