首页 > 数据库 >【SQLServer】快速查看SQL Server中所有数据库中所有表的行数

【SQLServer】快速查看SQL Server中所有数据库中所有表的行数

时间:2022-08-25 09:14:07浏览次数:55  
标签:JOIN name SUM SQLServer Server sys pages SQL id

1.查看某个数据库中每个表的行数

SELECT 
@@servername as servername,
db_name() as databasename,    
    s.name AS schemaname,
t.name AS tablename,
    p.rows AS rowcounts,
    SUM(a.total_pages) * 8 AS totalspaceKB, 
    SUM(a.used_pages) * 8 AS usedspaceKB, 
    (SUM(a.total_pages) - SUM(a.used_pages)) * 8 AS unusedspaceKB,
getdate() as captureddatetime
FROM sys.tables t
INNER JOIN sys.indexes i ON t.OBJECT_ID = i.object_id
INNER JOIN sys.partitions p ON i.object_id = p.OBJECT_ID AND i.index_id = p.index_id
INNER JOIN sys.allocation_units a ON p.partition_id = a.container_id
LEFT OUTER JOIN sys.schemas s ON t.schema_id = s.schema_id
WHERE t.NAME NOT LIKE 'dt%' 
    AND t.is_ms_shipped = 0
    AND i.OBJECT_ID > 255 
--and t.name =''XXXX'' ---- replace the XXXX with table name
GROUP BY 
t.name, s.name, p.Rows

  

2.查看SQL Server中每个数据库中每个表的行数

-- create table with only the names of databases that are published
SELECT 
name as databasename
INTO #alldatabases
FROM sys.databases WHERE database_id > 4
CREATE TABLE #alltablesizes(
servername sysname,
databasename sysname,
schemaName sysname,
tablename sysname,
rowcounts INT,
totalspaceKB DECIMAL(18,2),
usedspaceKB DECIMAL(18,2),
unusedspaceKB DECIMAL(18,2),
captureddatetime datetime
  );
