SQLServer stored procedure to achieve single condition paging

SQLServer-Code:

SQLServer Procedure Pagination_basic:
ALTER PROCEDURE [qiancheng].[Pagination_basic] (
@Table_name VARCHAR (255),
--name of table
@Rows_target VARCHAR (1000) = '*',
--search rows
@Rows_condition VARCHAR (1000) = '',
--the condition to find target (no where)
@Rows_order VARCHAR (255) = '',
--the rows to rank
@Order_type INT = 0,
-- *Q*C* 0 normal 1 down
@PageSizes INT = 10,
--the size of each page
@PageIndex INT = 1,
--current page
@ShowPages INT,
--whether show the pages *Q*C* 1-yes 0-no
@ShowRecords INT,
--whether show the record *Q*C* 1-yes 0-no
@Records_total INT OUTPUT,
--returned total records
@Pages_total INT OUTPUT --returned total pages
) AS
DECLARE @MainSQL_QC nvarchar (2000) --Main SQL sentence
DECLARE @Var_QC VARCHAR (100) --Temporary variate
DECLARE @Order_QC VARCHAR (400) --the sort to rank
SET @Records_total = 0
SET @Pages_total = 0
IF @ShowRecords = 1
OR @ShowPages = 1
BEGIN

IF @Rows_condition != ''
SET @MainSQL_QC = 'select @Records_total = count(1) from [' + @Table_name + '] where ' +@Rows_condition
ELSE

SET @MainSQL_QC = 'select @Records_total = count(1) from [' + @Table_name + ']' EXEC sp_executesql @MainSQL_QC,
 N'@Records_total int out' ,@Records_total OUTPUT
END
IF @ShowPages = 1
BEGIN

IF @Records_total <= @PageSizes
SET @Pages_total = 1
ELSE

BEGIN

SET @Pages_total = @Records_total /@PageSizes
IF (@Records_total %@PageSizes) > 0
SET @Pages_total = @Pages_total + 1
END
END
IF @Order_type = 1
BEGIN

SET @Var_QC = '<(select min'
SET @Order_QC = ' order by [' + @Rows_order + '] desc'
END
ELSE

BEGIN

SET @Var_QC = '>(select max'
SET @Order_QC = ' order by [' + @Rows_order + '] asc'
END
IF @PageIndex = 1
BEGIN

IF @Rows_condition != ''
SET @MainSQL_QC = 'select top ' + str(@PageSizes) + ' ' +@Rows_target + ' from [' + @Table_name + '] where ' + @Rows_condition + ' ' + @Order_QC
ELSE

SET @MainSQL_QC = 'select top ' + str(@PageSizes) + ' ' +@Rows_target + ' from [' + @Table_name + '] ' + @Order_QC
END
ELSE

BEGIN

IF @Rows_condition != ''
SET @MainSQL_QC = 'select top ' + str(@PageSizes) + ' ' +@Rows_target + ' from [' + @Table_name + '] where [' + @Rows_order + ']' + @Var_QC + '([' + @Rows_order + ']) from (select top ' + str((@PageIndex - 1) *@PageSizes) + ' [' + @Rows_order + '] from [' + @Table_name + '] where ' + @Rows_condition + ' ' + @Order_QC + ') as Tmep_QC) and ' + @Rows_condition + ' ' + @Order_QC
ELSE

SET @MainSQL_QC = 'select top ' + str(@PageSizes) + ' ' +@Rows_target + ' from [' + @Table_name + '] where [' + @Rows_order + ']' + @Var_QC + '([' + @Rows_order + ']) from (select top ' + str((@PageIndex - 1) *@PageSizes) + ' [' + @Rows_order + '] from [' + @Table_name + ']' + @Order_QC + ') as Tmep_QC)' + @Order_QC
END EXEC (@MainSQL_QC)

transfer:execute pagination_basic 'UserDetail','*','','id','1','5','1','1','1','',''

Mainly the sentence at the end, which is broken down like this:

select topColumn name per page from[table name] where[sort field name] <--1 output in reverse order if the column is less than the minimum value of the previous page

( select min([Sort field name]) from--2 Get the minimum value in a specified column name and output

( select top(Current page -1) * Number of pages per page [Sort field name] from[Table name] where[Condition] [Sort type]) --3 Select the previous page number to output the total data in reverse order

as Tmep_QC) --4 create a file called Tmep_QCtemporary table --2 obtain a specified minimum value in the column name and output

and [Condition] [Sort Type] --1 Reverse output if the column is less than the minimum value of the previous page

appendix:

Read the original text:https://blog.mazey.net/1938.html

Guess you like

Origin blog.51cto.com/mazey/2634810