Size Distribution of databases

Source: Internet
Author: User

---database size distribution

SET NoCount on

CREATE TABLE #DBsize

(

[DatabaseName] [nvarchar] (a) Not NULL,

[Size] [Decimal] Not NULL,

[Name] [nvarchar] (a) Not NULL,

[Filename] [nvarchar] (+) Not NULL

)

CREATE TABLE #drives

(

[Drive] [Char] (5) Not NULL,

[Mbfree] [Decimal] Not NULL

)

INSERT into #DBsize

EXEC sp_msforeachdb ' Select '? ' As DatabaseName, case when [?]. Sysfiles.size * 8/1024 = 0 Then 1 Else [?]. Sysfiles.size * 8/1024 End

As size,[?]. Sysfiles.name,

[?].. Sysfiles.filename from [?]. Sysfiles '

INSERT into #drives

EXEC xp_fixeddrives

SELECT @ @Servername As server name,

Count (DISTINCT RTRIM (DatabaseName as VARCHAR)) as database number,

Drive as [total data space used],

CAST (SUM (size) as VARCHAR) as [total size (MB)],

CAST (Mbfree as VARCHAR) as [remaining size (MB)]

From #DBsize

INNER JOIN #drives on left (#DBsize. Filename, 1) = #drives. Drive

GROUP by Drive,

Mbfree

SELECT RTRIM (CAST (DatabaseName as VARCHAR ()) as [database name],

Drive as [disk],

filename as [filename],

CAST (size as INT) as [Size (MB)],

CAST (Mbfree as VARCHAR) as [remaining size (MB)]

From #DBsize

INNER JOIN #drives on left (#DBsize. Filename, 1) = #drives. Drive

GROUP by DatabaseName,

Drive,

Mbfree,

Filename,

CAST (Size as INT)

order BY [disk character],

[Remaining size (MB)] DESC

DROP TABLE #DBsize

DROP TABLE #drives

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/database/SQLServer/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.