December 13, 2006 at 9:32 pm
My question is how to count the number of bytes defined in each row in a table using script.
Many thanks in advance.
December 13, 2006 at 10:38 pm
SELECT table_name, sum(COL_LENGTH(table_name, column_name)) AS ColumnLength
FROM INFORMATION_SCHEMA.columns
group by table_name
MohammedU
Microsoft SQL Server MVP
December 14, 2006 at 1:39 am
This won't work if some of your data is (n)varchar or text. If you want to count the size of these rows individually, you'll need to use the datalength function. Be careful of nulls, though: DATALENGTH(NULL) is NULL. If it's just an average you're after, you can use sp_MStablespace. These two links might be helpful.
http://www.sqlservercentral.com/columnists/aLohia/findingtablespace.asp
http://www.sqlservercentral.com/scripts/viewscript.asp?scriptid=135
John
December 14, 2006 at 8:36 am
i have this snippet where i was looking for the defined row size for each table:
this might help as well:
create table ##tmp (TableName varchar(40),DefinedRowSize int)
sp_msforeachtable 'INSERT INTO ##TMP Select ''?'' As TableName, SUM(C.Length) as Length from dbo.SysColumns C where C.id = object_id(''?'') '
select * from ##tmp order by DefinedRowSize desc
Lowell
December 14, 2006 at 1:59 pm
What's wrong with summing the length field in the syscolumns table? (where id = object_id( tablename ) )
December 14, 2006 at 9:53 pm
Nicely done... very nice, indeed.
--Jeff Moden
Change is inevitable... Change for the better is not.
December 15, 2006 at 8:53 am
Excellent! Thanks to John and Jeff.
December 15, 2006 at 4:49 pm
I appreciate the compliment but I didn't do anything... my comment was actually directed to Mohammed's fine and easy solution.
John is correct... using the system tables would certainly work, as well
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply