Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SQL 2000 all db sizes 1

Status
Not open for further replies.

patrick118

Technical User
Jan 14, 2004
315
NL
I want a list of all databases and there db sizes in 1 query but i get the problem that i need to login into each database to get the right information. In the query below it goes wroing when i try to login another database.
Could you help me get the answer or give another script?



declare @dbname nvarchar(30)
declare @name sysname
declare @dbdesc varchar(600)

set nocount on

--create table filesize
declare @filesize table
(
fileid smallint,
groupid smallint,
groupname nvarchar(50),
size smallint,
maxsize smallint,
status int,
name nvarchar(50),
filename nvarchar(100)
)

declare @filesizedb table
(
dbid smallint,
dbname nvarchar(30)
)



insert into @filesizedb (dbid, dbname)
select dbid, name from master.dbo.sysdatabases
where (@dbname is null or name = @dbname)

/*
** Now for each dbid in filesize, build the database sizes
**
*/
declare @curdbid smallint /* the one we're currently working on */
/*
** Set @curdbid to the first dbid.
*/
select @curdbid = min(dbid) from @filesizedb

while @curdbid IS NOT NULL
begin
set @name = db_name(@curdbid)

use @name

insert into @filesize
select sysfiles.fileid,
sysfilegroups.groupid,
sysfilegroups.groupname,
sysfiles.size,
sysfiles.maxsize,
sysfiles.status,
sysfiles.name,
sysfiles.filename
from sysfiles
LEFT OUTER JOIN sysfilegroups
ON sysfiles.groupid = sysfilegroups.groupid;

/*
** Now get the next, if any dbid.
*/
select @curdbid = min(dbid) from @filesizedb where dbid > @curdbid
end

/*
** Now filesize is complete so we can print out the db info
*/
select *
from @filesize
go










 
Try:
Code:
[COLOR=blue]CREATE[/color] [COLOR=blue]TABLE[/color] #filesize
(
    fileid [COLOR=blue]int[/color],
    groupid [COLOR=blue]int[/color],
    groupname nvarchar(50),
    [COLOR=blue]size[/color] [COLOR=blue]int[/color],
    maxsize [COLOR=blue]int[/color],
    status [COLOR=blue]int[/color],
    [COLOR=blue]name[/color] nvarchar(50),
    filename nvarchar(100)
)

[COLOR=blue]declare[/color]    @filesizedb [COLOR=blue]table[/color]
(    
    dbid [COLOR=blue]int[/color],
    dbname nvarchar(30)
)



