記錄乙個古老的Sql分頁過程

2021-08-18 01:37:17 字數 3058 閱讀 3427

/*

根據單位id獲取排班資訊

for:wxx

time:2017-11-22

*/alter proc [dbo].[proc_scheduleinfo]

@companyid nvarchar(20)='', --單位id

@classlineid nvarchar(20)='', --線路id

@vehicleid nvarchar(20)='', --車輛id

@gotime nvarchar(30) ='', --發車日期

@pageindex int=1, --當前頁

@pagesize int=1000, --頁容量

@numscount int=0 output, --總條數

@pagecount int=0 output --總頁數

as--declare @datenow nvarchar(20) --當前日期

--declare @sql varchar(max) --查詢sql

--declare @sqlcount varchar(max) --查詢countsql

--declare @top int

declare @countsql nvarchar(max) --總條數sql

declare @infosql nvarchar(max) --資料sql

declare @pagesql nvarchar(max) --分頁sql

declare @wheresql nvarchar(max) --條件sql

declare @execsql nvarchar(max) --總條數執行sql

declare @couns nvarchar(max)

set @wheresql = ' 1=1 '

if(@companyid is not null and @companyid <>'')

set @wheresql = @wheresql+' and companyid= '''+@companyid+''' '

if(@classlineid is not null and @classlineid <>'')

set @wheresql = @wheresql+' and classlineid = '+@classlineid+' '

if(@vehicleid is not null and @vehicleid <>'')

set @wheresql = @wheresql+' and vehicleid = '+@vehicleid+' '

if(@gotime is not null and @gotime <>'')

set @wheresql = @wheresql+' and date like '''+@gotime+'%'' '

--查詢總條數sql

set @countsql = 'select @count=count(1) from (

select s.*, d.remark as dr from

(select * from schedule where '+@wheresql+'

) as s

join company as d on s.companyid = d.cno

) as b '

--查詢資料sql

set @infosql = 'select top ('+convert(nvarchar(10),@pagesize)+') * from (

select row_number()over(order by date)rownumber,* from

(select s.*, d.remark as dr from

(select * from schedule where '+@wheresql+'

) as s

join company as d on s.companyid = d.cno

) as b '

set @pagesql = ' ) as t

where rownumber >= ('+convert(nvarchar(10),@pageindex)+'-1) * ('+convert(nvarchar(10),@pagesize)+')+1 order by rownumber'

--總頁數

set @execsql = @countsql

--select(@execsql) for xml path('')

exec sp_executesql @execsql, n'@count int out', @couns out

set @numscount = @couns

--分頁查詢

--select(@infosql+@wheresql+@pagesql) for xml path('')

exec(@infosql+@pagesql)

--分頁演算法

set @pagecount = @numscount % @pagesize;

if (@pagecount =0)

begin

set @pagecount = @numscount / @pagesize ;

end

else if(@numscount<@pagesize)

begin

set @pagecount=1;

end

else

begin

set @pagecount = @numscount / @pagesize + 1;

end

if (@pageindex>@pagecount)

begin

set @pageindex = @pagecount;

end

好記性不如雲儲存。

乙個分頁儲存過程

page language c import namespace system.data import namespace system.data.sqlclient 訂單號 服務專案 預訂日期 操作人員 分配狀態 databinder.eval container.dataitem,offerid...

乙個分頁儲存過程

louje 分頁儲存過程 create procedure usp page pagesize int,頁大小 pageno int,頁號 tablename nvarchar 500 表名 keyword nvarchar 20 主鍵 fieldname nvarchar 2000 要顯示的欄位名...

乙個分頁儲存過程

create procedure sp page tb varchar 50 表名 col varchar 50 按該列來進行分頁 coltype int,col 列的型別,0 數字型別,1 字元型別,2 日期時間型別 orderby bit,排序,0 順序,1 倒序 collist varchar...