The examples in this article describe the common methods of SQL Server query for the first N records. Share to everyone for your reference. Specifically as follows:
SQL Server Query the first n records are the operations we often use, the following SQL Server query the first n records of the method is described in detail, if you are interested, may wish to see.
SQL Server queries top N records:
Because IDs may not be contiguous, you cannot use the method of obtaining a 10<id<20 record.
There are three ways to achieve this:
First, search 20 records, specify not including the first 10
Statement:
Copy Code code as follows:
Select top of * from TBL where ID. (select top ID from TBL)
Second, the search record generates temporary tables, and establishes the ID of the temporary table. Obtain the required data by means of a record of the 10<id<20 of the self-increasing ID
Statement:
Copy Code code as follows:
Select Identity (int,1,1) as id,* into #temp from TBL;
SELECT * from #temp where ID between 20
The second method is actually two statements, but you can let him execute it continuously, just like a statement to complete the task.
Third, the friends feel that the first method is too inefficient, after discussion, come to the second method:
Statement:
Copy Code code as follows:
Select Top * "from" (select Top * "Tblorder by id" as Tbl2 order by Tbl2.id DESC
I hope this article will help you with your SQL Server database program.