SQLServer儲存過程實現單條件分頁

2022-09-21 09:57:10 字數 3580 閱讀 2147

話不多說,請看**:

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)www.cppcns.com = '',

--the rows to rank

@order_type int = 0,

-- *q*c* 0 normal 1 down

@phugdptngaagesizes 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 s程式設計客棧how 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

endif @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

endend

if @order_type = 1

begin

set @var_qc = '(select max'

set @order_qc = ' order by [' + @rows_order + '] asc'

endif @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

endelse

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)

呼叫:execute pagination_basic 'userdetail','*','','id','1','5','1','1','1','',''

主要是末尾的語句,拆分下來便是這樣:

select top 每頁數 列名 from [表名] where [排序欄位名]

(select min ( [排序欄位名] )from --2 獲得乙個指定列名中的最小值並輸出

(select top (當前頁-1)*每頁數 [排序欄位名] from [表名] where [條件] [排序型別]) --3 選擇之前頁數總資料倒序輸出

as tmep_qc)--4 建立乙個名為tmep_qc的臨時表--2 獲得乙個指定列名中的最小值並輸出

and [條件] [排序型別]--1 倒序輸出若列 小於之前頁數的最小值

本文標題: sqlserver儲存過程實現單條件分頁

本文位址: /shujuku/mssql/182481.html

SQL SERVER儲存過程實現分頁

create procedure dbo sp pagelist 建立儲存過程 stationid nvarchar 32 工位id pagesize int,每頁顯示的項數 pageindex int 當前頁數 as begin 獲取總行數,用count declare sqlselect nva...

sql server儲存過程實現批量刪除

在專案中用到了儲存過程來進行批量刪除的操作,給大家分享一下 原理就是把id組成的字串在資料庫分割成陣列放一張臨時表,刪除的時候與id進行對照 刪除會員資訊 ifobject id pro deluserinfo p is notnull 判斷儲存過程是否存在 drop proc pro deluse...

sql server儲存過程

建立表的語句 create table student sno int primary key,sname nvarchar 30 sgentle nvarchar 2 sage int,sbirth smalldatetime,sdept nvarchar 30 drop table studen...