【发布时间】:2010-12-25 16:56:26
【问题描述】:
我正在尝试使用 Microsoft SQL Server 提出一个简单的 while 循环示例。 在这里,我遍历所有以 temp 开头的表。
Declare @Object_ID int,@Name varchar(50)
set @Object_ID = 0
while exists (
select * from
sys.tables
where type = 'u'
and object_ID > @Object_ID
and Name like 'Temp%'
) BEGIN
select top 1 @Object_ID=Object_ID,@Name=Name
from sys.tables
where type = 'u'
and object_ID > @Object_ID
and Name like 'Temp%'
order by Object_ID
exec('Select ''' + @Name + ''' as TableName,count(*) AS Counter from ' + @Name)
END
我的问题是:既然我已经遍历了表格,我该如何使用我通过 exec 命令收集的信息? 换句话说,我可以将 exec 命令返回的表存储到变量中吗?
【问题讨论】:
标签: sql-server while-loop exec