To build a web application, the pagination browsing function is essential. This problem is very common in database processing. In fact, this pagination method is only applicable to a small amount of data. Let’s follow the editor of the Wrong New Technology Channel to learn more!
Source of ideas:
Take out records from nth to mth from publish table:
Select TOP m-n+1 *
FROM publish
Where (id NOT IN
(Select TOP n-1 id
FROM publish))
The following is the stored procedure:
The code copy is as follows:Create PROCEDURE pagination3
@tblName varchar(255) , -- table name
@strGetFields varchar(1000) = '*', -- columns to be returned
@fldName varchar(255)='', -- Sort field name
@PageSize int = 10, -- Page size
@PageIndex int = 1, -- Page number
@doCount bit = 0, -- Returns the total number of records, returns if a value is not 0
@OrderType bit = 0, -- Set the sort type, and if the value is not 0, descending order
@strWhere varchar(1500) = '' -- Query conditions (Note: do not add where)
AS
declare @strSQL varchar(5000) -- Subject statement
declare @strTmp varchar(110) -- Temporary variable
declare @strOrder varchar(400) -- sort type
if @doCount != 0
Begin
if @strWhere !=''
set @strSQL = 'select count(*) as Total from [' + @tblName + '] where '+@strWhere
else
set @strSQL = 'select count(*) as Total from [' + @tblName + '] '
end
--The above code means that if the @doCount passes over is not 0, the total count will be executed. All the following codes are case where @doCount is 0:
else
Begin
if @OrderType != 0
Begin
set @strTmp = '<(select min'
set @strOrder = ' order by [' + @fldName +'] desc'
--If @OrderType is not 0, then perform descending order. This sentence is very important!
end
else
Begin
set @strTmp = '>(select max'
set @strOrder = ' order by [' + @fldName +'] asc'
end
if @PageIndex = 1
Begin
if @strWhere != ''
set @strSQL = 'select top ' + str(@PageSize) +' '+@strGetFields+ ' from [' + @tblName + '] where ' + @strWhere + ' ' + @strOrder
else
set @strSQL = 'select top ' + str(@PageSize) +' '+@strGetFields+ ' from ['+ @tblName + '] '+ @strOrder
--If it is the first page, execute the above code, which will speed up the execution speed
end
else
Begin
set @strSQL = 'select top ' + str(@PageSize) +' '+@strGetFields+ ' from ['
+ @tblName + '] where [' + @fldName + ']' + @strTmp + '(['
+ @fldName + ']) from (select top ' + str((@PageIndex-1)*@PageSize) + ' ['
+ @fldName + '] from [' + @tblName + ']' + @strOrder + ') as tblTmp)'
+ @strOrder
if @strWhere != ''
set @strSQL = 'select top ' + str(@PageSize) +' '+@strGetFields+ ' from ['
+ @tblName + '] where [' + @fldName + ']' + @strTmp + '(['
+ @fldName + ']) from (select top ' + str((@PageIndex-1)*@PageSize) + ' ['
+ @fldName + '] from [' + @tblName + '] where ' + @strWhere + ' '
+ @strOrder + ') as tblTmp) and ' + @strWhere + ' ' + @strOrder
end
end
exec (@strSQL)
--print @strSQL
GO
Through the content introduced by the editor of the 未分New Technology Channel, I believe everyone has a certain understanding. If you want to know more technical content, please continue to pay attention to the 未分New Technology Channel!