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.
mysql> select table_type, table_name from information_schema. tables −> where table_rows >= 1; Above, we have considered only the table that have 1 or more than 1 rows i.e. non-empty table.
First, enable Object Explorer Details going to View > Object Explorer Details or by pressing F7 buton. Now, select Tables element in your database in Object Explorer. List of tables with details will show in in the right pane in Object Explorer Details tab.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With