Mssql implements mysqllimitsql statements

Source: Internet
Author: User
Mssql implements limitsql statements in mysql. You can use limit to query records from column m to column n. For example, select * fromtablenamelimitm, n. However, in SQL Server, the limit statement is not supported. What should we do? Solution: Although sqlserver does not support limit, it supports top. We use sqlserver2005 as mssql to implement limit SQL statements
In mysql, you can use limit to query records from column m to column n. For example:

Select * from tablename limit m, n

However, SQL server does not support limit statements. What should we do?

Solution:

Although SQL server does not support limit, it supports top.

Taking SQL server 2005 as an example, we use its own demo adventureworks as the test data:

Select addressid, city, stateprovinceid, postalcode
From person. [address]
Where stateprovinceid = 58
Order by addressid

If you want to query the first six records in the preceding results, the corresponding SQL statement is:

Select top 6 addressid, city, stateprovinceid, postalcode
From person. [address]
Where stateprovinceid = 58
Order by addressid

If you want to query 7th to 9th records in the preceding results, the corresponding SQL statement is:

Select top 3 addressid, city, stateprovinceid, postalcode
From person. [address]
Where stateprovinceid = 58
And addressid not in (
Select top 6 addressid from person. [address] where stateprovinceid = 58 order by addressid
)
Order by addressid

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.