T-SQL 将动态SQL的结果集赋值到变量
Posted BI Information
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了T-SQL 将动态SQL的结果集赋值到变量相关的知识,希望对你有一定的参考价值。
1. 使用输出变量
DECLARE @sqlCommand nvarchar(1000) DECLARE @city varchar(75) DECLARE @counts int SET @city = ‘New York‘ SET @sqlCommand = ‘SELECT @cnt=COUNT(*) FROM customers WHERE City = @city‘ EXECUTE sp_executesql @sqlCommand, N‘@city nvarchar(75),@cnt int OUTPUT‘, @city = @city, @cnt=@counts OUTPUT SELECT @counts as Counts
2. 使用临时表
DECLARE @sqlCommand nvarchar(1000) DECLARE @city varchar(75) SET @city = ‘New York‘ SET @sqlCommand = ‘SELECT COUNT(*) as count into #temp FROM customers WHERE City = @city‘ EXECUTE sp_executesql @sqlCommand, N‘@city nvarchar(75),@cnt int OUTPUT‘, @city = @city SELECT @counts = count from #temp
以上是关于T-SQL 将动态SQL的结果集赋值到变量的主要内容,如果未能解决你的问题,请参考以下文章