四种关于SQL查询信息分页的代码
四种关于SQL查询信息分页的代码
过程一:selecttopN条记录*from文章表whereidnotin(selecttopM条记录idfrom文章表orderbyiddesc)orderbyiddesc
过程二:selecttopN条记录*from文章表whereid<(selectmin(id)from(selecttopM条记录idfrom文章表orderbyiddesc)astblTmp)orderbyiddesc
过程三:在数据库中写语句!
createdatabasemydb
go
usemydb
go
createtablenews
(idvarchar(50)primarykey,
namevarchar(50)
)
go
insertintonewsvalues('020','sss')
insertintonewsvalues('021','ttt')
insertintonewsvalues('022','uuu')
insertintonewsvalues('001','aaa')
insertintonewsvalues('002','bbb')
insertintonewsvalues('003','ccc')
insertintonewsvalues('004','aaa')
insertintonewsvalues('005','ddd')
insertintonewsvalues('006','eee')
insertintonewsvalues('007','fff')
insertintonewsvalues('008','ggg')
insertintonewsvalues('009','hhh')
insertintonewsvalues('010','iii')
insertintonewsvalues('011','jjj')
insertintonewsvalues('012','kkk')
insertintonewsvalues('013','lll')
insertintonewsvalues('014','mmm')
insertintonewsvalues('015','nnn')
insertintonewsvalues('016','ooo')
insertintonewsvalues('017','ppp')
insertintonewsvalues('018','qqq')
insertintonewsvalues('019','rrr')
go
select*fromnews
go
createprocproc_cursor--定义存储过程
@pagesizeint,--每页有多少条数据
@pageindexint,--第几页
@pagetotalintoutput--总页数
as
begin
declare@totalint,@startint,@endint,@idvarchar(10),@namevarchar(10),@iint
--定义几个变量,作用后面解释
declaremycurscrollcursor
for
select*fromnewsorderbyID--定义一个滚动游标
openmycur--打开游标
set@total=@@cursor_rows--得到总的记录数
if@total>0
begin
if@total%@pagesize=0
set@pagetotal=@total/@pagesize
else
set@pagetotal=@total/@pagesize+1
--得到总页数