473,665 Members | 2,740 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

MSSQL DB Space usage

2 New Member
How to find out the space usage for all the databases in MS SQL thru SQL Query Analyzer?

Normaly i used to view thru SQL Server Enterprise Manager Screen (view --> Taskpad) by selecting 1 db per view.
Oct 8 '08 #1
3 3083
PaulThomasSilvey
3 New Member
I think you are looking for this EXEC sp_helpdb
Jan 5 '09 #2
ck9663
2,878 Recognized Expert Specialist
Also, check this out.

-- CK
Jan 5 '09 #3
madan agarwal
8 New Member
declare @TargetDatabase sysname
declare @Level varchar(10)
declare @UpdateUsage bit
declare @Unit char(2)
select @Unit='MB'
SET NOCOUNT ON;
IF OBJECT_ID('temp db.dbo.##Tbl_Co mbinedInfo', 'U') IS NOT NULL
DROP TABLE dbo.##Tbl_Combi nedInfo;

IF OBJECT_ID('temp db.dbo.##Tbl_Db FileStats', 'U') IS NOT NULL
DROP TABLE dbo.##Tbl_DbFil eStats;

IF OBJECT_ID('temp db.dbo.##Tbl_Va lidDbs', 'U') IS NOT NULL
DROP TABLE dbo.##Tbl_Valid Dbs;

IF OBJECT_ID('temp db.dbo.##Tbl_Lo gs', 'U') IS NOT NULL
DROP TABLE dbo.##Tbl_Logs;

CREATE TABLE dbo.##Tbl_Combi nedInfo (
DatabaseName sysname NULL,
[type] VARCHAR(10) NULL,
LogicalName sysname NULL,
T dec(10, 2) NULL,
U dec(10, 2) NULL,
[U(%)] dec(5, 2) NULL,
F dec(10, 2) NULL,
[F(%)] dec(5, 2) NULL,
PhysicalName sysname NULL );
CREATE TABLE dbo.##Tbl_DbFil eStats (
Id int identity,
DatabaseName sysname NULL,
FileId int NULL,
FileGroup int NULL,
TotalExtents bigint NULL,
UsedExtents bigint NULL,
Name sysname NULL,
FileName varchar(255) NULL );

CREATE TABLE dbo.##Tbl_Valid Dbs (
Id int identity,
Dbname sysname NULL );

CREATE TABLE dbo.##Tbl_Logs (
DatabaseName sysname NULL,
LogSize dec (10, 2) NULL,
LogSpaceUsedPer cent dec (5, 2) NULL,
Status int NULL );
DECLARE @Ver varchar(10),
@DatabaseName sysname,
@Ident_last int,
@String varchar(2000),
@BaseString varchar(2000);

SELECT @DatabaseName = '',
@Ident_last = 0,
@String = '',
@Ver = CASE WHEN @@VERSION LIKE '%9.0%' THEN 'SQL 2005'
WHEN @@VERSION LIKE '%8.0%' THEN 'SQL 2000'
WHEN @@VERSION LIKE '%10.0%' THEN 'SQL 2008'
END;

SELECT @BaseString =
' SELECT DB_NAME(), ' +
CASE WHEN @Ver = 'SQL 2000' THEN 'CASE WHEN status & 0x40 = 0x40 THEN ''Log'' ELSE ''Data'' END'
ELSE ' CASE type WHEN 0 THEN ''Data'' WHEN 1 THEN ''Log'' WHEN 4 THEN ''Full-text'' ELSE ''reserved'' END' END +
', name, ' +
CASE WHEN @Ver = 'SQL 2000' THEN 'filename' ELSE 'physical_name' END +
', size*8.0/1024.0 FROM ' +
CASE WHEN @Ver = 'SQL 2000' THEN 'sysfiles' ELSE 'sys.database_f iles' END +
' WHERE '
+ CASE WHEN @Ver = 'SQL 2000' THEN ' HAS_DBACCESS(DB _NAME()) = 1' ELSE 'state_desc = ''ONLINE''' END + '';
SELECT @String = 'INSERT INTO dbo.##Tbl_Valid Dbs SELECT name FROM ' +
CASE WHEN @Ver = 'SQL 2000' THEN 'master.dbo.sys databases'
WHEN @Ver IN ('SQL 2005', 'SQL 2008') THEN 'master.sys.dat abases'
END + ' WHERE HAS_DBACCESS(na me) = 1 ORDER BY name ASC';
EXEC (@String);
INSERT INTO dbo.##Tbl_Logs EXEC ('DBCC SQLPERF (LOGSPACE) WITH NO_INFOMSGS');
-- For data part
IF @TargetDatabase IS NOT NULL
BEGIN
SELECT @DatabaseName = @TargetDatabase ;
SELECT @String = 'INSERT INTO dbo.##Tbl_Combi nedInfo (DatabaseName, type, LogicalName, PhysicalName, T) ' + @BaseString;
INSERT INTO dbo.##Tbl_DbFil eStats (FileId, FileGroup, TotalExtents, UsedExtents, Name, FileName)
EXEC ('USE [' + @DatabaseName + '] DBCC SHOWFILESTATS WITH NO_INFOMSGS');
EXEC ('USE [' + @DatabaseName + '] ' + @String);

