如何在SQL Server中使用TOP关键字计算结果集的总行数? [英] How to count total rows of result set with TOP keyword in SQL Server?

查看:106
本文介绍了如何在SQL Server中使用TOP关键字计算结果集的总行数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友们,



我有一个下面提到的问题:



有三个表让A,B和C



我在程序上创建分页(使用上面的3个表格)

现在数据已经非常庞大。

所以我想用参数PageIndex和PageSize进行分页,



现在问题是我必须计算结果集的所有行假设有1个Lac记录,然后count()函数的时间与完全查询执行类似,



有任何方法可以计算TOP 10的结果集行(或n)记录以及结果集的总行数



请建议我





先谢谢



Shreeniwas Kushwah



RV解决方案诺伊达(UP)

Hi Friends,

I have a prob mentioned below:

There are three Tables Let A,B and C

I create pagination on a procedure (above 3 tables in it used)
now there data have been very huge .
so i want to paginate with parameter PageIndex and PageSize,

Now problem is that i have to count all rows of result set suppose there are 1 Lac records then time taken in it by count() function is similar as full query execution ,

It there any way to count result set rows with TOP 10(or n) record and also total rows of result set

Please suggest me


Thanks in Advance

Shreeniwas Kushwah

RV Solutions Noida (UP)

推荐答案

使用此查询可能会有所帮助u

use this query may be it helps u
select  top(3 or n) * ,(select count(*) from table_name) as COUNT from table_name



但请记住 TOP 关键字必须写首先,然后用逗号分隔其他列。

这里 COUNT (别名)将返回表格中的总行数



BY BKTHEBEST


but remember TOP keyword must write first and then other columns separated by comma.
here COUNT (alias name) will return total rows in your table

BY BKTHEBEST


您好,



也可以尝试这样...你可以找到使用系统表数据的表记录计数



Hi,

Try like this also... you can find the table record count using system table data

SELECT  TOP (3 or n) * , T.row_count
FROM table_name M
INNER JOIN (SELECT OBJECT_NAME(OBJECT_ID) TableName, st.row_count
FROM sys.dm_db_partition_stats st
WHERE index_id < 2
AND OBJECT_NAME(OBJECT_ID)='Table_Name') T ON T.TableName='Table_Name'





问候,

GVPrabu



Regards,
GVPrabu


这篇关于如何在SQL Server中使用TOP关键字计算结果集的总行数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