Alter procedure adminquestion //创建存储过程
(
@pagesize int, //分页大小
@pageindex int, //当前页数
@docount bit //true:返回数据量填充分页控件,false:返回数据填充数据显示控件
)
as
set nocount on
if(@docount=1) //返回数据量填充分页控件
Select count(questionid) FROM question
else
begin
declare @indextable table(questionid int identity(1,1),nid int) //定义虚拟表,包括int型的questonid字段初始值为1,自增1,已经int型的nid字段
declare @PageLowerBound int
declare @PageUpperBound int
set @PageLowerBound=(@pageindex-1)*@pagesize
set @PageUpperBound=@PageLowerBound+@pagesize
set rowcount @PageUpperBound //确定返回的数据量
insert into @indextable(nid) select questionid FROM question order by questionid desc //定义虚拟表
select O.NodeName,0.Score,O.total,O.state ,O.questionid,O.ptime ,case when len(title)>30 then left(title,28)+'...'else title end as title from question O,@indextable t where O.questionid=t.nid
and t.questionid>@PageLowerBound and t.questionid<=@PageUpperBound order by t.questionid //查找数据,返回给应用程序
end
set nocount off
(
@pagesize int, //分页大小
@pageindex int, //当前页数
@docount bit //true:返回数据量填充分页控件,false:返回数据填充数据显示控件
)
as
set nocount on
if(@docount=1) //返回数据量填充分页控件
Select count(questionid) FROM question
else
begin
declare @indextable table(questionid int identity(1,1),nid int) //定义虚拟表,包括int型的questonid字段初始值为1,自增1,已经int型的nid字段
declare @PageLowerBound int
declare @PageUpperBound int
set @PageLowerBound=(@pageindex-1)*@pagesize
set @PageUpperBound=@PageLowerBound+@pagesize
set rowcount @PageUpperBound //确定返回的数据量
insert into @indextable(nid) select questionid FROM question order by questionid desc //定义虚拟表
select O.NodeName,0.Score,O.total,O.state ,O.questionid,O.ptime ,case when len(title)>30 then left(title,28)+'...'else title end as title from question O,@indextable t where O.questionid=t.nid
and t.questionid>@PageLowerBound and t.questionid<=@PageUpperBound order by t.questionid //查找数据,返回给应用程序
end
set nocount off
本文介绍了一个使用SQL创建的分页存储过程,该过程通过输入参数来控制返回的数据量及显示方式,适用于需要进行高效分页操作的应用场景。

164

被折叠的 条评论
为什么被折叠?



