Solved

SQL Server: need a query that returns list of tables

Posted on 2014-09-24
4
429 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:ScottPletcher
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 14

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to shrink a transaction log file down to a reasonable size.

707 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now