Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Data Management

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

    Search

    XML Feeds

    Google Ads

    « Getting the tables with the most rows from your databaseSSRS Properties - FixedColumnHeaders »
    comments

    To see all the databases with their size on an instance, you can use sp_helpdb. That works but returns the results in some random order. In my case I see master, model and
    msdb followed by a couple of user database, then tempdb and then again some user databases. What if I want to get the list returned order by size descending? This is pretty easy if you dump the results into a table and then do the sorting when doing the SELECT query against this table. The one thing you have to do is taking out MB from the db_size column and converting it to something numeric.

    Here is what the query looks like

    1. CREATE TABLE #test (name varchar(100), db_size varchar(100),owner varchar(100),db_id int,created varchar(100),status varchar(1000),compatibility_level int)
    2.  
    3. INSERT #test
    4. EXEC sp_helpdb
    5.  
    6. SELECT name,db_size,owner,db_id,created,compatibility_level
    7. FROM #test
    8. ORDER BY CONVERT(float,REPLACE(db_size,' MB','')) DESC
    9.  
    10. DROP TABLE #test

    And here is what you would see

    name		     db_size	owner		db_id	created	    compatibility_level
    TestBigger	    988.31 MB	Denis\Denis	8	Nov 26 2012	110
    TestSmaller	    710.31 MB	Denis\Denis	9	Nov 26 2012	110
    msdb		     38.00 MB	sa		4	Feb 10 2012	110
    master		     18.63 MB	sa		1	Apr  8 2003	110
    ReportServer	     12.94 MB	Denis\Denis	5	Aug 16 2012	110

    This "check" will be part of the informational section of SQLCop

    About the Author

    User bio imageDenis has been working with SQL Server since version 6.5. Although he worked as an ASP/JSP/ColdFusion developer before the dot com bust, he has been working exclusively as a database developer/architect since 2002. In addition to English, Denis is also fluent in Croatian and Dutch, but he can curse in many other languages and dialects (just ask the SQL optimizer) He lives in Princeton, NJ with his wife and three kids.
    Social SitingsTwitterFacebookLinkedInHomePageFlickrLTD RSS Feed
    670 views
    InstapaperVote on HN

    No feedback yet

    Leave a comment


    Your email address will not be revealed on this site.

    To mislead the spambots.

    Your URL will be displayed.
    (Line breaks become <br />)
    (Name, email & website)
    (Allow users to contact you through a message form (your email will not be revealed.)