Well.... it's bit lengthy; I have used 6 functions to get the table size. Table size returned is in KB.
- fn_CalculateHeapSize: This function calculates the size of heap i.e when no clustered index has been defined.
- fn_ClusteredIndexSize: This function calculate the clustered index size.
- fn_GetNonClusteredIndexSize: This function calculate the size of all non-clustered index.
- fn_GetTableSize: This function combines the result of above functions to give the final results.
- fn_getIndexSpace: A common function used for calculation in above functions.
- fn_GetLeafLevelIndexSpace: A common function used for calculation in above functions.
- Usage: select dbo.fn_GetTableSize('tablename')
Note:
I have not considered the LOB cols. in my calculations; also this is an estimation of a table size as given in BOL "Estimating the table size"