views:

39

answers:

3

I have to query in SQL server about which table is having maximum data and which one is having minimum data.

Please tell me how can i do this..

i am runninf SQL server 2005.

+2  A: 

Have a look at sp_spaceused you can run it in a loop over all your tables

If you would like to get space usage for all tables you could run (the undocumented shortcut):

CREATE TABLE #t 
( 
    [name] NVARCHAR(128),
    [rows] CHAR(11),
    reserved VARCHAR(18), 
    data VARCHAR(18), 
    index_size VARCHAR(18),
    unused VARCHAR(18)
) 

INSERT #t
EXEC sp_MSforeachtable 'EXEC sp_spaceused ''?'',''TRUE'' '

Then search through #t for your results

Sam Saffron
I guess you got more out of the OP's question than I did. +1 for pointing out a stored proc I don't think I've used before.
Tim
@Tim ... its kind of hard to tell what he means by "maximum data" ... it could be "most rows", "total storage without indexes" or "total storage" and more ... it should be clarified
Sam Saffron
+2  A: 

If you are looking for metadata about the data structures, use:

select * from sys.objects
select * from sys.columns

Otherwise, please clarify.

Tim
Or sys.tables, for the tables specifically (sys.objects contains everything.....)
marc_s
@Marc you are correct. Force of habit; I usually start with sys.objects but sys.tables would be easier.
Tim
it helped me. thnks
articlestack
+2  A: 

Here is a query that returns the number of pages in all data structures for each table in current database:

select object_name([object_id]) as obj_name, sum(page_count) as page_num
from sys.dm_db_index_physical_stats(DB_ID(), NULL, NULL, NULL, NULL)
group by [object_id]
order by sum(page_count) desc

Dynamic management views provide lots of data about the database.

VladV