如下以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...... (如下略) ..................................