SQL Server extracts rows from n to m

Source: Internet
Author: User
Code
N to m rows
1. select top m * from tablename where id not in (select top n id from tablename order by id asc/** // * | desc */)
2. select top m * into temporary table (or table variable) from tablename order by columnname -- Insert the top m pen into the temporary table set rowcount n -- only obtain n results select * from Table variable order by columnname desc
3. select top n * from (select top m * from tablename order by columnname) a order by columnname desc
4. if no other identity columns exist in tablename, a sequence is stored in a temporary table. select identity (int) id0, * into # temp from tablename the n to m statements are: select * from # temp where id0> = n and id0 <= m if you report an error when executing the select identity (int) id0, * into # temp from tablename statement, that's because the select into/bulkcopy attribute in the middle of your DB is not open. First Run: exec sp_dboption your DB name, 'select into/bulkcopy', true
5. If the table contains the identity attribute, select * from tablename where identity_col between n and m
6. SQL2005 starts. you can use row_number () over () to generate a row number. with cte as (select id0 = row_number () over (order by id), * from tablename) select * from cte where id between n to m

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.