SQL Server Paging method

Source: Internet
Author: User

/// <summary>///use virtual tables for paged queries that do not apply to queries that explicitly know the column name/// </summary>/// <param name= "SQL" >SQL such as "SELECT * from name where 1=1"</param>/// <param name= "PageIndex" >page number such as "1"</param>/// <param name= "PageSize" >the number of bars, such as "</param>/// <returns>DataTable</returns> Public StaticDataTable selectpaging (String sql,intPageIndex,intpageSize) {    //Defining virtual table names    stringTemporarytable ="#"+randstring (); //Create your virtual tables    stringSqlpaging ="Select Identity (int) as ZZZZZ, * into"+ temporarytable +"From (select TOP Percent * FROM ("+ SQL +") a) a"; //using virtual tables for paged queriesSqlpaging + ="SELECT * from"+ temporarytable +"a WHERE 1=1"+"and a.zzzzz >="+ PageIndex * pageSize +"and a.zzzzz <="+ ((PageIndex +1) * PageSize-1) +" "; //Delete a virtual tableSqlpaging + ="DROP TABLE"+temporarytable; //Execute SQL statement selectcustomsql to execute SQL method you need to define yourselfDataTable dt =Selectcustomsql (sqlpaging); //determines whether the returned result is empty    if(dt! =NULL)    {        //Remove an identity columnDt. Columns.remove ("zzzzz"); }    //return Results    returnDT;}

Other paging methods Baidu on more.

SQL Server Paging method

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.