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.
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 Heapindex_id = 1 for Clustered indexindex_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 = 0index_id = 1