一台服务器上所有数据库(也可能是部分数据库)的大小是我们经常需要了解的,它不仅能让我们知道目前数据库使用磁盘的比例,
而且定期搜集这些信息,还能了解数据库一段时间的数据增量,更为常用的是在做数据迁移和升级时,方便规划新服务器磁盘容量。
这里介绍三种统计数据库大小的方法:
方法一:使用 sp_spaceused
declare @dbname varchar(100)
declare db_cur cursor for
select name from sys.sysdatabases where dbid>4 and name<>'distribution'
open db_cur
fetch next from db_cur into @dbname
while @@FETCH_STATUS=0
begin
print('use'+QUOTENAME(@dbname))
Print('go')
Print('sp_spaceused')
fetch next from db_cur into @dbname
end
close db_cur
deallocate db_cur
说明:我们做了初步的数据筛选,去掉了系统数据库和分发数据库,此方法会生成以下脚本
use [ReportServer]
go
sp_spaceused
use [ReportServerTempDB]
go
sp_spaceused