Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

All Databases Data & log file size, space used & free space loading into a Table. Expand / Collapse
Author
Message
Posted Thursday, February 14, 2013 10:05 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Thursday, May 09, 2013 11:26 AM
Points: 138, Visits: 509
I am working on Capture database growth trends on a server.

I got little nice script from below Link and I also copy pasted it.

I need help on loading the output into an TABLE with current time Stamp.
So that I can maintain history for the database growth trends.

Can any one please help me, Thanks in advance.

http://gallery.technet.microsoft.com/scriptcenter/All-Databases-Data-log-a36da95d

------------------------------Data file size----------------------------
if exists (select * from tempdb.sys.all_objects where name like '%#dbsize%')
drop table #dbsize
create table #dbsize
(Dbname sysname,dbstatus varchar(50),Recovery_Model varchar(40) default ('NA'), file_Size_MB decimal(30,2)default (0),Space_Used_MB decimal(30,2)default (0),Free_Space_MB decimal(30,2) default (0))
go

insert into #dbsize(Dbname,dbstatus,Recovery_Model,file_Size_MB,Space_Used_MB,Free_Space_MB)
exec sp_msforeachdb
'use [?];
select DB_NAME() AS DbName,
CONVERT(varchar(20),DatabasePropertyEx(''?'',''Status'')) ,
CONVERT(varchar(20),DatabasePropertyEx(''?'',''Recovery'')),
sum(size)/128.0 AS File_Size_MB,
sum(CAST(FILEPROPERTY(name, ''SpaceUsed'') AS INT))/128.0 as Space_Used_MB,
SUM( size)/128.0 - sum(CAST(FILEPROPERTY(name,''SpaceUsed'') AS INT))/128.0 AS Free_Space_MB
from sys.database_files where type=0 group by type'





go

-------------------log size--------------------------------------
if exists (select * from tempdb.sys.all_objects where name like '#logsize%')
drop table #logsize
create table #logsize
(Dbname sysname, Log_File_Size_MB decimal(38,2)default (0),log_Space_Used_MB decimal(30,2)default (0),log_Free_Space_MB decimal(30,2)default (0))
go

insert into #logsize(Dbname,Log_File_Size_MB,log_Space_Used_MB,log_Free_Space_MB)
exec sp_msforeachdb
'use [?];
select DB_NAME() AS DbName,
sum(size)/128.0 AS Log_File_Size_MB,
sum(CAST(FILEPROPERTY(name, ''SpaceUsed'') AS INT))/128.0 as log_Space_Used_MB,
SUM( size)/128.0 - sum(CAST(FILEPROPERTY(name,''SpaceUsed'') AS INT))/128.0 AS log_Free_Space_MB
from sys.database_files where type=1 group by type'


go
--------------------------------database free size
if exists (select * from tempdb.sys.all_objects where name like '%#dbfreesize%')
drop table #dbfreesize
create table #dbfreesize
(name sysname,
database_size varchar(50),
Freespace varchar(50)default (0.00))

