Question [SQL]: How can I add row numbers to my result set?

Source: Internet
Author: User
Question: How can I add row numbers to my result set?
In database pubs, have a table titles, now I want the result shown as below, each row have a row number, how can you do that?
Result:
Line-No title_id
-------------------
1 bu1032
2 bu1111
3 bu2075
4 bu7832
5 mc2222
6 mc3021
7 mc3026
8 pc1035
9 pc8888
10 pc9999
11 ps1372
12 ps2091
13 ps2106
14 ps3333
15 ps7777
16 tc3218
17 tc4203

18 tc7777

 

Answer:

Sql2005 version: Select Row_number () Over ( Order   By   [ Title_id ] ) As   [ Line-No ] , [ Title_id ]
From   [ Pubs ] . [ DBO ] . [ Titles ]
Order   By   [ Title_id ]

 

SQL2000 version:

Declare   @ Dt   Table (Line_no Int   Identity ( 1 , 1 ), Title_id Varchar ( 6 ))
Insert   Into   @ Dt (Title_id)
Select Title_id From   [ Pubs ] . [ DBO ] . [ Titles ]   Order   By   [ Title_id ]
Select   *   From   @ Dt ;

 

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.