server| Paging
Now generally used in the following 2 ways:
1. Select Top @pagesize * "from table1 where id" (SELECT top @pagesize * (@page-1) ID to table1 order by ID by ID
2. Select * FROM (select top @pagesize * to (select top @pagesize * @page * FROM table1 ORDER BY id DESC) b Order by ID
Which method is better? Try it.
Do two table, each has 10,000 records, a table ID has index, one does not
F
PublicIlistintPageSize,intPageIndex, out intTotalitemcount) { varTotalsql ="SELECT COUNT (1) from UserInfo"; Object result=getscalar (Totalsql); Totalitemcount= result = =NULL?0: Converthandler.toint32 (Result); varsql =string. Format (@"Select U2.N, u.* from UserInfo U, (select TOP {0} row_number () Over (ORDER by Name DESC) N, CODE from UserInfo) u2 WHERE U.code = U2. CODE and U2.N >= {1} ORDER by U2.N ASC", ((@pageIndex-1) * @pageSize) + @pageSize, ((@pageIndex-1) * @pageSize) +1); returnG
Tags: sort a table without bit BER 9.png CAS-Pre A little bit of the online circulation of the general change, even the results of the table query can be returned Create PROC [dbo]. [PageView]
(
@sql nvarchar (max),---The original query statement
@PageIndex int,--page number
@PageSize int,--Number of records per page
@Sort VARCHAR (255),-- Sort fields and rules without adding an order by
@GetCount bit--whether the total number of records is 1 for the total number of records, 0 for the total nu
1 Use [Database name]2 GO3 4 SETAnsi_nulls on5 GO6 7 SETQuoted_identifier on8 GO9 CREATE PROCEDURE [dbo].[Stored Procedure name]Ten @pageIndex INT, One @pageSize INT, A @totalCount INTOUTPUT - as - SETNOCOUNT on the - DECLARE @PageStart INT - DECLARE @PageEnd INT - + SET @pageStart=(@pageIndex-1)*@pageSize - SET @pageEnd=@pageIndex*@pageSize + BEGIN A SELECT @totalCount=COUNT(1) from [dbo].[Table name] at SELECT - [Id] - , Field 1 - , Field 2 - ,
Method 1:Applies to SQL Server 2000/2005SELECT TOP Page Size *From table1WHERE ID not in ( SELECT TOP Page Size * (pages-1) ID from table1 ORDER by ID )ORDER by IDMethod 2:Applies to SQL Server 2000/2005SELECT TOP Page Size *From table1WHERE ID > ( ( SELECT TOP Page Size * (pages-1) ID from table1 ORDER by ID
Label:Writing:Pretend to have a table shop, which has a list of shopname, taking 100000 to 100,050 data. The writing of Row_number SELECT * from
(SELECT Over (ORDER by Shopname) as R
from Shop ) T
WHERE R>100000 and R100050 The writing of Offset,fetch SELECT from Shop
ORDER by Shopname
OFFSET 100000 ROW
FETCHNEXT Comparison:The difference in execution time can be clearly seen when 100,000 data are taken. Although the previous people have given a lot of arguments, I will stick to th
Use [Reportservertempdb]gocreate PROCEDURE [dbo]. [Seachtablepage] (@TableName varchar,--table name @fileds varchar,--query field @orderfiled varchar,--sort field @isdesc BIT,-- Whether to sort in descending order @wherestring VARCHAR (2000),--query field @pageindex int,--current page @pagesize int,--number of bars per page @totalrecord int output--return total number of bars) ASBE Gindeclare @OrderString VARCHAR (@PageIndex is NULL OR @PageIndex SQL Server
server| Paging
Select ThreadID from(Select ThreadID, Row_number () over (order by stickydate) as Pos from Cs_threads) as Twhere T.pos > 100000 and T.pos
===========================================
If the table inside the Cs_threads data volume, for example, hundreds of millions of records, then this method should be problematic
Because, select ThreadID from(Select ThreadID, Row_number () over (order by
Paging query is very simple, the specific code is as follows:--Paged Query--query 1-3 rows of data Select Top 3 * from emp ORDER by Sal desc;--query 4-6 rows Data Select Top 3 *from empwhere empno not in (select Top 3 empno from emp order BY Sal Desc) Order by Sal desc;--query 7-9 rows of data select Top 3 *from empwhere empno not in (select top 6
empno from emp ORDER BY Sal Desc) Order by Sal desc;--query 10-12 rows of data select Top 3 *from empw
Tags: code type logs page Ges char style arch nbsp Alter procProc_getpage@tbname varchar( -),
@field varchar( $)='*',
@orderfield varchar( -),
@ordertype Char( -)='ASC',
@pagesize int ,
@pageindex int,
@strWhere varchar( -)="'
as
begin
Declare @sql varchar(Max)
if(@strWhere"')
Set @strWhere='where'+@strWhere
Set @sql='SELECT * FROM (select Row_number () over (order by'+@orderfield+' '+@ordertype+') as POS,'+@field+' from'+@tbname+' '+@strWhere+') as T where Pos between'+Str((@pageindex-1)*@pag
Tags: ar data sp div Art C on R BSScenario one (assuming the user only browses to the previous dozens of pages):Idea: Take out the page size * Number of pages of data, and then get to Intstartindex and Intendindex direct data; Advantages: Simple writing, universal, suitable for users to only browse the first few pages of the situation disadvantage: if the amount of data is more than tens of millions, read the last few pages will be very slow. SQL Server
Tags: des style color ar using SP data on BS--How to use: Execute in a stored procedure that gets a list of queries, and specify parameters on the line--exec listpage @SQL, @PageSize, @PageNo, @OrderStr, @OrderType--@SQL query statement--Number of @PageSize data--@PageNo Current page--@OrderStr sort field--@OrderType sort 0 ASC 1 DESCUse [DataBaseName]--Database nameGOSET ANSI_NULLS onGOSET QUOTED_IDENTIFIER ONGOCREATE PROCEDURE Listpage@InSQL varchar (max),@RowNum int,@PageNo int,@IndexWord var
IF EXISTS (SELECT * from SYSOBJECTS WHERE NAME = ' stuwebname ') DROP table stuwebnamegocreate table Stuwebname (stuid INT IDEN Tity (+) PRIMARY key,stuname varchar (2) not null,stupassword varchar (+) not null,stusex CHAR ("male"), Stuno INT Not NULL) godeclare @ID intset @ID =1while (@ID SQL Server Three paging methods
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.