如下以SQL Server 2000中的NorthWind數據庫中的Customers表爲例,數據庫
用 臨時表 + While循環 的方法, 對Customers表中的CompanyName列進行遍歷app
create table #temp
(
id int identity(1,1),
customer nvarchar(50)
)ide
declare @customer nvarchar(50)
declare @n int
declare @rows intui
select @n=1rest
insert #temp(customer) select distinct companyname from customersci
select @rows = @@rowcountit
while @n <= @rows
beginio
select @customer = companyname
from customers
where companyname=(select customer from #temp where id = @n)
order by companyname desctable
print(@customer)ast
select @n = @n + 1
end
運行後, 輸出結果以下:
(所影響的行數爲 91 行)
Alfreds FutterkisteAna Trujillo Emparedados y heladosAntonio Moreno TaqueríaAround the HornBerglunds snabbköpBlauer See DelikatessenBlondesddsl père et filsBólido Comidas preparadasBon app'Bottom-Dollar MarketsB's BeveragesCactus Comidas para llevarCentro comercial MoctezumaChop-suey ChineseComércio MineiroConsolidated HoldingsDie Wandernde KuhDrachenblut DelikatessenDu monde entierEastern ConnectionErnst HandelFamilia ArquibaldoFISSA Fabrica Inter. Salchichas S.A.Folies gourmandesFolk och fä HBFrance restaurationFranchi S.p.A.FrankenversandFuria Bacalhau e Frutos do MarGalería del gastrónomoGodos Cocina TípicaGourmet LanchonetesGreat Lakes Food MarketGROSELLA-Restaurante...... (如下略) ..................................