SQL查询表T中第500行到1000行的ID

来源:百度知道 编辑:UC知道 时间:2024/05/31 16:46:13
SQL2005 里查询某表T中的字段,从第500行到1000行所有的ID
之前这个表的ID被删除过,我要取的是500到1000行的ID,不是ID号为500~1000的ID

关于这个问题,可以用到 SQLServer2005中新增的CET和ROW_NUMBER函数功能
假设你表中原有ID列,我在CTE中新增一个TID列
WITH Temp AS
(
SELECT ROW_NUMBER() OVER(ORDER BY ID) AS TID, *
FROM TableName
)
SELECT * FROM Temp
WHERE TID BETWEEN 500 AND 1000

select * from (
select top 500 * from (select top 1000 * from tb order by id )t
order by id desc )x orderby id

select top 500 * from table 1 where ID not in(select top 500 ID from table)
--ID为唯一时

select * from table where id<=1000 and id>=500 order by id desc
这样查询出来的ID记录就是500-1000的。
你直接复制下去即可