日期:2014-05-17  浏览次数:20642 次

sqlserver 2005查看数据库大小
如题,在一台服务器上约创建有100个数据库,如何编写脚本一次性查看所有数据库的初始大小?

我是这样想的: 
  select name,'use '+name+char(10)+' exec sp_spaceused union all' from sys.databases 
可是,有问题,请教如何正确编写,谢谢。

------解决方案--------------------
SQL code

declare @sql varchar(4000)
select @sql=isnull(@sql,'')+' use '+name+char(10)+' exec sp_spaceused ' from sys.databases 
--print @sql
exec(@sql)

------解决方案--------------------
SQL code

----------------------------------------------------
--修改下系统的存储sp_spaceused
create procedure [dbo].[sp_spaceused_2]
as
begin
    declare  @type    character(2) -- The object type.
            ,@pages    bigint            -- Working variable for size calc.
            ,@dbname sysname
            ,@dbsize bigint
            ,@logsize bigint
            ,@reservedpages  bigint
            ,@usedpages  bigint
            ,@rowCount bigint
    select @dbsize = sum(convert(bigint,case when status & 64 = 0 then size else 0 end))
        , @logsize = sum(convert(bigint,case when status & 64 <> 0 then size else 0 end))
        from dbo.sysfiles

    select @reservedpages = sum(a.total_pages),
        @usedpages = sum(a.used_pages),
        @pages = sum(
                CASE
                    -- XML-Index and FT-Index-Docid is not considered "data", but is part of "index_size"
                    When it.internal_type IN (202,204) Then 0
                    When a.type <> 1 Then a.used_pages
                    When p.index_id < 2 Then a.data_pages
                    Else 0
                END
            )
    from sys.partitions p join sys.allocation_units a on p.partition_id = a.container_id
        left join sys.internal_tables it on p.object_id = it.object_id
    select 
        database_name = db_name(),
        database_size = ltrim(str((convert (dec (15,2),@dbsize) + convert (dec (15,2),@logsize)) 
            * 8192 / 1048576,15,2) + ' MB'),
        'unallocated space' = ltrim(str((case when @dbsize >= @reservedpages then
            (convert (dec (15,2),@dbsize) - convert (dec (15,2),@reservedpages)) 
            * 8192 / 1048576 else 0 end),15,2) + ' MB'),
        reserved = ltrim(str(@reservedpages * 8192 / 1024.,15,0) + ' KB'),
        data = ltrim(str(@pages * 8192 / 1024.,15,0) + ' KB'),
        index_size = ltrim(str((@usedpages - @pages) * 8192 / 1024.,15,0) + ' KB'),
        unused = ltrim(str((@reservedpages - @usedpages) * 8192 / 1024.,15,0) + ' KB')
    return (0) -- sp_spaceused
end

----------------------------------------------------
if object_id('#tab') is not null drop table #tab
go
create table #tab
(
    databasename varchar(30),
    database_size varchar(20),
    unallocated_space varchar(20),
    reserved varchar(20),
    data varchar(20),
    index_size varchar(20),
    unused varchar(20)
)
go
declare @sql nvarchar(4000)
select @sql=isnull(@sql,'')+' insert into #tab exec sp_executesql N''use '+name+' exec [dbo].[sp_spaceused_2]''' from sys.databases 
exec(@sql)
select * from #tab

------解决方案--------------------

declare @sql varchar(4000)
select @sql=isnull(@sql,'')+' use '+name+char(10)+' exec sp_spaceused ' from sys.databases 
--print @sql
exec(@sql)
------解决方案--------------------
探讨
SQL code

----------------------------------------------------
--修改下系统的存储sp_spaceused
create procedure [dbo].[sp_spaceused_2]
as
begin
declare @type character(2) -- The object type.
……

------解决方案--------------------
sp_spaceused只是当前大小,不是初始大小,不过貌似没有能直接查的,
如果要当前大小。你拿去拼接一下应该可以了
SQL code