急,关于数据表查询!

hzcrazybb 2003-10-17 10:55:25
如何在没有关键索引可以定位的条件下实现数据表的连续查询:
先取出前20条记录,接着取下面的20条记录,、、、依次类推。
...全文
24 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
zjcxc 2003-10-17
  • 打赏
  • 举报
回复
最基本的处理方法:

如果表中无主键,可以用临时表,加标识字段解决.这里的x,y可以用变量.

select id=identity(int,1,1),* into #tb from 表
select * from #tb where id between (x-1)*y and x*y-1
zjcxc 2003-10-17
  • 打赏
  • 举报
回复
刚开了一个贴子,针对此问题:

http://expert.csdn.net/Expert/topic/2365/2365596.xml?temp=5.801028E-02
伍子V5 2003-10-17
  • 打赏
  • 举报
回复

/*
用存储过程实现的分页程序
*/
CREATE procedure Department_pagination
@SelectStr nvarchar(1000),
@ColumnStr nvarchar (1000),
@OrderStr nvarchar (1000),
@CurrentPage int,
@PageCount int
as
declare @TimeName nvarchar(25)
declare @TableStr nvarchar(1000)

select @TimeName = convert(nvarchar(23), getdate(), 121)
set @TimeName = REPLACE(@TimeName, '.', '')
set @TimeName = REPLACE(@TimeName, ':', '')
set @TimeName = REPLACE(@TimeName, '-', '')
set @TimeName = REPLACE(@TimeName, ' ', '')

select @TableStr='create table ##Tab' + @TimeName + '(wb int identity,'
exec(@TableStr+@ColumnStr+')')
exec('insert into ##Tab' + @TimeName + ' ' + @SelectStr + ' order by ' + @OrderStr)
exec('select * from ##Tab' + @TimeName + ' where wb between ((' + @CurrentPage + '-1)*' + @PageCount + '+1) and ' + @CurrentPage + '*' + @PageCount + ' order by wb')
exec('drop table ##Tab' + @TimeName)
GO



参数1:select语句。2:字段列表。3:排序字段。4:当前页。5每页数目

friendliu 2003-10-17
  • 打赏
  • 举报
回复
select idenetity(int,1,1) as iid, * into #temp from tablename
select top * from #temp where id<=10
go
select top * from #temp where id>10 and id<=20
go
txlicenhe 2003-10-17
  • 打赏
  • 举报
回复
参考:
http://expert.csdn.net/Expert/topic/2142/2142477.xml?temp=.7186701
分页处理
declare @SQLStr varchar(8000)
set @SQLStr='SELECT Top '+cast(@每页大小 as varchar)+' * FROM 表
WHERE num NOT IN (SELECT TOP '+cast(@每页大小*@第几页 as varchar)+' num from 表 )'
exec(@SQLStr)


查询N-M条记录。
select IDENTITY(int,1,1) as iid,* into #temptable from yourtable order by 排序字段
select top M-N * from #temptable where iid>=N

22,207

社区成员

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

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