ms sql server 怎么查询指定行数的记录

fengsky491 2009-04-01 06:01:22
有表(主键为自动增长列,但可能不连续),
问:
怎么查询指定行数的数据?
如,怎么取得21行到29行的数据?
...全文
2556 16 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
kxshflyingbird 2010-05-10
  • 打赏
  • 举报
回复
study
Xiaowuasa 2009-04-02
  • 打赏
  • 举报
回复
select * from (
select *,px=(select count(1) from tb where 主键<a.主键)+1 from tb a) b
where px between 21 and 29
fengsky491 2009-04-02
  • 打赏
  • 举报
回复
好多答案啊,一会我去测试去,呵呵。
fengsky491 2009-04-02
  • 打赏
  • 举报
回复
昨天去面试的。
只知道Oracle怎么取,不知道mssql怎么取,郁闷。
[Quote=引用 5 楼 Teng_s2000 的回复:]
面试题?哈哈
[/Quote]
gao512008 2009-04-02
  • 打赏
  • 举报
回复
Study
htl258_Tony 2009-04-01
  • 打赏
  • 举报
回复
[Quote=引用楼主 fengsky491 的帖子:]
有表(主键为自动增长列,但可能不连续),
问:
怎么查询指定行数的数据?
如,怎么取得21行到29行的数据?
[/Quote]

declare @t table(id int,col varchar(10))
insert @t select 1,'a'
insert @t select 2,'b'
insert @t select 3,'c'
insert @t select 4,'d'
insert @t select 5,'e'
insert @t select 6,'f'
insert @t select 7,'g'
insert @t select 8,'h'
insert @t select 12,'i'
insert @t select 13,'j'
insert @t select 15,'k'
insert @t select 16,'l'
insert @t select 17,'m'
insert @t select 18,'n'
insert @t select 19,'o'
insert @t select 20,'p'
insert @t select 21,'q'
insert @t select 22,'r'
insert @t select 23,'s'
insert @t select 24,'t'
insert @t select 25,'u'
insert @t select 26,'v'
insert @t select 27,'w'
insert @t select 28,'x'
insert @t select 29,'y'
insert @t select 30,'z'
insert @t select 31,'a'
insert @t select 32,'b'
insert @t select 33,'c'
insert @t select 34,'d'
insert @t select 35,'e'
insert @t select 36,'f'

select * from @t t where (select count(1) from @t where id<=t.id) between 21 and 29
/*
id col
----------- ----------
25 u
26 v
27 w
28 x
29 y
30 z
31 a
32 b
33 c

(9 行受影响)
*/

lihan6415151528 2009-04-01
  • 打赏
  • 举报
回复

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


fan_xiaohu 2009-04-01
  • 打赏
  • 举报
回复
order by 后面是 IdentityId 而不是 Identity 写错了不好意思
fan_xiaohu 2009-04-01
  • 打赏
  • 举报
回复
假设自增字段名为IdentityId

select top 9 * from TableName where IdentityId not in (select top 20 * from TableName order by IdentityId asc) order by Identity asc
  • 打赏
  • 举报
回复
查询第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 and x*y-1


ks_reny 2009-04-01
  • 打赏
  • 举报
回复

select top 9 * from
(select top 29 * from tablename order by columnname) a
order by columnname desc
Teng_s2000 2009-04-01
  • 打赏
  • 举报
回复
面试题?哈哈
play7788 2009-04-01
  • 打赏
  • 举报
回复
向小梁学习。
sdhdy 2009-04-01
  • 打赏
  • 举报
回复
select * from (
select *,px=(select count(1) from tb where 主键<a.主键)+1 from tb a) b
where px between 21 and 29
rucypli 2009-04-01
  • 打赏
  • 举报
回复
select *
from (
select *,row_number() over(order by idx) as row_num
from tb
)T
where row_num between 21 and 29
liangCK 2009-04-01
  • 打赏
  • 举报
回复
取n到m行

