我试图想出一个使用Microsoft SQL Server的while循环的简单示例。 在这里我循环了所有以temp开头的表格。虽然在Microsoft SQL Server 2008中循环
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都属于你。 – 2009-12-11 16:50:03