3
SELECT d.NAME
,ROUND(SUM(mf.size) * 8 / 1024, 0) Size_MBs
,(SUM(mf.size) * 8 / 1024) / 1024 AS Size_GBs
FROM sys.master_files mf
INNER JOIN sys.databases d ON d.database_id = mf.database_id
WHERE d.database_id > 4
GROUP BY d.NAME
ORDER BY d.NAME

I have the above T-SQL script which lists all databases on an SQL Server instance along with their corresponding size in MBs & GBs.

What i'm struggling with is also to include a column for the number of tables in each database.

Does any one also know how i can improve the above script to also show the total numbers of tables in each listed database. Optionally, it would be nice to get also the number of rows in each table but this is not a big issue.

I'm targeting sql server 2005 and obove.

1
  • 1
    You can't do it just by extending the current script - table definitions are stored within each database in their own metadata tables - and you can't write a single SQL query that joins to an (unknown) number of additional tables (one per database) Commented May 14, 2015 at 9:57

2 Answers 2

2

A wider report about all your databases could include stats over tables, views, procedures, triggers, amenities (xml, spatial indexes) and so far.

if object_ID('TempDB..#AllTables','U') IS NOT NULL drop table #AllTables
CREATE TABLE #AllTables (
  [DB Name] sysname, 
  [Tables] int, 
  [Views] int, 
  [Procedures] int, 
  [Triggers] int, 
  [Full Text Catalogs] int, 
  [Xml Indexes] int, 
  [Spatial Indexes] int)

DECLARE @SQL NVARCHAR(MAX)

SELECT @SQL = COALESCE(@SQL,'') + 'USE ' + quotename(name) + '
insert into #AllTables 
select ' + QUOTENAME(name,'''') + ' as [DB Name], 
    (select count(*) from ' + QUOTENAME(Name) + '.sys.tables),
    (select count(*) from ' + QUOTENAME(Name) + '.sys.views),
    (select count(*) from ' + QUOTENAME(Name) + '.sys.procedures),
    (select count(*) from ' + QUOTENAME(Name) + '.sys.triggers),
    (select count(*) from ' + QUOTENAME(Name) + '.sys.fulltext_catalogs),
    (select count(*) from ' + QUOTENAME(Name) + '.sys.xml_indexes),
    (select count(*) from ' + QUOTENAME(Name) + '.sys.spatial_indexes)
 '
FROM sys.databases
ORDER BY name
-- print @SQL -- debug 
EXECUTE(@SQL)
SELECT * FROM #AllTables

The provided solution, being far to be optimal, is easy to master and extend.

NOT OPTIMAL, just for sporadic report

based on (and credits to): http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/how-to-get-information-about-all-databas/

Sign up to request clarification or add additional context in comments.

1 Comment

This answer worked beautifully for my use case! I also added the Schema detail, and you are right, it was easy peasy to extend this framework...
1

Not as a single script, you could obtain the requested result by

CREATE TABLE AllTables ([DB Name] sysname, [Schema Name] sysname, [Table Name] sysname)


    DECLARE @SQL NVARCHAR(MAX)

    SELECT @SQL = COALESCE(@SQL,'') + '
    insert into AllTables
    select ' + QUOTENAME(name,'''') + ' as [DB Name], [Table_Schema] as 
    [Table Schema], [Table_Name] as [Table Name] from ' +
    QUOTENAME(Name) + '.INFORMATION_SCHEMA.Tables;' FROM sys.databases
    ORDER BY name 
    EXECUTE(@SQL)     
    SELECT * FROM AllTables ORDER BY [DB Name],[SCHEMA NAME], [Table Name]
    DROP TABLE AllTables

Reference:List of All Tables in All Databases

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.