/* Procedure copyright(c) 1995 by Edward M Barlow */

/************************************************************************\ 
|* Procedure Name:	helpdb					
|*									
\************************************************************************/ 

:r database
go
:r dumpdb
go

if exists (select *
           from   sysobjects
           where  type = "P"
           and    name = "sp__helpdb")
begin
    drop proc sp__helpdb
end
go

/* If @dbname=NoPrint no print statements will be run */
create procedure sp__helpdb ( @dbname char(30)=NULL, @dont_format char(1)=NULL )
as
	declare @msg char(128)
	declare @numpgsmb float
	select @numpgsmb = (1048576. / v.low)
		  from master.dbo.spt_values v
		  where v.number = 1 and v.type = "E"

	set nocount on
	if @dbname is not null and @dbname <> "NoPrint"
	begin
		/* Check Existence */
		if not exists ( select * from master..sysdatabases
							where name=@dbname )
		begin
			select @msg="Unknown Database: "+@dbname
			if @dont_format is not null
				print  @msg
			return 
		end

		/* Print Some Database Information */
   	select name = substring(d.name, 1, 16),
	  		 size_data=( select sum(u.size) from master..sysusages u
				where u.dbid=*d.dbid and u.segmap!=4 )/@numpgsmb,
	  		 size_log=( select sum(u.size) from master..sysusages u
				where u.dbid=*d.dbid and u.segmap=4 )/@numpgsmb,
	  		 owner = substring(suser_name(d.suid), 1, 10)
    	from   master..sysdatabases d
	 	where d.name=@dbname

		exec sp__helpdbdev @dbname

	 	return
	 end

    select distinct name = substring(d.name, 1, 20),
	   size_data=( select sum(u.size) from master..sysusages u
			where u.dbid=*d.dbid and u.segmap!=4 ),
	   size_log=( select sum(u.size) from master..sysusages u
			where u.dbid=*d.dbid and u.segmap=4 ),
	   owner = substring(suser_name(d.suid), 1, 10),
		dbid,
		status,
		status2=0,
	   si = convert(char(2), null),
	   tl = convert(char(2), null),
	   cr = convert(char(2), null),
	   cl = convert(char(2), null),
	   ds = convert(char(2), null),
	   ro = convert(char(2), null),
	   do = convert(char(2), null),
	   su = convert(char(2), null),
	   an = convert(char(2), null),
	   nc = convert(char(2), null),
	   ab = convert(char(2), null),
	   nf = convert(char(2), null),
	   ai = convert(char(2), null)
    into   #dbd_tbl
    from   master..sysdatabases d
    group by d.dbid

    update #dbd_tbl
    set size_log=round((v.low * convert(float,size_log) / 1048576), 0)
    from #dbd_tbl, master..spt_values v
    where  v.type = 'E'
    and    v.number = 1
    and    size_log is not null

    update #dbd_tbl
    set size_data=round((v.low * convert(float,size_data) / 1048576), 0)
    from #dbd_tbl, master..spt_values v
    where  v.type = 'E'
    and    v.number = 1

    update #dbd_tbl
    set    si = 'Y'
    where  status & 4 = 4

    update #dbd_tbl
    set    tl = 'Y'
    where  status & 8 = 8

    update #dbd_tbl
    set    cr = 'Y'
    where  status & 16 = 16

    update #dbd_tbl
    set    name =  rtrim(name) + " ("+convert(varchar,dbid)+")"

    update #dbd_tbl
    set    cl = 'Y'
    where  status & 32 = 32

    update #dbd_tbl
    set    ds = 'Y'
    where  status & 256 = 256

    update #dbd_tbl
    set    ro = 'Y'
    where  status & 1024 = 1024 

    update #dbd_tbl
    set    do = 'Y'
    where  status & 2048= 2048 

    update #dbd_tbl
    set    su = 'Y'
    where  status & 4096= 4096 

    update #dbd_tbl
    set    nc = 'Y'
    where  status & 16384= 16384 

    update #dbd_tbl
    set    an = 'Y'
    where  status & 8192= 8192 

    update #dbd_tbl
    set    ab = 'Y'
    where  status2 & 0x01= 0x01 

    update #dbd_tbl
    set    nf = 'Y'
    where  status2 & 0x02= 0x02 

    update #dbd_tbl
    set    ai = 'Y'
    where  status2 & 0x04= 0x04 
	   
    set nocount on
	 if @dbname <> "NoPrint" and @dont_format is null
	 begin
    	print "key   description             key   description"
    	print "---   -----------             ---   -----------"
    	print "si    select into/bulkcopy    ro    read only"
    	print "tl    trunc. log on chkpt     do    dbo use only"
    	print "cr    no chkpt on recovery    su    single user"
    	print "cl    crashed during load     ab    abort tran"
    	print "ds    database suspect"
    	print ""
    	print "****** DATABASE CONFIGURATION *******"

    select "database" = name,
	   data=str(size_data,8,0),
	   log=isnull(str(size_log,8,0),"N/A"),
	   owner,
	   /* created,*/
	   si = isnull(si, " "),
	   tl = isnull(tl, " "),
	   cr = isnull(cr, " "),
	   cl = isnull(cl, " "),
	   ds = isnull(ds, " "),
	   ro = isnull(ro, " "),
	   do = isnull(do, " "),
	   su = isnull(su, " "),
	   ab = isnull(ab, " ")
    from   #dbd_tbl
	order by name
	 end
	 else
    select "database" = name,
	   data=str(size_data,8,0),
	   log=isnull(str(size_log,8,0),"N/A"),
	   owner,
	   /* created,*/
	   si = isnull(si, " "),
	   tl = isnull(tl, " "),
	   cr = isnull(cr, " "),
	   cl = isnull(cl, " "),
	   ds = isnull(ds, " "),
	   ro = isnull(ro, " "),
	   do = isnull(do, " "),
	   su = isnull(su, " "),
	   ab = isnull(ab, " "),

	   an = isnull(an, " "),
	   nc = isnull(nc, " "),
	   nf = isnull(nf, " "),
	   ai = isnull(ai, " ")
    from   #dbd_tbl
	order by name


	 if @dbname <> "NoPrint" 
	 begin 
    	set nocount on
    	select "total space used"=str(sum(size_data+isnull(size_log,0)),10,2),
    	   "total data"=str(sum(size_data),10,2),
    	   "total log"=str(sum(isnull(size_log,0)),10,2)
    	from   #dbd_tbl
	 end 
	
return (0)

go

/* Give execute privilege to users. This can be removed if you only want
   the sa to have excute privilege on this stored proc */
grant exec on sp__helpdb to public
go

exit
