SQL Server query the data in the database between the first few to several SQL statement writing _ database other

Source: Internet
Author: User
Tags sql server query

Today, when writing a program, you need to generate an SQL statement from the start ID to the end ID. It turns out that you don't need this feature now.

How do you query SQL Server for data SQL statements that are queried between the first few to several in the database?
For example, in SQL Server, how do I query the data SQL statements from 10th through 30 in the database?


------Solution--------------------
Select top * from table where ID in (select Top ID from table order by ID) desc
------Solution--------------------
If there is a unique column that can use LS's


Select Identity (int,1,1) id,* into the temp from table

SELECT * from temp where ID between and 30
------Solution--------------------
Select top * from table where identity field not in (select Top 9 identifies field from table)
------Solution--------------------
1
Select Top * FROM table
where ID not in (select Top ID from table order by ID)
ORDER BY ID

2--should start from 11.
SELECT * FROM table where ID between 30

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.