The SQL statement used to query the data between the nth and NTH rows in the database in SQL SERVER.
When writing a program today, You need to generate an SQL statement from the start id to the end id. This function is not required now.
In SQL server, how do I write an SQL statement to query the data between the first and last entries in a database?
For example, in SQL server, how do I write SQL statements that query data between 10th and 30 in a database?
------ Solution --------------------
Select top 20 * from table where id in (select top 30 id from Table order by id) order by id desc
------ Solution --------------------
If a unique Column exists, you can use
Select identity (int, 1, 1) id, * into temp from table
Select * from temp where id between 10 and 30
------ Solution --------------------
Select top 20 * from table where Id field not in (select top 9 Id field from table)
------ Solution --------------------
1
Select top 20 * from table
Where id not in (select top 10 id from Table order by id)
Order by id
2 -- should start from 11
Select * from table where id between 11 and 30