Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SQL Server: need a query that returns list of tables

Posted on 2014-09-24
4
Medium Priority
?
599 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 66

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 70

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 2000 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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

705 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