查询A表中31到40的数据(ID 不一定连续)
解决方案 »
- 求查询语句
- 为了使多个客户机存取访问服务器数据库不产生并发冲突问题,是否尽量少用或不用全局的记录集变量?就是避免使用以下象这样的Public变量对吗?
- SQLSERVERAGENT 启动不了,是怎么回事?
- 为什么我的游标里有一条纪录,可去循环两次呀!!
- 如何自动在两个数据库之间导入数据?
- 有什么办法可以浏览所有的表,找到一个我想要的数据呢?
- 麻烦大家看一下这个有关默认对象等SQL语句啊???
- 一个mdf的数据库文件及ldf日志文件,在不知道原来是什么数据库名的情况下怎么附加数据库?
- 怎么样才取得连接到SQL Server 的服务器,请指教!
- 关于sql server2000在win2000专业版上的安装。
- sql 查询、 删除表中重复记录
- 有很多商品,m种,最后入库时间不一样,有什么方法能简单实现以下结果: m种产品各自最后的入库时间.
from tb t
where (select count(*) from tb where id<=t.id) between 31 and 40
--sql2000select *
from (select *,px = (select count(*) from tb where id <= t.id)
from tb t)t
where px between 31 and 40--sql2005select *
from (select *,px = row_number() over (order by id)
from tb)t
where px between 31 and 40
as
(select *,id=row_number()over(order by id ) from tb )
select *
from tt
where id between 31 and 40
select top m * from tablename where id not in (select top n id from tablename order by id asc/*|desc*/) 2.
select top m * into 临时表(或表变量) from tablename order by columnname -- 将top m笔插入到临时表
set rowcount n --只取n条结果
select * from 表变量 order by columnname desc 3.
select top n * from
(select top m * from tablename order by columnname) a
order by columnname desc
4.如果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
5.如果表里有identity属性,那么简单:
select * from tablename where identity_col between n and m 6.SQL2005开始.可以使用row_number() over()生成行号
;with cte as
(
select id0=row_number() over(order by id),* from tablename
)
select * from cte where id0 between n to m
from (select *,px = (select count(*) from tb where id <= t.id)
from tb t)t
where px between 31 and 40--sql2005select *
from (select *,px = row_number() over (order by id)
from tb)t
where px between 31 and 40