[COLOR=blue]insert[/color] [COLOR=blue]into[/color] @filesizedb (dbid, dbname)
[COLOR=blue]select[/color] dbid, [COLOR=blue]name[/color] [COLOR=blue]from[/color] master.dbo.sysdatabases
[COLOR=green]--        where (@dbname is null or name = @dbname)
[/color]
[COLOR=green]/*
[/color][COLOR=green]**  Now for each dbid in filesize, build the database sizes
[/color][COLOR=green]**  
[/color][COLOR=green]*/[/color]
[COLOR=blue]declare[/color] @curdbid [COLOR=blue]smallint[/color]    [COLOR=green]/* the one we're currently working on */[/color]
[COLOR=green]/*
[/color][COLOR=green]**  Set @curdbid to the first dbid.
[/color][COLOR=green]*/[/color]
[COLOR=blue]select[/color] @curdbid = [COLOR=#FF00FF]min[/color](dbid) [COLOR=blue]from[/color] @filesizedb

[COLOR=blue]DECLARE[/color] @sql [COLOR=blue]varchar[/color](8000)
[COLOR=blue]while[/color] @curdbid [COLOR=blue]IS[/color] NOT NULL
[COLOR=blue]begin[/color]

    [COLOR=blue]SET[/color] @sql = [COLOR=red]'INSERT INTO #filesize
[/color]                [COLOR=blue]select[/color] sysfiles.fileid,
                       sysfilegroups.groupid,
                       sysfilegroups.groupname,
                       sysfiles.size,
                       sysfiles.maxsize,
                       sysfiles.status,
                       sysfiles.name,
                       sysfiles.filename
                [COLOR=blue]from[/color] [[COLOR=red]'+db_name(@curdbid)+'[/color]]..sysfiles sysfiles
                [COLOR=#FF00FF]LEFT[/color] [COLOR=blue]JOIN[/color] [[COLOR=red]'+db_name(@curdbid)+'[/color]]..sysfilegroups sysfilegroups
                     [COLOR=blue]ON[/color] sysfiles.groupid = sysfilegroups.groupid[COLOR=red]'
[/color]    [COLOR=blue]Exec[/color](@sql)
    [COLOR=green]/*
[/color][COLOR=green]    **  Now get the next, if any dbid.
[/color][COLOR=green]    */[/color]
    [COLOR=blue]select[/color] @curdbid = [COLOR=#FF00FF]min[/color](dbid) [COLOR=blue]from[/color] @filesizedb [COLOR=blue]where[/color] dbid > @curdbid
[COLOR=blue]end[/color]

[COLOR=green]/*
[/color][COLOR=green]**  Now filesize is complete so we can print out the db info
[/color][COLOR=green]*/[/color]
[COLOR=blue]select[/color] *
[COLOR=blue]from[/color]  #filesize
[COLOR=blue]DROP[/color] [COLOR=blue]TABLE[/color] #filesize
[COLOR=blue]go[/color]

Borislav Borissov
VFP9 SP2, SQL Server 2000/2005.
 
Thank you.

Completely solved the problem

regards
Patrick
 
maybe this is to much but getting the dbname in the ouput as well?
 
But you have it.
In the NAME field.

Borislav Borissov
VFP9 SP2, SQL Server 2000/2005.
 
the name field is the name of the filename it can be different from the name of the database

 
The name field is the Logical name of the file, but
here:
Code:
[COLOR=blue]CREATE[/color] [COLOR=blue]TABLE[/color] #filesize
(
    DBNAME [COLOR=blue]varchar[/color](200),
    fileid [COLOR=blue]int[/color],
    groupid [COLOR=blue]int[/color],
    groupname nvarchar(50),
    [COLOR=blue]size[/color] [COLOR=blue]int[/color],
    maxsize [COLOR=blue]int[/color],
    status [COLOR=blue]int[/color],
    [COLOR=blue]name[/color] nvarchar(50),
    filename nvarchar(100)
)

[COLOR=blue]declare[/color]    @filesizedb [COLOR=blue]table[/color]
(    
    dbid [COLOR=blue]int[/color],
    dbname nvarchar(30)
)



[COLOR=blue]insert[/color] [COLOR=blue]into[/color] @filesizedb (dbid, dbname)
[COLOR=blue]select[/color] dbid, [COLOR=blue]name[/color] [COLOR=blue]from[/color] master.dbo.sysdatabases
[COLOR=green]--        where (@dbname is null or name = @dbname)
[/color]
[COLOR=green]/*
[/color][COLOR=green]**  Now for each dbid in filesize, build the database sizes
[/color][COLOR=green]**  
[/color][COLOR=green]*/[/color]
[COLOR=blue]declare[/color] @curdbid [COLOR=blue]smallint[/color]    [COLOR=green]/* the one we're currently working on */[/color]
[COLOR=green]/*
[/color][COLOR=green]**  Set @curdbid to the first dbid.
[/color][COLOR=green]*/[/color]
[COLOR=blue]select[/color] @curdbid = [COLOR=#FF00FF]min[/color](dbid) [COLOR=blue]from[/color] @filesizedb

[COLOR=blue]DECLARE[/color] @sql [COLOR=blue]varchar[/color](8000)
[COLOR=blue]while[/color] @curdbid [COLOR=blue]IS[/color] NOT NULL
[COLOR=blue]begin[/color]

    [COLOR=blue]SET[/color] @sql = [COLOR=red]'INSERT INTO #filesize
[/color]                [COLOR=blue]select[/color] [COLOR=red]'''+db_name(@curdbid)+'''[/color],
                       sysfiles.fileid,
                       sysfilegroups.groupid,
                       sysfilegroups.groupname,
                       sysfiles.size,
                       sysfiles.maxsize,
                       sysfiles.status,
                       sysfiles.name,
                       sysfiles.filename
                [COLOR=blue]from[/color] [[COLOR=red]'+db_name(@curdbid)+'[/color]]..sysfiles sysfiles
                [COLOR=#FF00FF]LEFT[/color] [COLOR=blue]JOIN[/color] [[COLOR=red]'+db_name(@curdbid)+'[/color]]..sysfilegroups sysfilegroups
                     [COLOR=blue]ON[/color] sysfiles.groupid = sysfilegroups.groupid[COLOR=red]'
[/color]    [COLOR=blue]Exec[/color](@sql)
    [COLOR=green]/*
[/color][COLOR=green]    **  Now get the next, if any dbid.
[/color][COLOR=green]    */[/color]
    [COLOR=blue]select[/color] @curdbid = [COLOR=#FF00FF]min[/color](dbid) [COLOR=blue]from[/color] @filesizedb [COLOR=blue]where[/color] dbid > @curdbid
[COLOR=blue]end[/color]

[COLOR=green]/*
[/color][COLOR=green]**  Now filesize is complete so we can print out the db info
[/color][COLOR=green]*/[/color]
[COLOR=blue]select[/color] *
[COLOR=blue]from[/color]  #filesize
[COLOR=blue]DROP[/color] [COLOR=blue]TABLE[/color] #filesize
[COLOR=blue]go[/color]

Borislav Borissov
VFP9 SP2, SQL Server 2000/2005.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top