Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Get size of all tables of all Database

Author  Topic 

Portekoi
Starting Member

1 Post

Posted - 2010-01-19 : 04:52:51
Hi,

First : sorry for my english but i'm french and i'm not get used to write in this language :)

So, i want to know the size of ALL tables in ALL database with one stored procedure.

I'm running under SQL Server 2000. This is what i do but the function 'USE' is not working...


--SELECT name AS DBName, dbid as DBId, SUSER_SNAME(sid) AS Owner, crdate as CreationDate, cmptlevel AS CompatibilityLevel FROM master.dbo.sysdatabases


Declare @temp varchar(50), @DBName_ varchar(100), @TBName_ varchar(100), @Size_ float
Declare @sql varchar(8000), @proc varchar(8000)


Set @sql = ''
Set @proc = ''
--Création de la table temporaire
CREATE TABLE #sp_spaceused (TBName varchar(150), NbRows bigint, SReserved varchar(50), SData varchar(50), Sindex varchar(50), SFree varchar(50))

Declare cursor_db CURSOR
--On liste les basses des données
For SELECT name AS DBName FROM master.dbo.sysdatabases
OPEN cursor_db
Fetch next from cursor_db
INTO @DBName_
WHILE @@Fetch_STATUS = 0
BEGIN
--Print 'SELECT table_name FROM information_schema.tables WHERE table_Catalog= ''' + @DBName_ + ''' '
--Print 'USE ' + @DBName_
--On liste les tables...
execute('USE ' + @DBName_ )
set @sql = 'Declare cursor_size CURSOR'
set @sql = @sql + ' For SELECT table_name FROM ' + @DBName_ + '.information_schema.tables WHERE Table_Type = ''BASE TABLE'''
--For SELECT table_name FROM information_schema.tables WHERE table_Catalog= @DBName_ and Table_Type = 'BASE TABLE'
Execute(@sql)
OPEN cursor_size
Fetch next from cursor_size
INTO @TBName_
WHILE @@Fetch_STATUS = 0
BEGIN
if rtrim(@TBName_) <> ''
BEGIN
--insert into #sp_spaceused execute sp_spaceused @TBName_
execute('USE ' + @DBName_)
--Go
set @proc = 'insert into #sp_spaceused execute sp_spaceused ''' + @DBName_ + '.dbo.' + @TBName_ + ''''
print @proc
execute(@proc)
END
Fetch next from cursor_size
INTO @TBName_
END
CLOSE cursor_size;
DEALLOCATE cursor_size;

Fetch next from cursor_db
INTO @DBName_
END
CLOSE cursor_db;
DEALLOCATE cursor_db;

select * from #sp_spaceused
Drop Table #sp_spaceused


execute sp_spaceused @TBName_


How can i do that?

Thx


Portekoi

ianfrater
Starting Member

6 Posts

Posted - 2010-01-29 : 10:27:37
I use this SP 'Works a treat'- not sure if its what you are looking for


Ian
___________________________________________________________________
ALTER procedure s_SpaceUsed
@SourceDB varchar(128)
as
/*
exec s_SpaceUsed 'mydb'
*/

set nocount on

declare @sql varchar(128)
create table #tables(name varchar(128))

select @sql = 'insert #tables select TABLE_NAME from ' + @SourceDB + '.INFORMATION_SCHEMA.TABLES where TABLE_TYPE = ''BASE TABLE'''
exec (@sql)

create table #SpaceUsed (name varchar(128), rows varchar(11), reserved varchar(18), data varchar(18), index_size varchar(18), unused varchar(18))
declare @name varchar(128)
select @name = ''
while exists (select * from #tables where name > @name)
begin
select @name = min(name) from #tables where name > @name
select @sql = 'exec ' + @SourceDB + '..sp_executesql N''insert #SpaceUsed exec sp_spaceused ' + @name + ''''
exec (@sql)
end
select * from #SpaceUsed
drop table #tables
drop table #SpaceUsed

___________________________________________________________________





___________________________________________________________________
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2010-01-29 : 13:20:32
This will tell you the tables sizes in a single database; it has several useful options. Would need modifying for all tables PLUS all databases.

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=61762
Go to Top of Page
   

- Advertisement -