SqlServer 分頁的存儲過程
--方法一 取數據30-40 先取前40 再取前10且不存在前30中
<pre name="code" class="sql"><p>if exists(select 1 from sysobjects where name='P_GetUserInfoByPage' and type='p')
drop proc P_GetUserInfoByPage//檢查是否存在命名存儲過程,存在則刪除
go
create proc P_GetUserInfoByPage
(
@pageIndex int,--分頁的索引,默認爲1(查看的頁數)
@pageSize int ,--分頁的大小,每一頁的數據條數
@pageCount int out --總的分頁數
)
as
declare @endNum int, ---數據截止量
@sql varchar(500),--命令sql語句的長度
@recordCount int--總的數據條數
select @recordCount=COUNT(*)from tb_userinfo
--set @pageCount =ceiling(@recordCount *1/@pageSize )--
set @pageCount =ceiling (@recordCount*1.0/@pageSize )--ceiling進一法取值 必須將其精度提高到0.1
if(@pageIndex>@pageCount)
set @pageIndex=@pageCount
set @endNum=(@pageIndex -1)*@pageSize--查看的頁數的前一頁的最後一條數據
set @sql='select top '+convert(varchar(20),@pageSize)+' * from tb_userinfo where userid
not in(select top '+convert(varchar(20),@endNum)+'userid from tb_userinfo)'
exec (@sql)
go
declare @pageCount int
exec P_GetUserInfoByPage 3,3,@pageCount out
print @pageCount
select top 6* from tb_userinfo where userid
not in(select top 12 userid from tb_userinfo)
go
--SQL SERVER 2008 無法啓動T-SQL調試
--exec sp_addsrvrolemember 'SDWM-20150327QS\Administrator', 'sysadmin'
--分頁方法2
--開窗函數 取30-40 先對所有的數據進行排序 再取30-40
if exists(select * from sysobjects where name='P_GetUserInfoListByPage' and type='p')
drop proc P_GetUserInfoListByPage
go
create proc P_GetUserInfoListByPage
(
@pageIndex int,
@pageSize int
)
as
declare @statrNum int,
@eneNum int
set @statrNum=(@pageIndex-1)*@pageSize+1
set @eneNum=@pageIndex*@pageSize
select * from(select ROW_NUMBER () Over(order by userid) as rownum, * from tb_userinfo )as temp
where temp.rownum between @statrNum and @eneNum
go
exec P_GetUserInfoListByPage 2,6
exec P_GetUserInfoByPage 2,6
-----帶條件的查詢
if exists(select * from sysobjects where name='P_getListUserInfoByCondition' and type ='p')
drop proc P_getListUserInfoByCondition
go
create proc P_getListUserInfoByCondition
( @userid int,
@username varchar(30),
@roleid int,
@pageIndex int,
@PageSize int,
@pageCount int out
)
as
declare @recordCount int,--表示滿足條件的數據總數
--拼接查詢字符串
@sql nvarchar(3000),
@sqlconditional nvarchar(1000)
set @sqlconditional='';
set @sql ='select @rowcount=count(*) from tb_userinfo where 1=1';--查詢語句 1=1格式匹配where
if @userid is not null and @userid >0
set @sqlconditional =@sqlconditional+ 'and userid='+CONVERT (nvarchar(10),@userid)+'';
if @username is not null and @username <> N''--N Unicode格式字符匹配
set @sqlconditional=@sqlconditional+ 'and username like''%'+@username+'%'' ';
if @roleid is not null and @roleid >0
set @sqlconditional=@sqlconditional+'and roleid ='+CONVERT (nvarchar(20),@roleid)+'';
set @sql=@sql +@sqlconditional
exec sp_executesql @sql,N'@rowcount int out',@recordCount out--sp_executesql
set @pageCount=CEILING (@recordCount *1.0/@PageSize);
declare
@statrNum int,
@endNum int,
@sqlpage nvarchar(4000)
set @statrNum=(@pageIndex-1)*@pageSize+1;
set @endNum=@pageIndex*@pageSize
set @sqlpage =' Select ROW_NUMBER() over(order by userid) as rcount,* From tb_UserInfo Where 1=1 ';
set @sqlpage = @sqlpage + @sqlconditional;--把sql拼接的查詢條件組合在一起
set @sqlpage = 'Select * From ('+@sqlpage+') as t where t.rcount between '+convert(varchar(20),@statrNum)+
' and '+convert(varchar(20),@endNum)+' ';
exec sp_executesql @sqlpage
go
declare @pageCount int
exec P_getListUserInfoByCondition null,'a',null,1,100,@pageCount out
print @pageCount</p><h2>--通用存儲分頁(開窗函數)</h2>if exists (select 1
<span style="white-space:pre"> </span> from sysobjects
<span style="white-space:pre"> </span> where [name]='proc_CommonPage'
<span style="white-space:pre"> </span> and [type]='p')
drop proc proc_CommonPage
go
create proc proc_CommonPage
( @pageSize int,--分頁大小(每一頁多少)
@pageIndex int,--分頁索引(頁數)
@tablename varchar(50),--表名
@prikey varchar(20),--查詢關鍵字
@condtional varchar(2000),--查詢條件
@pageCount int out --總頁數
)
as
--構建查詢的sql語句
declare @sql nvarchar(1000);--查詢的sql
declare @recordCount int,--數據總數
@startIndex int,--分頁的啓示行
<span style="white-space:pre"> </span>@endIndex int--分頁的結束行
set @sql='select @recordCount=count(*) from '+@tablename+' where 1=1 ';
if<span style="white-space:pre"> </span>@condtional is not null and @condtional<> N''--查詢條件不爲空 也不爲空格
begin
set @sql=@sql+@condtional--把查詢條件加入sql語句
end
print @sql
exec sp_executesql @sql,N'@recordCount int out',@recordCount out;
--計算最大的分頁數--
set @pageCount = CEILING(@recordcount*1.0/@pagesize);--最大的分頁數 進一法取值
if @pageindex > @pageCount --如果分頁的索引大於最大的索引
begin
set @pageindex = @pageCount;
end
--構建分頁的查詢的起始行和結束行
set @startIndex = (@pageindex-1)*@pagesize +1;--計算起始行
set @endIndex = @pageindex *@pagesize
--構建分頁的sql
set @sql =''
set @sql = 'Select row_number() over (order by '+@prikey+' asc) as rowid,* from '+@tablename+' where 1=1';
if @condtional is not null and @condtional <> N'' --如果查詢條件不爲空
begin
set @sql = @sql+ @condtional;
end
set @sql = 'Select * From ('+@sql+') as tc where tc.rowid between '+convert(varchar(5),@startIndex)+' and '+convert(varchar(5),@endIndex)+'';
exec (@sql)--查詢語句
go
--測試
declare @pagecount int
exec proc_CommonPage 1,5,'tb_userinfo','userid','And usertrue=''aa'' and roleid=2',@pagecount out
go
declare @recordCount int
select @recordCount=count(*) from tb_userinfo where 1=1
pt