UPDATE dbo.##Tbl_DbFil eStats SET DatabaseName = @DatabaseName;
END
ELSE
BEGIN
WHILE 1 = 1
BEGIN
SELECT TOP 1 @DatabaseName = Dbname FROM dbo.##Tbl_Valid Dbs WHERE Dbname > @DatabaseName ORDER BY Dbname ASC;
IF @@ROWCOUNT = 0
BREAK;
/*
IF @UpdateUsage <> 0 AND DATABASEPROPERT YEX (@DatabaseName, 'Status') = 'ONLINE'
AND DATABASEPROPERT YEX (@DatabaseName, 'Updateability' ) <> 'READ_ONLY'
BEGIN
SELECT @String = 'DBCC UPDATEUSAGE (''' + @DatabaseName + ''') ';
PRINT '*** ' + @String + '*** ';
EXEC (@String);
PRINT '';
END
*/

SELECT @Ident_last = ISNULL(MAX(Id), 0) FROM dbo.##Tbl_DbFil eStats;
SELECT @String = 'INSERT INTO dbo.##Tbl_Combi nedInfo (DatabaseName, type, LogicalName, PhysicalName, T) ' + @BaseString;
EXEC ('USE [' + @DatabaseName + '] ' + @String);

INSERT INTO dbo.##Tbl_DbFil eStats (FileId, FileGroup, TotalExtents, UsedExtents, Name, FileName)
EXEC ('USE [' + @DatabaseName + '] DBCC SHOWFILESTATS WITH NO_INFOMSGS');
UPDATE dbo.##Tbl_DbFil eStats SET DatabaseName = @DatabaseName WHERE Id BETWEEN @Ident_last + 1 AND @@IDENTITY;
END
END
-- set used size for data files, do not change total obtained from sys.database_fi les as it has for log files
UPDATE dbo.##Tbl_Combi nedInfo
SET U = s.UsedExtents*8 *8/1024.0
FROM dbo.##Tbl_Combi nedInfo t JOIN dbo.##Tbl_DbFil eStats s
ON t.LogicalName = s.Name AND s.DatabaseName = t.DatabaseName;
-- set used size and % values for log files:
UPDATE dbo.##Tbl_Combi nedInfo
SET [U(%)] = LogSpaceUsedPer cent,
U = T * LogSpaceUsedPer cent/100.0
FROM dbo.##Tbl_Combi nedInfo t JOIN dbo.##Tbl_Logs l
ON l.DatabaseName = t.DatabaseName
WHERE t.type = 'Log';
UPDATE dbo.##Tbl_Combi nedInfo SET F = T - U, [U(%)] = U*100.0/T;
UPDATE dbo.##Tbl_Combi nedInfo SET [F(%)] = F*100.0/T;
IF UPPER(ISNULL(@L evel, 'DATABASE')) = 'FILE'
BEGIN
IF @Unit = 'KB'
UPDATE dbo.##Tbl_Combi nedInfo
SET T = T * 1024, U = U * 1024, F = F * 1024;

IF @Unit = 'GB'
UPDATE dbo.##Tbl_Combi nedInfo
SET T = T / 1024, U = U / 1024, F = F / 1024;

