Is there a way for SQL Server Management Studio Express How to list all the non empty tables? I have over 100 tables to go through and check for data.
Try :
WITH TableRows AS
(
SELECT
SUM(row_count) AS [RowCount],
OBJECT_NAME(OBJECT_ID) AS TableName
FROM
sys.dm_db_partition_stats
WHERE
index_id = 0 OR index_id = 1
GROUP BY
OBJECT_ID
)
SELECT *
FROM TableRows
WHERE [RowCount] > 0
Morris Miao's solution uses the deprecated sys.sysindexes view; and performs the join to INFORMATION_SCHEMA.TABLES on the basis of table name, which is not guaranteed to be unique; even within a database.
Simon's solution is not scoped to the current database; but can be refined through the use of sys.tables:
SELECT r.table_name, r.row_count, r.[object_id]
FROM sys.tables t
INNER JOIN (
SELECT OBJECT_NAME(s.[object_id]) table_name, SUM(s.row_count) row_count, s.[object_id]
FROM sys.dm_db_partition_stats s
WHERE s.index_id in (0,1)
GROUP BY s.[object_id]
) r on t.[object_id] = r.[object_id]
WHERE r.row_count > 0
ORDER BY r.table_name;
Lets say Tables can be of two types.
All tables in SQL Server are divided into partitions. So that all table will have atleast one partition .
In sys.partitions, there exists one row for each partition of all tables.
These rows in sys.partitions
contains information about number of rows in that partition of the corresponding table.
Since all tables in SQL Server contain aleast one partition, we can get the information about number of rows in a table from sys.partitions
.
SELECT
OBJECT_NAME(T.OBJECT_ID) AS TABLE_NAME,
SUM(P.ROWS) AS TOTAL_ROWS
FROM
SYS.TABLES T
INNER JOIN
SYS.PARTITIONS P
ON T.OBJECT_ID = P.OBJECT_ID
WHERE
P.INDEX_ID IN (0,1)
GROUP BY
T.OBJECT_ID
HAVING
SUM(P.ROWS) > 0
While taking sum of rows in different partitions, we are considering index_id
(0,1)
index_id = 0 for Heap
index_id = 1 for Clustered index
index_id > 1 are for nonclustered index.
A table can have either one clustered index or none.
But that is not the case with nonclustered index. A table can have multiple nonclustered indexes.
So we cannot use those index_id
while summing rows.
index_id = 0
index_id = 1
One could also use "Object Explorer Details (F7)", navigate to the "Tables"-Folder of the database of interest and set the Object Explorer Details to show thw Row Count (right click on the headers)
You could try using sysindexes
and INFORMATION_SCHEMA.TABLES
:)
SELECT 'Table Name'=convert(char(25),t.TABLE_NAME),
'Total Record Count'=max(i.rows)
FROM sysindexes i, INFORMATION_SCHEMA.TABLES t
WHERE t.TABLE_NAME = object_name(i.id)
and t.TABLE_TYPE = 'BASE TABLE'
GROUP BY t.TABLE_SCHEMA, t.TABLE_NAME
HAVING max(i.rows)<=0
This is a slight improvement to @jophab's answer. It will show schema for tables as well as compare rows = 0
using sys.partitions
.
SELECT
sch.name as SchemaName,
t.NAME AS TableName,
p.rows AS RowCounts
FROM
sys.tables t
INNER JOIN
sys.partitions p ON t.object_id = p.OBJECT_ID
INNER JOIN sys.schemas sch
on t.schema_id = sch.schema_id
WHERE
t.NAME NOT LIKE 'dt%'
AND t.is_ms_shipped = 0
AND p.rows = 0
GROUP BY
sch.name,t.Name, p.Rows
ORDER BY
sch.name,t.Name