sql 儲存過程分頁

2021-06-05 20:15:58 字數 1864 閱讀 8956

use [graduate]

go/****** 物件:  storedprocedure [dbo].[common_sp_pagination]    指令碼日期: 04/16/2012 21:02:37 ******/

set ansi_nulls on

goset quoted_identifier on

gocreate proc [dbo].[common_sp_pagination]

@strsql nvarchar(max) ,

@pageindex int ,--當前頁碼

@pagecount int ,--每頁條數

@sortclause nvarchar(500) ,--排序字段,不加「order by」

@filter nvarchar(max) ,    --條件,可以為空,不加where

@totalnum int output --總記錄條數

as declare @exesql nvarchar(max)

declare @orderbystr nvarchar(500)

declare @startrowindex int

declare @endrowindex int

set @startrowindex = ( @pageindex - 1 ) * @pagecount + 1

set @endrowindex = @pageindex * @pagecount

/*--條件篩選--*/

if @filter is not null

and @filter <> ''

begin

set @strsql = @strsql + ' where 1=1 ' + @filter

end/*--排序--*/

if @sortclause is not null

and @sortclause <> ''

begin

set @orderbystr = ' order by ' + @sortclause 

set @exesql = ' select * from (' + @strsql

+ ') as a where rowindex between ' + cast(@startrowindex as varchar)

+ ' and ' + cast(@endrowindex as varchar) + ' ' + @orderbystr 

endelse

begin

set @exesql = ' select * from (' + @strsql

+ ') as a where rowindex between ' + cast(@startrowindex as varchar)

+ ' and ' + cast(@endrowindex as varchar) + ' '

endprint @strsql

print @exesql

/*---查詢總數-*/

create table #temp ( totalnum int )              

declare @inserttemp varchar(max)

set @inserttemp = 'insert into #temp select count(*) from (' + @strsql + ') as  a'

--print @inserttemp

exec(@inserttemp)

select  @totalnum = totalnum

from    #temp

drop table #temp

/*--查詢分頁資訊--*/    

exec(@exesql)

sql 儲存過程分頁

create proc myx prpagerecordset querystr nvarchar 1000 keyfield nvarchar 200 pagesize int,pagenumber int as begin declare sqltext as nvarchar 4000 dec...

SQL 儲存過程 分頁

1.俄羅斯儲存過程 的改良版 create procedure pagination1 pagesize int,頁面大小,如每頁儲存20條記錄 pageindex int 當前頁碼 as set nocount on begin declare indextable table id int id...

SQL 分頁儲存過程

create procedure splitpage sql nvarchar 4000 不帶排序語句的sql語句 page int,頁碼 recsperpage int,每頁容納的記錄數 id varchar 255 需要排序的不重複的id號 sort varchar 255 排序欄位及規則 as...