Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find tables without data

How can we retrieve all tables in database without data (as in, there are no rows in table) in the case of a Microsoft SQL Server?
Is there any method?

like image 210
Sabith Paul Avatar asked Jul 08 '14 05:07

Sabith Paul


People also ask

How do I view an empty table?

The screen capture shows that if you'd like to show an empty table cell in a cross-browser manner (IE7 and Firefox) using the 'separate' border model, you can use ' ' within a table cell to display the border of the empty table cell.

How do you search for a value in a table when you don't have the exact value to search for?

How do you search for a value in a database table when you don't have the exact value to search for? In such cases, the LIKE condition operator is used to select rows that match a character pattern. This is also called 'wildcard' search.


2 Answers

Try this

   SELECT   TableName=OBJECT_NAME(OBJECT_ID) ,Data_Rows= SUM(row_count) 
   FROM     sys.dm_db_partition_stats
   WHERE    index_id in (0 ,1)
   GROUP BY OBJECT_ID
   HAVING   SUM(row_count)  = 0

OR If u need only user defined tables then use this

   SELECT TableName=OBJECT_NAME(s.OBJECT_ID) ,Data_Rows= SUM(row_count) 
   FROM     sys.dm_db_partition_stats s
   JOIN     sys.tables  T
   ON       T.object_id = S.object_id       
   WHERE    index_id in (0 ,1)
   and      T.type  = 'U'
   GROUP BY s.OBJECT_ID
   HAVING   SUM(row_count)  = 0
like image 141
Azar Avatar answered Sep 22 '22 06:09

Azar


SELECT '[' + SCHEMA_NAME(t.schema_id) + '].[' + t.name + ']' AS fulltable_name, SCHEMA_NAME(t.schema_id) AS schema_name, t.name AS table_name,
i.rows
FROM sys.tables AS t INNER JOIN
sys.sysindexes AS i ON t.object_id = i.id AND i.indid < 2 and i.rows=0

It will give table name and rows in that tables

like image 41
Sagar Chavan Avatar answered Sep 26 '22 06:09

Sagar Chavan