sqlserver 2005查看数据库大小
如题,在一台服务器上约创建有100个数据库,如何编写脚本一次性查看所有数据库的初始大小?
我是这样想的:
select name,'use '+name+char(10)+' exec sp_spaceused union all' from sys.databases
可是,有问题,请教如何正确编写,谢谢。
[解决办法]
declare @sql varchar(4000)select @sql=isnull(@sql,'')+' use '+name+char(10)+' exec sp_spaceused ' from sys.databases --print @sqlexec(@sql)
[解决办法]
------------------------------------------------------修改下系统的存储sp_spaceusedcreate procedure [dbo].[sp_spaceused_2]asbegin 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_spaceusedend----------------------------------------------------if object_id('#tab') is not null drop table #tabgocreate 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))godeclare @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)
[解决办法]
select str(sum(convert(dec(17,2),size)) / 128,10,2) + 'MB' from dbo.sysfiles GO