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