How to implement the functionality of Limit m,n in SQL Server _mysql

Source: Internet
Author: User
In MySQL, you can use Limit to query the records in columns m to n columns, for example:
Copy Code code as follows:

SELECT * FROM tablename limit m, n

But Limit statements are not supported in SQL Server. What do we do?
Solution:
Although SQL Server does not support Limit, it supports top.
Let's take SQL Server 2005, for example, to test data with its own demo database AdventureWorks:
Copy Code code as follows:

Select ID from tablename

if you want to query the first 6 records in the above results, the corresponding SQL statement is:
Copy Code code as follows:

Select Top 6 IDs from TableName

If you want to query 7th through 9th Records in the results above, the corresponding SQL statement is:
Copy Code code as follows:

Select top 3 IDs from TableName
where ID not in (
Select Top 6 IDs from TableName
)

Copy Code code as follows:

Select Top (n-m+1) ID from tablename
where ID not in (
Select top M-1 IDs from TableName
)

Copy Code code as follows:

Select top @pageSize ID from tablename
where ID not in (
Select top @offset ID from tablename
)

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.