首页  |  讨论区  |  Java文栏  |  Java源代码  |  Java开源  |  Java下载  |  Web开发  |  数据库技术  |  编程语言  |  Linux  |  业界  |  Wiki
 首页 » 数据库 » MSSQL技术
四种方法取表里n到m条纪录
    发表于:2005-07-05     阅读:

Google
 
四种方法取表里n到m条纪录 - IdeaGrace | 数据库 | MSSQL技术,IdeaGrace,web,开发,技术,交流,教程 - http://www.ideagrace.com/
1.
select top m * into 临时表(或表变量) from tablename order by columnname -- 将top m笔插入
set rowcount n
select * from 表变量 order by columnname desc


2.
select top n * from
(select top m * from tablename order by columnname) a
order by columnname desc


3.如果tablename里没有其他identity列,那么:
select identity(int) id0,* into #temp from tablename

取n到m条的语句为:
select * from #temp where id0 >=n and id0 <= m

如果你在执行select identity(int) id0,* into #temp from tablename这条语句的时候报错,那是因为你的DB中间的select into/bulkcopy属性没有打开要先执行:
exec sp_dboption 你的DB名字,'select into/bulkcopy',true


4.如果表里有identity属性,那么简单:
select * from tablename where identitycol between n and m


从第x条到第y条的数据集?

查询第X页,每页Y条记录

最基本的处理方法(原理):

如果表中有主键(记录不重复的字段也可以),可以用类似下面的方法,当然y,(x-1)*y要换成具体的数字,不能用变量:

select top y * from 表 where 主键 not in(select top (x-1)*y 主键 from 表)



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

select id=identity(int,1,1),* into #tb from 表
select * from #tb where id between (x-1)*y+1 and x*y



喜欢本文?那就在线订阅更多文章更新吧!
加入技术论坛讨论
访问IdeaGrace开发者博客
浏览更多java开源项目
IdeaGrace开发者Wiki

更好的浏览体验,


关于Idea Grace | 版权声明 | 联系我们 | 友情链接
©2008 Idea Grace