Friday, March 23, 2012

How can I calculate the disk space which some records used in SQL 2005 database?

I execute the following SQL, and return 3 records, I want to know how many disk space the 3 records use, how can I do? thanks!

select * from myTable where username='Paul'

I'd like to use a temp table:


select * INTO tbl_tmp from myTable where username='Paul'

exec sp_spaceused tbl_tmp

drop table tbl_tmp

|||Many thanks! but is there a better way?|||

Or you have to sum date length for all fields:


SELECT [TotalDataSize(Bytes)]=SUM(DATALENGTH(col1)+DATALENGTH(col2)+DATALENGTH(username))
FROM myTable where username='Paul'

|||Sorry, I'm a beginner, what does '[TotalDataSize(Bytes)]' mean ?|||

And more, ifcol1 is null in some records, doesSELECT [TotalDataSize(Bytes)]=SUM(DATALENGTH(col1)+DATALENGTH(col2)+DATALENGTH(username))
FROM myTable where username='Paul'
cause error?

Many thanks!

|||BTW, the datalength method is not very accurate. It will give you a kind of estimate, but it isn't even close to correct in many cases.

No comments:

Post a Comment