Technical Article

Script to get total rows and space used

,

Script to Get Total Rows, space used and reserved (KB) of all tables

set nocount on

declare	@Owner		varchar(80)
declare	@Tabla		varchar(80)
declare	@NomTabla	varchar(80)

create table #tmpEspacio (
	Name		varchar(60)	not null,
	Rows		int		not null,
	Reserved	varchar(20)	not null,
	Data		varchar(20)	not null,
	Index_Size	varchar(20)	not null,
	Unused		varchar(20)	not null
)

declare	tmpCur insensitive cursor for
	select	rtrim(so.Name) as 'Tabla', rtrim(su.Name) as 'Owner'
	from	sysobjects so (nolock) inner join sysusers su (nolock)
			on so.uId = su.uId
	where	type = 'U'

open tmpCur

fetch next from tmpCur into @Tabla, @Owner

while @@Fetch_Status = 0 begin

	set @NomTabla = @Owner + '.[' + @Tabla + ']'

	insert	#tmpEspacio (Name, Rows, Reserved, Data, Index_Size, Unused)
	exec sp_spaceused @NomTabla

	fetch next from tmpCur into @Tabla, @Owner

end

close tmpCur

deallocate tmpCur

update	#tmpEspacio
set	Reserved = left(Reserved, len(Reserved) - 3), Data = left(Data, len(Data) - 3),
	Index_Size = left(Index_Size, len(Index_Size) - 3), Unused = left(Unused, len(Unused) - 3)

alter table #tmpEspacio
	alter column Reserved	int

alter table #tmpEspacio
	alter column Data	int

alter table #tmpEspacio
	alter column Index_Size	int

alter table #tmpEspacio
	alter column Unused	int

select	*
from	#tmpEspacio
order	by Reserved desc

drop table #tmpEspacio

set nocount off

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating