Solved

SQL Server: need a query that returns list of tables

Posted on 2014-09-24
4
577 Views
Last Modified: 2014-10-23
Hi, I need a query that returns list of all not empty data tables in the database;
 where count(*) > 0

thanx
0
Comment
Question by:quasar_ee
4 Comments
 
LVL 65

Expert Comment

by:Jim Horn
ID: 40342461
This gives you the list of tables.  With rowcounts is also possible but I don't have that SQL handy, will dig..
SELECT name FROM sys.tables ORDER BY name

Open in new window

0
 
LVL 17

Expert Comment

by:dbaSQL
ID: 40342648
This will work:

     CREATE TABLE #TableCounts (TableName VARCHAR(500), RecordCount INT  )
     INSERT #TableCounts
     EXEC sp_msForEachTable  'SELECT PARSENAME(''?'', 1), COUNT(*) FROM ? WITH (NOLOCK)' ;

     SELECT TableName,RecordCount
     FROM #TableCounts
     WHERE RecordCount > 0
     ORDER BY TableName
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 40342810
sys.dm_db_partition_stats view can give you that instantly, although it's not guaranteed to be 100% accurate if there are in-flight INSERTs/DELETEs to the table at that moment.

SELECT OBJECT_NAME(object_id) AS table_name
FROM sys.dm_db_partition_stats
WHERE index_id IN (0, 1) AND OBJECTPROPERTYEX(object_id, 'IsUserTable') = 1
GROUP BY object_id
HAVING SUM(row_count) >= 1
ORDER BY table_name

Of you can do a:
IF EXISTS(SELECT TOP (1) * FROM dbo.<table_name>)
for every table to test for a row.  Don't count every row, that could be a HUGE waste of resources since you don't need an actual count.
0
 
LVL 15

Accepted Solution

by:
Vikas Garg earned 500 total points
ID: 40343481
Hi ,

You can throw this simple SQL code for the same

select t.name TableName, i.rows Records
from sysobjects t, sysindexes i
where t.xtype = 'U' and i.id = t.id and i.indid in (0,1)
and i.rows > 0
order by TableName;

Open in new window

0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SQL Stored Proc - Performance Enhancement 15 53
how to just get time from a date 6 32
SQL Dump exec output to table 3 21
SQL Availablity Groups Shared Path 2 14
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question