码迷,mamicode.com
首页 > 数据库 > 详细

T-SQL 将动态SQL的结果集赋值到变量

时间:2017-01-04 10:54:06      阅读:241      评论:0      收藏:0      [点我收藏+]

标签:sql   customer   select   int   where   动态sql   set   arch   count   

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的结果集赋值到变量

标签:sql   customer   select   int   where   动态sql   set   arch   count   

原文地址:http://www.cnblogs.com/bi-info/p/6222681.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!