1.
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
支持 MS SQL 2005 之前的版本 不支持 sql 2008 介绍 Log Explorer主要用于对MSSQLServer的事物分析和数据恢复。你可以浏览日志、导出数据、恢复被修改或者删除的数据(包括执行过update,delete,drop和truncate语句的表格)。一旦由于系统故障或者人为因素导致数据丢失,它能够提供在线快速的数据恢复,最大程度上保证恢复期间的其他事物不间断执行。 他可以支持SQLServer7.0、SQLServer2000和SQLServer2005,提取标准数据库的日志文件或者备份文件中的信息。 其中提供两个强大的工具:日志分析浏览,对象恢复。具体功能如下: l 日志文件浏览 l 数据库变更审查 l 计划和授权变更审查 l 将日志记录导出到文件或者数据库表 l 实时监控数据库事物 l 计算并统计负荷 l 通过有选择性的取消或者重做事物来恢复数据 l 恢复被截断或者删除表中的数据 l 运行SQL脚本 产品 LogExplore包含两部分 l 客户端软件 l 服务器代理 服务器端代理是保存在SQLServer主机中的一个只读存储过程,他的作用是接受客户端请求,读取在线事物日志块并通过网络传给客户端软件,由客户端软件来读取这些原始的数据块来完成Log Explore所提供的所有功能。 他依赖来的网络协议包括: l Named Pipe:局域网中适用 l Tcp/Ip:广域网中适用 数据库相关介绍 事物日志(Transaction Log) SQLServer的每个数据库都包含事物日志,它以文件的形式存储,可以记录数据库的任何变化。发生故障时SQLServer就是通过它来保证数据的完整性。 操作(Operation) 操作是数据库中定义的"原子行为",每个操作都在日志文件中保存为一条记录。它可以是用户直接输入的SQL语句,比如标准的insert命令,日志文件中便会记录一条操作代码来标志这个insert操作。 事物(Transaction) 事物是一系列操作组成的序列。他可以理解为直观的不可分割的一笔业务,可以执行成功或者失败。典型的事物比如由应用程序发出的具有开启-提交功能的一组SQL语句。不同的事物靠事物Id号(transaction ID)来区分,具有相同ID的事物记录的日志也相同。 在线事物日志(Online Transaction Log) 在线事物日志是指当前活动数据库所用的日志。可以通过如下命令来确定其对应文件 Select * from SYSFILES 他的文件后缀名一般是.LDF 离线事物日志(Offline Transaction Log) 离线事物日志是指非活动数据库所用的日志。当其数据库处于关闭(ShutDown)才状态下可以进行复制备份操作。他的结果同在线事物日志完全相同。 备份文件 备份文件是保存食物日志备份的文件,通常管理员通过运行SQL语句或者企业管理器来生成该文件。备份文件的内部结构和事物日志不同,他采用称为MTF的格式来保存数据。一个备份文件可以包含一个日志的多组备份,甚至包括多个数据库的混合备份. 设置为自动收缩 企业管理器--服务器--右键数据库--属性--选项--选择"自动收缩" 强烈要求该项不要选中.否则SQLServer将已循环的方式来覆盖先前的日志记录,将会导致LogExplore无法恢复错误. 数据恢复介绍 LogExplore允许你恢复应为误操作或者程序错误而导致的数据丢失或者更改.比如执行update\Delete语句时丢失了where子句,或者错误使用了Dts功能. LogExplore不支持直接修改数据库.他可以生成事物的逆操作脚本. 如果log是delete table where ...的话,生成的文件代码就是insert table .... 你可以通过SQL查询分析器,或者LogExplore的Run SQL Script功能来执行生成脚本. 关于Undo Undo功能可以逆操作一组指定的用户事物。包括insert,delete和update,其局限性如下: l 事物类别:LogExplore只能undo用户事物。用户事物是指在用户表上定义的事物,不支持系统表的更新恢复。同时,他也不支持计划变更的回滚。 l Blob类型:包括text,ntext,image类型。LogExplore只支持这些类型的insert和delete恢复,不支持update语句恢复。 关于redo Redo功能可以再次运行一组指定事物。它可以在以下情况中用到: 丢失数据库而且没有任何备份文件。 l 如果原始日志文件没有丢失可以通过Redo来实现恢复。 l

34,871

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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