MYSQL-SQLSERVER获取某个数据库的表记录数

MYSQL:

1,可以使用MYSQL的系统表的记录数(亲测,有时候,会不准确,被坑了一把,如果还是想通过此方式实现查询表记录数,可以按照文章后的链接进行操作)

use information_schema;
select table_name,table_rows from tables
where TABLE_SCHEMA = 'wmstesting'
order by table_rows desc;

为什么会记录不准确

怎么解决让记录准确

2,使用union 关联所有的表数据行数(这个是比较准确的,查询每个表的记录数在链接起来)

SELECT 'select * from (' UNION ALL
SELECT CONCAT( 'SELECT "', TABLE_NAME, '" as tableCodes, COUNT(*) as rows FROM ', TABLE_SCHEMA, '.', TABLE_NAME, ' UNION ALL' ) EXEC_SQL
FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'wmstesting'
UNION ALL SELECT ') a ORDER BY rows desc'

把生成的 SQL 语句拷贝出来,并去掉最后的一个“ UNION ALL ”就可以执行了。再加上排序 如下

select * from (
SELECT "abnormal_record_heare", COUNT(*) as rows FROM uco_wms_prod.abnormal_record_heare UNION ALL
SELECT "adjustment_type", COUNT(*) as rows FROM uco_wms_prod.adjustment_type UNION ALL
SELECT "allocation_rule_detail", COUNT(*) as rows FROM uco_wms_prod.allocation_rule_detail ) a ORDER BY rows desc

如果要查询整个数据库的汇总表行数,加上sum(rows)就可以了

SQLSERVER

select b.[name] 表名,max(a.rowcnt) 记录数
from sysindexes a
join sys.objects b on b.object_id=a.id
where b.type='U'
group by b.[name]
order by max(a.rowcnt) desc

-- 清除库里所有表的数据

-- USE TTX_QIMEN_EAI_DATA
-- EXEC sp_MSforeachtable N'TRUNCATE TABLE ?'
posted @ 2019-08-19 11:20  darling331  阅读(331)  评论(0编辑  收藏  举报