SELECT DatabaseName AS 'Database',
type AS 'Type',
LogicalName,
T AS 'Total',
U AS 'Used',
[U(%)] AS 'Used (%)',
F AS 'Free',
[F(%)] AS 'Free (%)',
PhysicalName
FROM dbo.##Tbl_Combi nedInfo
WHERE DatabaseName LIKE ISNULL(@TargetD atabase, '%')
ORDER BY DatabaseName ASC, type ASC;
SELECT CASE WHEN @Unit = 'GB' THEN 'GB' WHEN @Unit = 'KB' THEN 'KB' ELSE 'MB' END AS 'SUM',
SUM (T) AS 'TOTAL', SUM (U) AS 'USED', SUM (F) AS 'FREE' FROM dbo.##Tbl_Combi nedInfo;
END
IF UPPER(ISNULL(@L evel, 'DATABASE')) = 'DATABASE'
BEGIN
DECLARE @Tbl_Final TABLE (
DatabaseName sysname NULL,
TOTAL dec (10, 2),
[=] char(1),
used dec (10, 2),
[used (%)] dec (5, 2),
[+] char(1),
free dec (10, 2),
[free (%)] dec (5, 2),
[==] char(2),
Data dec (10, 2),
Data_Used dec (10, 2),
[Data_Used (%)] dec (5, 2),
Data_Free dec (10, 2),
[Data_Free (%)] dec (5, 2),
[++] char(2),
Log dec (10, 2),
Log_Used dec (10, 2),
[Log_Used (%)] dec (5, 2),
Log_Free dec (10, 2),
[Log_Free (%)] dec (5, 2) );
INSERT INTO @Tbl_Final
SELECT x.DatabaseName,
x.Data + y.Log AS 'TOTAL',
'=' AS '=',
x.Data_Used + y.Log_Used AS 'U',
(x.Data_Used + y.Log_Used)*100 .0 / (x.Data + y.Log) AS 'U(%)',
'+' AS '+',
x.Data_Free + y.Log_Free AS 'F',
(x.Data_Free + y.Log_Free)*100 .0 / (x.Data + y.Log) AS 'F(%)',
'==' AS '==',
x.Data,
x.Data_Used,
x.Data_Used*100/x.Data AS 'D_U(%)',
x.Data_Free,
x.Data_Free*100/x.Data AS 'D_F(%)',
'++' AS '++',
y.Log,
y.Log_Used,
y.Log_Used*100/y.Log AS 'L_U(%)',
y.Log_Free,
y.Log_Free*100/y.Log AS 'L_F(%)'
FROM
( SELECT d.DatabaseName,
SUM(d.T) AS 'Data',
SUM(d.U) AS 'Data_Used',
SUM(d.F) AS 'Data_Free'
FROM dbo.##Tbl_Combi nedInfo d WHERE d.type = 'Data' GROUP BY d.DatabaseName ) AS x
JOIN
( SELECT l.DatabaseName,
SUM(l.T) AS 'Log',
SUM(l.U) AS 'Log_Used',
SUM(l.F) AS 'Log_Free'
FROM dbo.##Tbl_Combi nedInfo l WHERE l.type = 'Log' GROUP BY l.DatabaseName ) AS y
ON x.DatabaseName = y.DatabaseName;

IF @Unit = 'KB'
UPDATE @Tbl_Final SET TOTAL = TOTAL * 1024,
used = used * 1024,
free = free * 1024,
Data = Data * 1024,
Data_Used = Data_Used * 1024,
Data_Free = Data_Free * 1024,
Log = Log * 1024,
Log_Used = Log_Used * 1024,
Log_Free = Log_Free * 1024;

IF @Unit = 'GB'
UPDATE @Tbl_Final SET TOTAL = TOTAL / 1024,
used = used / 1024,
free = free / 1024,
Data = Data / 1024,
Data_Used = Data_Used / 1024,
Data_Free = Data_Free / 1024,
Log = Log / 1024,
Log_Used = Log_Used / 1024,
Log_Free = Log_Free / 1024;

DECLARE @GrantTotal dec(11, 2);
SELECT @GrantTotal = SUM(TOTAL) FROM @Tbl_Final;
/*
SELECT
CONVERT(dec(10, 2), TOTAL*100.0/@GrantTotal) AS 'WEIGHT (%)',
substring(Datab aseName,1,30) AS 'DATABASE',
CONVERT(VARCHAR (12), used) + ' (' + CONVERT(VARCHAR (12), [used (%)]) + ' %)' AS 'USED (%)',
[+],
CONVERT(VARCHAR (12), free) + ' (' + CONVERT(VARCHAR (12), [free (%)]) + ' %)' AS 'FREE (%)',
[=],
TOTAL,
[=],
CONVERT(VARCHAR (12), Data) + ' (' + CONVERT(VARCHAR (12), Data_Used) + ', ' +
CONVERT(VARCHAR (12), [Data_Used (%)]) + '%)' AS 'DATA (used, %)',
[+],
CONVERT(VARCHAR (12), Log) + ' (' + CONVERT(VARCHAR (12), Log_Used) + ', ' +
CONVERT(VARCHAR (12), [Log_Used (%)]) + '%)' AS 'LOG (used, %)'
FROM @Tbl_Final
WHERE DatabaseName LIKE ISNULL(@TargetD atabase, '%')
ORDER BY DatabaseName ASC;

select substring(Datab aseName , 1, 30 ) 'Database' , Data , '=' '=' , Data_Used , '+' '+' , Data_Free , '|' '|' , Log, '=' '=' , Log_Used, '+' '+' , Log_Free from @Tbl_Final
select substring(Datab aseName , 1, 20 ) 'Database' ,
substring(cast( Data as varchar(20)), 1 , 20) 'Data' , '=' '=' ,
substring(cast( Data_Used as varchar(20)), 1 , 20) 'Data Used' , '+' '+' ,
substring(cast( Data_Free as varchar(20)), 1 , 20) 'Data Free' , '|' '|' ,
substring(cast( Log as varchar(20)), 1 , 20) 'Log', '=' '=' ,
substring(cast( Log_Used as varchar(20)), 1 , 20) 'Log Used', '+' '+' ,
substring(cast( Log_Free as varchar(20)), 1 , 20) 'Log Free'
from @Tbl_Final
*/
print '============== =============='
print ' ' + substring(@@ser vername , 1, 30)
print '============== =============='
print ''
print '-------------------- ------------------------------------- ----------- ---- ------------------------------------- -----------'
select substring(Datab aseName , 1, 20 ) 'Database' ,
substring(
substring(cast( Data as varchar(20)), 1 , 20) + ' = ' +
substring(cast( Data_Used as varchar(20)), 1 , 20) + ' + ' +
substring(cast( Data_Free as varchar(20)), 1 , 20)
,1,37)'DATA = USED + FREE (MB)' ,
substring(cast([Data_Free (%)] as varchar(10)), 1 , 10) + '%' 'FREE(%)' , '|' '|' ,
substring(
substring(cast( Log as varchar(20)), 1 , 20) + ' = ' +
substring(cast( Log_Used as varchar(20)), 1 , 20) + ' + ' +
substring(cast( Log_Free as varchar(20)), 1 , 20)
,1,37)'LOG = USED + FREE (MB)' ,
substring(cast([Log_Free (%)] as varchar(10)), 1 , 10) + '%' 'FREE(%)'
from @Tbl_Final
print '-------------------- ------------------------------------- ----------- ---- ------------------------------------- -----------'
END
GO

------------------------------------------------------------------------------------------------------------------------
Jan 16 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
3698
by: Arni Snorri Eggertsson | last post by:
Hi I am trying to design an IO subsystem for my SQL Server and for that I need to try and predict IO activity on each table in my MSSQL Database. My idea is to move the hottest tables into special disk subsystem away from the less hotter tables. So far I have gathered that we have three tables more hot than the others but I have no feeling on ratio on how hot each is and how much activity is on the less hotter tables. I need to...
2
2104
by: Pablo | last post by:
C:\Program Files\Microsoft SQL Server\MSSQL\Data\" contains .mdb and ..ldb files that are huge, some reach 21GB! while backup files don't exceed 8MB. I guess it's a kind of transaction log, but I'm not sure. Can I erase them all and lose no information? Where I configure it to avoid it to happen in the future? Thanks in advance.
4
1645
by: Don | last post by:
I have MSSQL2k SP3a on WIN2k SP4. moved a Date/log files to this server about a week ago from a SQL7 server and attached it to this new Sql2k server. everything works fine for about 24hrs and then it starts timing out !! all I have to so is restart the MSSQL service and works fine again till the next day !
4
1189
by: bthrnds-ns2 | last post by:
hey guys--i need some expert advice! we have a Dell PE 2650 with 2GHz and 2MB RAM with 2000 Server. on it we have about 6 MSSQL DB's. a couple of the DB's are the back-end for a decent sized web app (5-10 users on average, 20 users at most). many times, the cpu skyrockets on our db server when using the web app. sometimes things timeout, other times not. at this time, upgrading the hardware is not an option. aside from increasing the...
27
20042
by: Aurangzeb M. Agha | last post by:
I'm running Postgres 7.1.3, and just started having a problem where my dynamic site is going down (read-only DB, with no writes happening to the DB) regularly (every other day). I have no idea whay this is happening, and my search of the FAQ's and mail list don't bring up anything. i've attached the error from the log file, at the end of this message. Here's an output of the disk usage from within the DB dir $ du -k . 1716 ...
16
1757
by: davemateer | last post by:
Hi We have a current system: Linux / Apache / PHP4.x talking to Microsoft SQL 2000 Thinking about going to: Windows 2003 / PHP4.x talking to the same Microsoft SQL 2000 box...
9
4414
by: dunleav1 | last post by:
Does enabling/disabling Data Execution Prevention have a performance impact on SQL 2000 or SQL 2005? For SQL best performance - how should I configure for: Processor Scheduling: Programs or Background services Memory Usage: Programs or System Cache
24
3511
by: rogynskyy | last post by:
Hi guys, I have to run a simple query on a MSSQL 2000. I'm new to database usage. I need to create a view with the following table columns LIST_PRICE decimal; MAP varchar(6); STREET_NO varchar(10);
0
917
by: stevegardner | last post by:
Dear members, Please help me in regarding my problem. I am very much confused on whom to blame for the problem........MSSQL? or .NET ? or Win server? I have a dedicated server with 2GB RAM, SQL 2005, 150GB bandwidth, 160 GB disk space etc., I have created a .net application that fetches data from different servers using their XML feed (wether report, stock data, etc.,). I programmed it in such a way that it gets the data using XML and...
0
8438
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8348
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8863
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8779
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8549
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8636
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6187
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5660
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
2004
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.