|
/*
Function description: General paging display query
Condition: There is no field indicating increment in the table
Input parameters:
@tblName: table name
@strGetFields: need to return the column'*': return all column information
@fldName: sorted field name
@OrderType: Set the sort type, non-zero value is descending
@PageSize: page size
@PageIndex: page number
@doCount: Returns the total number of records, non-zero value is returned
@strOrderBy: Default sort field information (Note: Do not add ORDER BY)
Format: Field1 DESC, Field2 ASC,
@strWhere: query conditions (note: do not add WHERE)
Output parameters: @RecordCount: total number of records
Author: ningfeiyang
Created: 2015-01-19
Change record:
*/
ALTER PROCEDURE Pagination2
(
@tblName varchar(255),
@strGetFields varchar(1000) ='*',
@fldName varchar(255) ='',
@PageSize int = 10,
@PageIndex int = 1,
@doCount bit = 0,
@OrderType bit = 0,
@strOrderBy varchar(500) ='',
@strWhere varchar(1500) ='',
@RecordCount int output
)
AS
DECLARE @strSQL varchar(5000) - main statement
DECLARE @strTmp varchar(200) - Temporary variable
DECLARE @strOrder varchar(400) - sort variable
--If @doCount is not 0, perform total statistics
IF (@doCount != 0)
BEGIN
DECLARE @sWhere varchar(2000)
SET @sWhere =''
IF (@strWhere !='')
SET @sWhere = 'WHERE' + @strWhere
SET @strSQL ='if exists (select * from dbo.sysobjects where id = object_id(``[dbo].[tmpTable]'') and OBJECTPROPERTY(id,''IsUserTable'') = 1) '
SET @strSQL = @strSQL + 'UPDATE tmpTable SET Total = (SELECT COUNT(*) FROM ['+ @tblName +']' + @sWhere +') '
SET @strSQL = @strSQL + 'ELSE SELECT COUNT(*) AS Total INTO tmpTable FROM ['+ @tblName +']' + @sWhere
EXEC (@strSQL)
SELECT @RecordCount=Total FROM tmpTable
-Delete the total statistics temporary table
EXEC ('DROP TABLE tmpTable')
END
--PRINT @RecordCount
--If @OrderType is not 0, perform descending order
IF (@OrderType != 0)
BEGIN
SET @strTmp = '<(SELECT MIN'
SET @strOrder = 'ORDER BY' + @strOrderBy +'[' + @fldName +'] DESC'
END
ELSE
BEGIN
SET @strTmp = '> (SELECT MAX'
SET @strOrder = 'ORDER BY' + @strOrderBy +'[' + @fldName +'] ASC'
END
--If it is the first page, execute the above code, which will speed up execution
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
END
ELSE
BEGIN
--Create automatic number for search table and save to temporary table
SET @strSQL ='SELECT TOP '+ str(@PageIndex*@PageSize) +' IDENTITY(int,1,1) AS IID, '+ @strGetFields +' INTO #tmpTable FROM ['+ @tblName +']'
IF @strWhere !=''
SET @strSQL = @strSQL + 'WHERE' + @strWhere + '' + @strOrder
ELSE
SET @strSQL = @strSQL + @strOrder
--The following code gives @strSQL the SQL code that is actually executed
SET @strSQL = @strSQL + 'SELECT TOP' + str(@PageSize) + '' + @strGetFields + 'FROM #tmpTable'
+ 'WHERE IID' + @strTmp +'(IID) FROM (SELECT TOP '+ str((@PageIndex-1)*@PageSize) +' IID FROM #tmpTable) AS tblTmp) DROP TABLE #tmpTable'
END
--PRINT @strSQL
--Perform paging query
EXEC (@strSQL) |
|