Question : Determine space used by each COLUMN in a table

Is there a way to calculate the actual space used by each column and indivdual index in an SQL 2000 table?  I've found several ways to list the total space used by the entire table, and the total of all indexes on the table, but now that I know what tables are taking up the most space, I'd like to know which columns are within the tables.  My tables are extremely wide (hundreds of columns in some cases) and many of the columns are varchar or other variable length data types, so I don't think I can do basic size multiplication.

Answer : Determine space used by each COLUMN in a table

Something like the following might help...

Also you can uncomment the line: "-- and object_name(a.id)=leName>" and replace the with the name of a specific Table Name... Otherwise that script will query every columns in every tables in your current database...

Hope this helps.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
Set NoCount ON
declare @TableName sysname,@ColName sysname,@cmd varchar(254)
DECLARE collist CURSOR FOR 
SELECT object_name(a.id)TableName,a.name ColName from syscolumns a JOIN sysobjects b on a.id=b.id WHERE b.xtype='U'
-- and object_name(a.id)=
order by 1,2
 
OPEN collist
FETCH NEXT FROM collist into @TableName, @ColName
WHILE @@FETCH_STATUS = 0
BEGIN
	select rtrim(@TableName)+'\'+@ColName
	Set @cmd='SELECT SUM(DATALENGTH('+@ColName+')) FROM '+@TableName
--	print 'Command to execute: '+@cmd
	exec(@cmd)
   FETCH NEXT FROM collist into @TableName, @ColName
END
CLOSE collist
DEALLOCATE collist
GO
Open in New Window Select All
Random Solutions  
 
programming4us programming4us