How can I get list of tables which have rows in my database.
Select A.Name
From Sys.Tables A
-- Inner Join with ?
-- Or What conditions ?
Is it possible?
please guide me! Thank you.
How can I get list of tables which have rows in my database.
Select A.Name
From Sys.Tables A
-- Inner Join with ?
-- Or What conditions ?
Is it possible?
please guide me! Thank you.
This query will help.
SELECT obj.name TableName, st.row_count
FROM sys.dm_db_partition_stats(nolock) st
inner join sys.objects(nolock) obj on st.object_id = obj.object_id
WHERE index_id < 2 and st.row_count > 1 and obj.type = 'u'
Edited to show user tables only.
Well you could get the list of tables from INFORMATION_SCHEMA.TABLES and then cursor through each table and exec some dynamic sql to get the COUNT(*) for each table. So yes, it is possible, but I don't know if there is a more simpler solution.
I hope below query help you for your problem
SELECT sc.name +'.'+ ta.name TableName ,SUM(pa.rows) RowCnt FROM sys.tables ta INNER JOIN sys.partitions pa ON pa.OBJECT_ID = ta.OBJECT_ID INNER JOIN sys.schemas sc ON ta.schema_id = sc.schema_id WHERE RowCnt > 0 GROUP BY sc.name,ta.name ORDER BY SUM(pa.rows) DESC