DECLARE @command VARCHAR(MAX);
-- run the below code to get table count from all the databases 
SET @command = '
USE [?]
IF DB_NAME() IN (SELECT databasename FROM #alldatabases)
BEGIN
INSERT #alltablesizes
SELECT 
@@servername as servername,
db_name() as databasename,    
    s.name AS schemaname,
t.name AS tablename,
    p.rows AS rowcounts,
    SUM(a.total_pages) * 8 AS totalspaceKB, 
    SUM(a.used_pages) * 8 AS usedspaceKB, 
    (SUM(a.total_pages) - SUM(a.used_pages)) * 8 AS unusedspaceKB,
getdate() as captureddatetime
FROM sys.tables t
INNER JOIN sys.indexes i ON t.OBJECT_ID = i.object_id
INNER JOIN sys.partitions p ON i.object_id = p.OBJECT_ID AND i.index_id = p.index_id
INNER JOIN sys.allocation_units a ON p.partition_id = a.container_id
LEFT OUTER JOIN sys.schemas s ON t.schema_id = s.schema_id
WHERE t.NAME NOT LIKE ''dt%'' 
    AND t.is_ms_shipped = 0
    AND i.OBJECT_ID > 255 
--and t.name =''XXXX'' ---- replace the XXXX with table name
GROUP BY 
t.name, s.name, p.Rows
END';
EXEC sp_MSforeachdb @command
select * from #alltablesizes
order by 5 desc
drop table #alltablesizes
drop table #alldatabases

  

3.查看SQL Server中每个数据库中表的总行数

SELECT
name as databasename
INTO #alldatabases
FROM sys.databases WHERE database_id > 4
CREATE TABLE #alltablesizes(
servername sysname,
databasename sysname,
schemaName sysname,
tablename sysname,
rowcounts INT,
totalspaceKB DECIMAL(18,2),
usedspaceKB DECIMAL(18,2),
unusedspaceKB DECIMAL(18,2),
captureddatetime datetime
);
DECLARE @command VARCHAR(MAX);
-- run the below code to get table count from all the databases
SET @command = '
USE [?]
IF DB_NAME() IN (SELECT databasename FROM #alldatabases)
BEGIN
INSERT #alltablesizes
SELECT
@@servername as servername,
db_name() as databasename,
s.name AS schemaname,
t.name AS tablename,
p.rows AS rowcounts,
SUM(a.total_pages) * 8 AS totalspaceKB,
SUM(a.used_pages) * 8 AS usedspaceKB,
(SUM(a.total_pages) - SUM(a.used_pages)) * 8 AS unusedspaceKB,
getdate() as captureddatetime
FROM sys.tables t
INNER JOIN sys.indexes i ON t.OBJECT_ID = i.object_id
INNER JOIN sys.partitions p ON i.object_id = p.OBJECT_ID AND i.index_id = p.index_id
INNER JOIN sys.allocation_units a ON p.partition_id = a.container_id
LEFT OUTER JOIN sys.schemas s ON t.schema_id = s.schema_id
WHERE t.NAME NOT LIKE ''dt%''
AND t.is_ms_shipped = 0
AND i.OBJECT_ID > 255
--and t.name =''XXXX'' ---- replace the XXXX with table name
GROUP BY
t.name, s.name, p.Rows
END';
EXEC sp_MSforeachdb @command

select servername,databasename,captureddatetime,sum(cast(rowcounts as bigint)) from #alltablesizes
group by servername,databasename,captureddatetime
order by 1,2
drop table #alltablesizes
drop table #alldatabases

  

标签:JOIN,name,SUM,SQLServer,Server,sys,pages,SQL,id
From: https://www.cnblogs.com/abclife/p/16611138.html

相关文章

  • MySQL数据库忘记root密码
    在Linux系统中MySQL数据库的root密码忘记后,我们可以通过修改配置文件的方式先跳过密码的方式跳过密码登录MySQL数据库,后在数据库中再进行修改密码。1.修改MySQL配置文......
  • Centos7 安装 Mysql
    Centos7安装Mysql资料链接:https://pan.baidu.com/s/1TTZY1qSYNj0TC7eAlA-qsA?pwd=jjjj提取码:jjjj准备本文通过rpm,进行MySQL数据库的安装,主要的步骤如下:1).检......
  • Sqlserver 执行计划
    https://www.csdn.net/tags/MtzaAg5sMjE5NjctYmxvZwO0O0OO0O0O.htmlSQL执行计划为了帮助开发人员根据数据表中现有索引情况,了解自己编写的SQL的执行过程、优化SQL结构......
  • Mysql蜜罐读取电脑配置文件
    关于Mysql蜜罐的具体技术细节,网上文章介绍的太多了,大家可以自己从网上搜索文章,我写一个简介吧:mysql中有一个loaddatalocalinfile函数能够读取本地文件到mysql数据库中。......
  • SQL Server查询优化
    从上至下优化看过一篇文章,印象深刻,里面将数据库查询优化分为四个大的方向使用钞能力——给DB服务器加物理配置,内存啊,CPU啊,硬盘啊,全上顶配替换存储系统——根据实际的......
  • 学长告诉我,大厂MySQL都是通过SSH连接的
    大家好,我是咔咔 不期速成,日拱一卒一、背景之前待的几个公司,数据库、服务器权限都是给所有后端直接拉满的,但也会出现员工离职的情况,每次有人离职时都需要改数据库密码、......
  • 基于.NET6、FreeSql、若依UI、LayUI、Bootstrap构建插件式的CMS
    近几年,.net生态日益强大,特别是跨平台技术,性能提升,那真的是强大无比。为了日常能够快速开发,笔者基于基于.NET6、FreeSql、若依UI、LayUI、Bootstrap构建插件式的CMS,请大家......
  • MySQL索引连环问
    MySQL索引连环问什么是索引?索引类似于书本的目录,是存储引擎用于提高数据库表的访问速度的一种数据结构。数据是存储在磁盘上的,查询数据时,如果没有索引,会加载所有的数据......
  • MySQL学习
    MySQL学习01数据库的基本概念1.数据库的英文单词:DataBase简称:DB2.什么是数据库*用于存储和管理的仓库3.数据库的特点:*1.持久化存储数据。其实数据库就是一个......
  • java.sql.SQLException: The server time zone value '�й���׼ʱ��' is unrecognized or re
        这种报错主要是时区没有设置好,1.在数据库后面“?”加上:useSSL=true&characterEncoding=UTF-8&useUnicode=true&serverTimezone=GMT2.在旁边datab......