以下以SQL Server 2000中的NorthWind數據庫中的Customers表為例,
用 游標(Cursor) + While循環 的方法, 對Customers表中的CompanyName列進行遍歷
declare @customer nvarchar(50)
declare pcurr cursor for
select distinct companyname from customers
open pcurr
fetch next from pcurr into @customer
while (@@fetch_status = 0)
begin
print (@customer)
fetch next from pcurr into @customer
end
close pcurr
deallocate pcurr
運行後, 輸出結果如下:
(所影響的行數為 91 行)
Alfreds Futterkiste
Ana Trujillo Emparedados y helaDOS
Antonio Moreno Taquería
Around the Horn
Berglunds snabbk&oUML;p
Blauer See Delikatessen
Blondesddsl père et fils
Bólido Comidas preparadas
Bon app'
Bottom-Dollar Markets
B's Beverages
Cactus Comidas para llevar
Centro comercial Moctezuma
Chop-suey Chinese
Comércio Mineiro
Consolidated Holdings
DIE Wandernde Kuh
Drachenblut Delikatessen
Du monde entIEr
Eastern Connection
Ernst Handel
Familia Arquibaldo
FISSA Fabrica Inter. Salchichas S.A.
FolIEs gourmandes
Folk och f&aUML; HB
France restauration
Franchi S.p.A.
Frankenversand
Furia Bacalhau e Frutos do Mar
Galería del gastrónomo
GoDOS Cocina Típica
Gourmet Lanchonetes
Great Lakes Food Market
GROSELLA-Restaurante
...... (以下略) ..................................