怎样实现分页

okgoodsun 2003-09-02 05:36:10
再sqlserver里怎样实现乡mysql那样得limit 0,50 的分页语法?谢谢
...全文
20 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
okgoodsun 2003-09-23
  • 打赏
  • 举报
回复
to tsqgw2001(程式猎人):
谢谢你的存储过程,呵呵,这个存储过程的原理我又想过,不过我个人认为这样的存储过程太过麻烦,使用起来不是很灵活,所以我想有没有像mysql的limit那样的简便方法,我说这话没别的意思,知识和大家讨论一下,呵呵
atlasGS 2003-09-23
  • 打赏
  • 举报
回复
资源共享
ALTER PROCEDURE up_custompage @vc_order_column_name varchar(100),@vc_select_column_list varchar(100),
@vc_select_table_list varchar(100),@vc_condition varchar(100),@page_size int,@current_page int,@total1 int output
/*
(
@vc_order_column_name :表要排序列的列名,只能按一列排序,而且该列必须得在输出列表中;
@vc_select_column_list :返回列的列名列表,可以为多个列;
@vc_select_table_list:要查询的表名及关联表的连接;
@vc_condition:查询条件的字符串,必须要有查询条列,否则会抛出异常;
@page_size:每页显示记录的条数;
@current_page:当前页的页码;
@total1:所有符合条件的记录的总数。
)
*/
AS
--声明要用到的变量,@temp1是正常的分页语句字符串,@temp2是最后一页的分页语句字符串,@page_total表一共有几页,@last_page
--是最后一页的页码
declare @temp1 varchar(500),@temp2 nvarchar(500),@page_total int,@last_page int
--构造获得总页的数的检索语句
set @temp2=N'select @total2=count(*) from ' + @vc_select_table_list + ' where ' + @vc_condition
--执行检索语句,取得总的记录条数
exec sp_executesql @temp2,N' @total2 int output ',@total1 output

/*构造分页检索语句,基本原理是先取出@page_size*@current_page条记录,相当于是取出当前页及当前页前面的所有页面的记录
然后取出当前面所要显示的记录,也就是反序排序后取前@page_size条记录;最后再反序排序(因为前面的顺序被反排过一次,
现在再反排一次,正好是我们要的顺序),最后执行,返回结果集。
*/
if @total1>0
begin
set @page_total=(@total1+@page_size-1)/@page_size
--如果当前页不是最后一页
if @current_page<@page_total or @total1%@page_size=0
set @temp1='select top ' + cast(@page_size as varchar(4)) + ' * from
(select top ' + cast(@page_size as varchar(4)) + ' ' + @vc_select_column_list + ' from ' + @vc_select_table_list +' where ' + @vc_order_column_name
+ ' in (select top ' + cast(@page_size*@current_page as varchar(10)) + ' ' + @vc_order_column_name + ' from ' +
@vc_select_table_list + ' where '+ @vc_condition + ' order by ' +
@vc_order_column_name + ') order by ' + @vc_order_column_name + ' DESC) as temp1 order by ' + @vc_order_column_name
else
--最后一页只返回分页后的最后几条记录,也就是@total1%@page_size条记录
begin
set @last_page=@total1%@page_size
set @temp1='select top ' + cast(@last_page as varchar(4)) + ' * from
(select top ' + cast(@last_page as varchar(4)) + ' ' + @vc_select_column_list + ' from ' + @vc_select_table_list +' where ' + @vc_order_column_name
+ ' in (select top ' + cast(@total1 as varchar(10)) + ' ' + @vc_order_column_name + ' from ' +
@vc_select_table_list + ' where '+ @vc_condition + ' order by ' +
@vc_order_column_name + ') order by ' + @vc_order_column_name + ' DESC) as temp1 order by ' + @vc_order_column_name
end
--执行检索
exec(@temp1)
end
else
return
atlasGS 2003-09-23
  • 打赏
  • 举报
回复
我倒是有一个分页的存储过程可是搂主给的分数太低了!
Anton 2003-09-23
  • 打赏
  • 举报
回复
但是 pengdali(大力 V2.0) 所提出的第二种方法通用性不强,它必须提供一个关键字作来控制记录的范围,要是使一个复杂查询的结果分页,查询结果没关键字(例如自动编号ID之类的),那又该如何处理哪?
okgoodsun 2003-09-03
  • 打赏
  • 举报
回复
还有,用存储过程可不可以实现哪?
okgoodsun 2003-09-03
  • 打赏
  • 举报
回复
谢谢大家,但我想和大家讨论一下能不能作出一个函数能够模仿mysql的语法,例如函数limit(起始纪录,需要的记录数) 这样的话就会很容易使用

对于上面两种方法一个使用临时表,一个是做成函数使用集合相减,那那个效率更高哪?
lynx1111 2003-09-03
  • 打赏
  • 举报
回复
搜索分页有很多的!
okgoodsun 2003-09-03
  • 打赏
  • 举报
回复
自己顶
Joing 2003-09-02
  • 打赏
  • 举报
回复
相比第一种,我更喜欢第二种方法
pengdali 2003-09-02
  • 打赏
  • 举报
回复
1、
select IDENTITY(int, 1,1) AS ID_Num,* into #temp from 表
select * from #temp where ID_Num between 10 and 20


2、
declare @SQLStr varchar(8000)
set @SQLStr='SELECT Top '+cast(@每页大小 as varchar)+' * FROM 表 WHERE 主键列 NOT IN (SELECT TOP '+cast(@每页大小*@第几页 as varchar)+' 主键列 from 表 )'
exec(@SQLStr)
txlicenhe 2003-09-02
  • 打赏
  • 举报
回复
select identity(int,,1,1) as idd,* into #t from yourtable
select * from #t where idd between 100 and 150
nforcedzh 2003-09-02
  • 打赏
  • 举报
回复
关注
nforcedzh 2003-09-02
  • 打赏
  • 举报
回复
没有完全达到楼主的要求
hjb111 2003-09-02
  • 打赏
  • 举报
回复
select top 50 * from yourtable

select identity(int,,1,1) as idd,* into #t from yourtable
select * from #t where idd between 1 and 50
zyhlhx 2003-09-02
  • 打赏
  • 举报
回复
sql server 好象没有这个功能 只能 自己写程序控制了

22,210

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