insert into #dbfreesize(name,database_size,Freespace)
exec sp_msforeachdb
'use [?];SELECT database_name = db_name()
,database_size = ltrim(str((convert(DECIMAL(15, 2), dbsize) + convert(DECIMAL(15, 2), logsize)) * 8192 / 1048576, 15, 2) + ''MB'')
,''unallocated space'' = ltrim(str((
CASE
WHEN dbsize >= reservedpages
THEN (convert(DECIMAL(15, 2), dbsize) - convert(DECIMAL(15, 2), reservedpages)) * 8192 / 1048576
ELSE 0
END
), 15, 2) + '' MB'')
FROM (
SELECT dbsize = sum(convert(BIGINT, CASE
WHEN type = 0
THEN size
ELSE 0
END))
,logsize = sum(convert(BIGINT, CASE
WHEN type <> 0
THEN size
ELSE 0
END))
FROM sys.database_files
) AS files
,(
SELECT reservedpages = sum(a.total_pages)
,usedpages = sum(a.used_pages)
,pages = sum(CASE
WHEN it.internal_type IN (
202
,204
,211
,212
,213
,214
,215
,216
)
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
INNER 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
) AS partitions'
-----------------------------------



if exists (select * from tempdb.sys.all_objects where name like '%#alldbstate%')
drop table #alldbstate
create table #alldbstate
(dbname sysname,
DBstatus varchar(55),
R_model Varchar(30))

--select * from sys.master_files

insert into #alldbstate (dbname,DBstatus,R_model)
select name,CONVERT(varchar(20),DATABASEPROPERTYEX(name,'status')),recovery_model_desc from sys.databases
--select * from #dbsize

insert into #dbsize(Dbname,dbstatus,Recovery_Model)
select dbname,dbstatus,R_model from #alldbstate where DBstatus <> 'online'

insert into #logsize(Dbname)
select dbname from #alldbstate where DBstatus <> 'online'

insert into #dbfreesize(name)
select dbname from #alldbstate where DBstatus <> 'online'

select

d.Dbname,d.dbstatus,d.Recovery_Model,
(file_size_mb + log_file_size_mb) as DBsize,
d.file_Size_MB,d.Space_Used_MB,d.Free_Space_MB,
l.Log_File_Size_MB,log_Space_Used_MB,l.log_Free_Space_MB,fs.Freespace as DB_Freespace
from #dbsize d join #logsize l
on d.Dbname=l.Dbname join #dbfreesize fs
on d.Dbname=fs.name
order by Dbname
Post #1420176
Posted Thursday, February 14, 2013 10:15 AM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Thursday, May 16, 2013 6:44 AM
Points: 1,856, Visits: 528
Without reading and testing each block, it looks like you've already done the real work. From here you have two choices:

1. When your script is done, insert the data from these temp tables into a physical table.
2. Instead of inserting into the temp table, insert directly into the physical table.
Post #1420182
Posted Thursday, February 14, 2013 10:26 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Thursday, May 09, 2013 11:26 AM
Points: 138, Visits: 509
Thanks for the replay Ed,

But my concern is, I need the Current TIME STAMP into.

I ok with any good post available, Please post the links.
Post #1420192
Posted Thursday, February 14, 2013 10:39 AM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Thursday, May 16, 2013 6:44 AM
Points: 1,856, Visits: 528
First create physical tables to use for long-term tracking of the size info you want. Include a datetime column. You'll only do this once when you first set things up. This will store the data from each run of you procedure so you can analyze it over time.

You'll want to do this for each table with info you want to track long-term. For example, to create the table to store database size using your definition of #dbsize temp table:

create table dbsize (
Dbname sysname,
dbstatus varchar(50),
Recovery_Model varchar(40),
file_Size_MB decimal(30,2),
Space_Used_MB decimal(30,2),
Free_Space_MB decimal(30,2),
entry_date datetime);

Then, at the end of your procedure, populate the real table with the data from the data from your #dbsize temp table and include your date field. You'll want to populate each table you created above. So, for the dbsize table:

INSERT INTO dbsize(Dbname,dbstatus,Recovery_Model,file_Size_MB,Space_Used_MB,Free_Space_MB, entry_date)
SELECT Dbname,dbstatus,Recovery_Model,file_Size_MB,Space_Used_MB,Free_Space_MB, GetDate()
FROM #dbsize;

Don't forget to drop those temp tables at the end of you procedure. Please do yourself a favor and make sure you understand how this stuff works before deploying it to a production environment. Always understand stuff before running it in production, no matter who it's from.
Post #1420199
Posted Friday, February 15, 2013 1:27 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Friday, April 12, 2013 3:51 AM
Points: 5,075, Visits: 4,831
This is how I do it currently, I capture the recovery model in a different proc, but wont be to hard to build it into this

CREATE TABLE [DatabaseFileUsage]
(
[ID] [bigint] IDENTITY(1,1) NOT NULL,
[DatabaseName] [sysname] NOT NULL,
[FileID] [int] NULL,
[FileSizeMB] [decimal](18, 2) NULL,
[SpaceUsedMB] [decimal](18, 2) NULL,
[FreeSpaceMB] [decimal](18, 2) NULL,
[LogicalName] [sysname] NOT NULL,
[FileLocation] [sysname] NOT NULL,
[DateCollected] [date] NULL DEFAULT GETUTCDATE(),
CONSTRAINT [PK_DB_DatabaseFileUsage] PRIMARY KEY CLUSTERED ([ID] ASC)
)
GO
DECLARE @SQL NVARCHAR(MAX)
SELECT @SQL = REPLACE(
CAST(
(
SELECT 'USE ' + QUOTENAME(name) +';' + CHAR(13) + CHAR(10) +
'INSERT INTO [DatabaseFileUsage] (DatabaseName, FileID, FileSizeMB, SpaceUsedMB, FreeSpaceMB, LogicalName, FileLocation)' + CHAR(13) + CHAR(10) +
'SELECT ' + CHAR(13) + CHAR(10) +
'DatabaseName = DB_NAME(), ' + CHAR(13) + CHAR(10) +
'a.FILEID, ' + CHAR(13) + CHAR(10) +
'[FILE_SIZE_MB] = CONVERT(DECIMAL(12, 2), ROUND(a.size / 128.000, 2)), ' + CHAR(13) + CHAR(10) +
'[SPACE_USED_MB] = CONVERT(DECIMAL(12, 2), ROUND(fileproperty(a.NAME, ' + CHAR(39) + 'SpaceUsed' + CHAR(39) +') / 128.000, 2)), ' + CHAR(13) + CHAR(10) +
'[FREE_SPACE_MB] = CONVERT(DECIMAL(12, 2), ROUND((a.size - fileproperty(a.NAME, ' + CHAR(39) + 'SpaceUsed' + CHAR(39) +')) / 128.000, 2)), ' + CHAR(13) + CHAR(10) +
'a.NAME, a.FILENAME ' + CHAR(13) + CHAR(10) +
'FROM dbo.sysfiles a;' + CHAR(13) + CHAR(10)
FROM sys.databases
FOR XML PATH('')
) AS NVARCHAR(MAX)
),'&#x 0D;',CHAR(13) + CHAR(10) --remove the space between '&#x 0D;'
)
EXECUTE sp_executesql @SQL





Want an answer fast? Try here
How to post data/code for the best help - Jeff Moden
Need a string splitter, try this - Jeff Moden
How to post performance problems - Gail Shaw
CrossTabs-Part1 & Part2 - Jeff Moden
SQL Server Backup, Integrity Check, and Index and Statistics Maintenance - Ola Hallengren
Managing Transaction Logs - Gail Shaw
Troubleshooting SQL Server: A Guide for the Accidental DBA - Jonathan Kehayias and Ted Krueger

Post #1420423
Posted Friday, February 15, 2013 8:11 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Thursday, May 09, 2013 11:26 AM
Points: 138, Visits: 509
Thanks Anthony

When I try to execute, I am getting errors, Copied below.
Will it work for SQL server 2012 release?

DECLARE @SQL NVARCHAR(MAX)
SELECT @SQL = REPLACE(
CAST(
(
SELECT 'USE ' + QUOTENAME(name) +';' + CHAR(13) + CHAR(10) +
'INSERT INTO [DatabaseFileUsage] (DatabaseName, FileID, FileSizeMB, SpaceUsedMB, FreeSpaceMB, LogicalName, FileLocation)' + CHAR(13) + CHAR(10) +
'SELECT ' + CHAR(13) + CHAR(10) +
'DatabaseName = DB_NAME(), ' + CHAR(13) + CHAR(10) +
'a.FILEID, ' + CHAR(13) + CHAR(10) +
'[FILE_SIZE_MB] = CONVERT(DECIMAL(12, 2), ROUND(a.size / 128.000, 2)), ' + CHAR(13) + CHAR(10) +
'[SPACE_USED_MB] = CONVERT(DECIMAL(12, 2), ROUND(fileproperty(a.NAME, ' + CHAR(39) + 'SpaceUsed' + CHAR(39) +') / 128.000, 2)), ' + CHAR(13) + CHAR(10) +
'[FREE_SPACE_MB] = CONVERT(DECIMAL(12, 2), ROUND((a.size - fileproperty(a.NAME, ' + CHAR(39) + 'SpaceUsed' + CHAR(39) +')) / 128.000, 2)), ' + CHAR(13) + CHAR(10) +
'a.NAME, a.FILENAME ' + CHAR(13) + CHAR(10) +
'FROM dbo.sysfiles a;' + CHAR(13) + CHAR(10)
FROM sys.databases
FOR XML PATH('')
) AS NVARCHAR(MAX)
),'&#x 0D;',CHAR(13) + CHAR(10) --remove the space between '&#x 0D;'
)
EXECUTE sp_executesql @SQL

I am getting error:
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near '&'.
Msg 102, Level 15, State 1, Line 2
Incorrect syntax near '&'.
Msg 102, Level 15, State 1, Line 11
Incorrect syntax near '&'.
Msg 102, Level 15, State 1, Line 12
Incorrect syntax near '&'.
Msg 102, Level 15, State 1, Line 21
Incorrect syntax near '&'.
Msg 102, Level 15, State 1, Line 22
Incorrect syntax near '&'.
Msg 102, Level 15, State 1, Line 31
Incorrect syntax near '&'.
Msg 102, Level 15, State 1, Line 32
Incorrect syntax near '&'.
Msg 102, Level 15, State 1, Line 41
Incorrect syntax near '&'.
Msg 102, Level 15, State 1, Line 42
Incorrect syntax near '&'.
Msg 102, Level 15, State 1, Line 51
Incorrect syntax near '&'.
Msg 102, Level 15, State 1, Line 52
Incorrect syntax near '&'.
Msg 102, Level 15, State 1, Line 61
Incorrect syntax near '&'.
Msg 102, Level 15, State 1, Line 62
Incorrect syntax near '&'.
Msg 102, Level 15, State 1, Line 71
Incorrect syntax near '&'.
Msg 102, Level 15, State 1, Line 72
Incorrect syntax near '&'.
Msg 102, Level 15, State 1, Line 81
Incorrect syntax near '&'.
Msg 102, Level 15, State 1, Line 82
Incorrect syntax near '&'.
Msg 102, Level 15, State 1, Line 91
Incorrect syntax near '&'.
Msg 102, Level 15, State 1, Line 92
Incorrect syntax near '&'.
Msg 102, Level 15, State 1, Line 101
Incorrect syntax near '&'.
Msg 102, Level 15, State 1, Line 102
Incorrect syntax near '&'.
Msg 102, Level 15, State 1, Line 111
Incorrect syntax near '&'.
Msg 102, Level 15, State 1, Line 112
Incorrect syntax near '&'.
Msg 102, Level 15, State 1, Line 121
Incorrect syntax near '&'.
Msg 102, Level 15, State 1, Line 122
Incorrect syntax near '&'.
Msg 102, Level 15, State 1, Line 131
Incorrect syntax near '&'.
Msg 102, Level 15, State 1, Line 132
Incorrect syntax near '&'.
Msg 102, Level 15, State 1, Line 141
Incorrect syntax near '&'.
Msg 102, Level 15, State 1, Line 142
Incorrect syntax near '&'.
Msg 102, Level 15, State 1, Line 151
Incorrect syntax near '&'.
Msg 102, Level 15, State 1, Line 152
Incorrect syntax near '&'.
Msg 102, Level 15, State 1, Line 161
Incorrect syntax near '&'.
Msg 102, Level 15, State 1, Line 162
Incorrect syntax near '&'.
Msg 102, Level 15, State 1, Line 171
Incorrect syntax near '&'.
Msg 102, Level 15, State 1, Line 172
Incorrect syntax near '&'.
Msg 102, Level 15, State 1, Line 181
Incorrect syntax near '&'.
Msg 102, Level 15, State 1, Line 182
Incorrect syntax near '&'.
Msg 102, Level 15, State 1, Line 191
Incorrect syntax near '&'.
Msg 102, Level 15, State 1, Line 192
Incorrect syntax near '&'.
Msg 102, Level 15, State 1, Line 201
Incorrect syntax near '&'.
Msg 102, Level 15, State 1, Line 202
Incorrect syntax near '&'.
Msg 102, Level 15, State 1, Line 211
Incorrect syntax near '&'.
Msg 102, Level 15, State 1, Line 212
Incorrect syntax near '&'.
Msg 102, Level 15, State 1, Line 221
Incorrect syntax near '&'.
Msg 102, Level 15, State 1, Line 222
Incorrect syntax near '&'.
Msg 102, Level 15, State 1, Line 231
Incorrect syntax near '&'.
Msg 102, Level 15, State 1, Line 232
Incorrect syntax near '&'.
Msg 102, Level 15, State 1, Line 241
Incorrect syntax near '&'.
Msg 102, Level 15, State 1, Line 242
Incorrect syntax near '&'.
Post #1420568
Posted Friday, February 15, 2013 8:14 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Friday, April 12, 2013 3:51 AM
Points: 5,075, Visits: 4,831
Did you remove the space on the line with the comment?



Want an answer fast? Try here
How to post data/code for the best help - Jeff Moden
Need a string splitter, try this - Jeff Moden
How to post performance problems - Gail Shaw
CrossTabs-Part1 & Part2 - Jeff Moden
SQL Server Backup, Integrity Check, and Index and Statistics Maintenance - Ola Hallengren
Managing Transaction Logs - Gail Shaw
Troubleshooting SQL Server: A Guide for the Accidental DBA - Jonathan Kehayias and Ted Krueger

Post #1420569
Posted Friday, February 15, 2013 8:30 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Thursday, May 09, 2013 11:26 AM
Points: 138, Visits: 509
Anthony,

it execute good, But gives an error on the table. below is the error message.
I have created the table in one of the user databases

Msg 208, Level 16, State 1, Line 3
Invalid object name 'DatabaseFileUsage'.
Post #1420583
Posted Friday, February 15, 2013 8:37 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Friday, April 12, 2013 3:51 AM
Points: 5,075, Visits: 4,831
Did you create the table in the script?

If not comment out the line which does the insert in the other script?




Want an answer fast? Try here
How to post data/code for the best help - Jeff Moden
Need a string splitter, try this - Jeff Moden
How to post performance problems - Gail Shaw
CrossTabs-Part1 & Part2 - Jeff Moden
SQL Server Backup, Integrity Check, and Index and Statistics Maintenance - Ola Hallengren
Managing Transaction Logs - Gail Shaw
Troubleshooting SQL Server: A Guide for the Accidental DBA - Jonathan Kehayias and Ted Krueger

Post #1420590
Posted Friday, February 15, 2013 9:01 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Thursday, May 09, 2013 11:26 AM
Points: 138, Visits: 509
First created the Table. And then running the DECLARE InSERT into statement to load into the table DatabaseFileUsage.

My understanding is the statement is loads in to the table right ?
Post #1420605
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse