一条sql语句实现分页查询,且能返回记录总数

请问如何实现一条sql语句实现分页查询,且能返回记录总数。具体是,我想在java里通过一条sql语句实现分页查询,且能返回记录总数,不是用存储过程的方式。数据库是sql Server 2000。我用如下的方法可以实现分页查询,但是无法得到记录总数:
select * from(
select top 2 * from(
select top 4 * from(
select * from t1 -- 这里就是具体的查询语句
)as ttb0 order by id --compute count(ttb0.id)
)as ttb1 order by id desc
)as ttb2 order by id
,我想接着用compute count(ttb0.id)来追加一个总记录数,但是不行!
哪位知道有什么好的方法啊,请不吝赐教,谢谢!

可以是可以,不过土了点,用个子查询。

select top 10 *,(select count(1) from table) as cnt from table where id not in ..

这样有一个问题。就是你查询出来的每条记录里,都带一个总行数。
温馨提示:答案为网友推荐,仅供参考
第1个回答  2010-05-12
select *,(select count(*) from t1) from(
select top 2 * from(
select top 4 * from(
select * from t1
)as ttb0 order by id
)as ttb1 order by id desc
)as ttb2 order by id
在select后加个子查询就OK了,
相似回答