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@Ownervarchar(80)
declare@Tablavarchar(80)
declare@NomTablavarchar(80)

create table #tmpEspacio (
Namevarchar(60)not null,
Rowsintnot null,
Reservedvarchar(20)not null,
Datavarchar(20)not null,
Index_Sizevarchar(20)not null,
Unusedvarchar(20)not null
)

declaretmpCur insensitive cursor for
selectrtrim(so.Name) as 'Tabla', rtrim(su.Name) as 'Owner'
fromsysobjects so (nolock) inner join sysusers su (nolock)
on so.uId = su.uId
wheretype = '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
setReserved = 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 Reservedint

alter table #tmpEspacio
alter column Dataint

alter table #tmpEspacio
alter column Index_Sizeint

alter table #tmpEspacio
alter column Unusedint

select*
from#tmpEspacio
orderby 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