Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 SQL Script

Author  Topic 

Justu
Starting Member

4 Posts

Posted - 2011-01-09 : 19:39:26
Hi Friends,

Can somebody please help me with the script to list all the tables. I would really appreciate your help.

Thank you

malpashaa
Constraint Violating Yak Guru

264 Posts

Posted - 2011-01-09 : 21:56:03
The following query will return the result you need. But beware that the column size_of_table_in_MB is the size reserved for each table, and will not include the size reserved for internal tables related to that table.

SELECT T.name AS table_name,
PS.row_count AS number_of_rows,
PS.reserved_page_count * 8 / 1024.0 AS size_of_table_in_MB
FROM sys.tables AS T
CROSS APPLY
(SELECT SUM(CASE WHEN (PS.index_id < 2) THEN PS.row_count ELSE 0 END) AS row_count,
SUM(PS.reserved_page_count) AS reserved_page_count
FROM sys.dm_db_partition_stats AS PS
WHERE PS.object_id = T.object_id) AS PS
ORDER BY number_of_rows;


As you can see I used sys.dm_db_partition_stats to get the required information, and you can read about it in Books Online ([url]http://msdn.microsoft.com/en-us/library/ms187737.aspx[/url]) for more details.
Go to Top of Page

Justu
Starting Member

4 Posts

Posted - 2011-01-10 : 02:18:53
Thank you very much for your help Muhammad!! I really really appreciate it.
Go to Top of Page

malpashaa
Constraint Violating Yak Guru

264 Posts

Posted - 2011-01-10 : 03:51:45
You are welcome.
Go to Top of Page

Lumbago
Norsk Yak Master

3271 Posts

Posted - 2011-01-10 : 04:14:04
That is quite a bit of overkill if you only need the table names! This would be far more efficient:

SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE'

- Lumbago

My blog (yes, I have a blog now! just not that much content yet)
-> www.thefirstsql.com
Go to Top of Page
   

- Advertisement -