我試圖想出一個使用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