daglugub37
Technical User
Is there a way to find out how much space all databases of a particular server are using? Not allocated space but an actual sumation of space used across all databases
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
SELECT @@ServerName AS Server,
LEFT(@@version, 26) AS Version
PRINT 'Database Files - locations - sizes...'
--File Sizes and locations etc...
DECLARE @dbid INT
DECLARE @MaxId INT
DECLARE @dbName SYSNAME
SET @MaxId = (SELECT MAX(dbid) FROM MASTER.dbo.sysdatabases)
SET @dbid = 1
WHILE @dbid <= @MaxId
BEGIN
SET @dbName = (SELECT name FROM MASTER.dbo.sysdatabases WHERE dbid = @dbid)
IF (@dbname IS NOT NULL)
BEGIN
EXEC ('SET QUOTED_IDENTIFIER OFF
SELECT "[' + @dbname +']" AS DBName,
RTRIM(name) AS DevName,
RTRIM(filename) AS Filename,
RTRIM(size/128) AS Size FROM [' + @dbname + '].dbo.sysfiles')
SET @dbid = @dbid + 1
END
ELSE
SET @dbid = @dbid + 1
END
DBCC UPDATEUSAGE
SELECT SUBSTRING(/*u.name + '.' +*/ o.name,1,50) AS TableName,
Rows,
Reserved * 8 AS SizeKB,
Reserved * 8 / 1024 AS SizeMB
FROM sysobjects o (NOLOCK)
INNER JOIN sysindexes i (NOLOCK) ON o.id = i.id AND o.type = 'U' AND i.indid < 2
INNER JOIN sysusers u (NOLOCK) ON u.uid = o.uid
ORDER BY o.name, u.name
COMPUTE SUM(Rows), SUM(Reserved * 8), SUM(Reserved * 8 / 1024)
CREATE TABLE [dbo].[dbspace_stats] (
[datestamp] [datetime] NULL ,
[Servername] [varchar] (50) NULL ,
[DataBaseName] [varchar] (50) NULL ,
[Fileid] [int] NULL ,
[GroupId] [int] NULL ,
[Grp] [varchar] (50) NULL ,
[logical_name] [varchar] (500) NULL ,
[size_mb] [varchar] (50) NULL ,
[used_mb] [varchar] (50) NULL ,
[space_free] [decimal](5, 2) NULL ,
[file_growth] [varchar] (50) NULL ,
[max_mb] [varchar] (50) NULL ,
[physical_name] [varchar] (50) NULL ,
[filetype] [varchar] (10) NULL ,
[freediskspace] [varchar] (10) NULL
) ON [PRIMARY]
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
CREATE procedure usp_dbspace_stats
AS
truncate table dbspace_stats
--truncate table dbspace_dsk
EXEC usp_dbspace_dsk
SET ARITHABORT OFF
SET ANSI_WARNINGS OFF
insert into dbspace_stats
exec sp_MSforeachdb @command1='use [?]
select GetDate(),
@@servername,
''?'' AS DataBaseName,
fileid,
sf.groupid,
grp = left([groupname],20),
phname = upper (rtrim(filename)),
size_mb=[size]/128,
used_mb = FILEPROPERTY([name], ''SpaceUsed'')/128,
space_free = case when (100 - ((convert (float,(FILEPROPERTY([name], ''SpaceUsed'')/128)) / (convert (float,[size]/128)))*100)) is NULL then 0
else 100 - ((convert (float,(FILEPROPERTY([name], ''SpaceUsed'')/128)) / (convert (float,[size]/128)))*100) end,
file_growth = case when (sf.status&0x100000) > 0 then str(growth)+'' %''
else str(growth/128)+'' mb'' end,
max_mb=case when [maxsize]<0 then ''Unrestricted''
else str([maxsize]/128) end,
lname = rtrim([name]),
filetype = case
when filename like ''%.mdf%'' THEN ''DATAFILE''
when filename like ''%.MDF%'' THEN ''DATAFILE''
when filename like ''%.ldf%'' THEN ''LOGFILE''
when filename like ''%.LDF%'' THEN ''LOGFILE''
when filename like ''%.ndf%'' THEN ''FILEGROUP''
else ''UNKNOWN FILE TYPE''
END,
''''
from sysfiles sf
left outer join
sysfilegroups sfg
on sf.groupid=sfg.groupid
order by 1, 2, 3'
update dbspace_stats
set freediskspace = DRVSPACE
from dbspace_stats, dbspace_dsk
where [SQLSERVER_NAME] = [Servername]
and (left (logical_name, 1)) = DRVLETTER
--select * from dbspace_stats
--select * from dbspace_dsk
--drop table dbspace_stats
SET ARITHABORT ON
SET ANSI_WARNINGS ON
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
CREATE procedure usp_dbspace_dsk
AS
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[DBspace_dsk]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[DBspace_dsk]
DECLARE @server_name Varchar (50)
SET @server_name = (SELECT @@servername)
CREATE TABLE #T1(
DRVLETTER CHAR(1),
DRVSPACE INT
)
INSERT INTO #T1 EXEC master.dbo.xp_fixeddrives
SELECT
SQLSERVER_NAME = @server_name,
DRVLETTER,
DRVSPACE
INTO DBspace_dsk
FROM #T1
--SELECT * FROM DBspace_dsk
--DROP TABLE DBspace_dsk
DROP TABLE #T1
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO