Use the row_number () over function to assign line numbers to each record in the SQL database _mssql

Source: Internet
Author: User

Starting with SQL Server 2005, adding a new function row_number (), one of his great role is that you can add a column in the datasheet from the beginning of the 1 line number, which greatly replaces all the extra code to produce line numbers. Let's teach you how to use the Row_number () function.

Suppose the database has the following table:

Copy Code code as follows:

ID Name
1 AA
3 BB
4 cc

After executing the following SQL statement, you will get the following result:

SELECT row_number () over (order by ID) as rownum,* from table name

Copy Code code as follows:

RowNum ID Name
1 1 AA
2 3 BB
3 4 cc

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.