SQL query traversal data method one [temporal table + while loop]

Source: Internet
Author: User

The following is an example of the Customers table in the Northwind database in SQL Server 2000.

CompanyName columns in the Customers table are traversed with the temporary table + while loop method

CREATE TABLE #temp
(
ID int identity (+),
Customer nvarchar (50)
)


declare @customer nvarchar (50)
DECLARE @n int
DECLARE @rows int

Select @n=1

Insert #temp (Customer) SELECT distinct CompanyName from Customers

Select @rows = @ @rowcount

While @n <= @rows
Begin


Select @customer = CompanyName
From customers
Where Companyname= (select customer from #temp where id = @n)
ORDER BY CompanyName Desc

Print (@customer)

Select @n = @n + 1

End


After running, the output is as follows:


(The number of rows affected is 91 rows)

Alfreds Futterkiste
Ana Trujillo Emparedados y helados
Antonio Moreno Taquería
Around the Horn
Berglunds snabbk?p
Blauer See Delikatessen
BLONDESDDSL père et fils
Bólido Comidas Preparadas
Bon app '
Bottom-dollar
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? 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
...... (The following is a bit) ....... ................

SQL query traversal data method one [temporal table + while loop]

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.