Home > Link, Microsoft SQL Server, MSBI, Optimization, Query, Script, SQL Mentalist, SQL PraRup, SQL Query, SQL Server, Technology,, TSQL, Vishal Pawar > BI SQL # 263 : SQL Server DBA Scripts : All Database Information

BI SQL # 263 : SQL Server DBA Scripts : All Database Information

Hi Folks,

In this article we are going to cover How to find All Database Information.

In this post we are going to discuss following points:

  • Problem Statement of SQL Script:
  • Description of SQL Script:
  • SQL Script Output Column
  • SQL Script Code
  • User Level to execute

Problem Statement of SQL Script:

How to Find All Database Information ?

Description of SQL Script:

This script will Find All Database Information.

SQL Script Output Column

clip_image001 clip_image002

SQL Script Code

SELECT database_id
    ,CONVERT(VARCHAR(25), DB.NAME) AS dbName
    ,CONVERT(VARCHAR(10), DATABASEPROPERTYEX(NAME, 'status')) 
    AS [Status]
    ,state_desc
    ,(
        SELECT COUNT(1)
        FROM sys.master_files
        WHERE DB_NAME(database_id) = DB.NAME
            AND type_desc = 'rows'
        ) AS DataFiles
    ,(
        SELECT SUM((size * 8) / 1024)
        FROM sys.master_files
        WHERE DB_NAME(database_id) = DB.NAME
            AND type_desc = 'rows'
        ) AS [Data MB]
    ,(
        SELECT COUNT(1)
        FROM sys.master_files
        WHERE DB_NAME(database_id) = DB.NAME
            AND type_desc = 'log'
        ) AS LogFiles
    ,(
        SELECT SUM((size * 8) / 1024)
        FROM sys.master_files
        WHERE DB_NAME(database_id) = DB.NAME
            AND type_desc = 'log'
        ) AS [Log MB]
    ,user_access_desc AS [User access]
    ,recovery_model_desc AS [Recovery model]
    ,CASE compatibility_level
        WHEN 60
            THEN '60 (SQL Server 6.0)'
        WHEN 65
            THEN '65 (SQL Server 6.5)'
        WHEN 70
            THEN '70 (SQL Server 7.0)'
        WHEN 80
            THEN '80 (SQL Server 2000)'
        WHEN 90
            THEN '90 (SQL Server 2005)'
        WHEN 100
            THEN '100 (SQL Server 2008)'
        END AS [compatibility level]
    ,CONVERT(VARCHAR(20), create_date, 103) + ' ' + CONVERT(
        VARCHAR(20), create_date, 108) AS [Creation date]
    ,
    -- last backup
    ISNULL((
            SELECT TOP 1 CASE TYPE
                    WHEN 'D'
                        THEN 'Full'
                    WHEN 'I'
                        THEN 'Differential'
                    WHEN 'L'
                        THEN 'Transaction log'
                    END + ' – ' + LTRIM(ISNULL(STR(ABS(DATEDIFF(
                                    DAY, GETDATE(), 
                                    Backup_finish_date))) + 
                        ' days ago', 'NEVER')) + ' – ' + CONVERT(
                    VARCHAR(20), backup_start_date, 103) + ' ' + 
                CONVERT(VARCHAR(20), backup_start_date, 108) + 
                ' – ' + CONVERT(VARCHAR(20), backup_finish_date
                    , 103) + ' ' + CONVERT(VARCHAR(20), 
                    backup_finish_date, 108) + ' (' + CAST(
                    DATEDIFF(second, BK.backup_start_date, BK.
                        backup_finish_date) AS VARCHAR(4)) + ' ' 
                + 'seconds)'
            FROM msdb..backupset BK
            WHERE BK.database_name = DB.NAME
            ORDER BY backup_set_id DESC
            ), '-') AS [Last backup]
    ,CASE 
        WHEN is_fulltext_enabled = 1
            THEN 'Fulltext enabled'
        ELSE ''
        END AS [fulltext]
    ,CASE 
        WHEN is_auto_close_on = 1
            THEN 'autoclose'
        ELSE ''
        END AS [autoclose]
    ,page_verify_option_desc AS [page verify option]
    ,CASE 
        WHEN is_read_only = 1
            THEN 'read only'
        ELSE ''
        END AS [read only]
    ,CASE 
        WHEN is_auto_shrink_on = 1
            THEN 'autoshrink'
        ELSE ''
        END AS [autoshrink]
    ,CASE 
        WHEN is_auto_create_stats_on = 1
            THEN 'auto create statistics'
        ELSE ''
        END AS [auto create statistics]
    ,CASE 
        WHEN is_auto_update_stats_on = 1
            THEN 'auto update statistics'
        ELSE ''
        END AS [auto update statistics]
    ,CASE 
        WHEN is_in_standby = 1
            THEN 'standby'
        ELSE ''
        END AS [standby]
    ,CASE 
        WHEN is_cleanly_shutdown = 1
            THEN 'cleanly shutdown'
        ELSE ''
        END AS [cleanly shutdown]
FROM sys.databases DB
ORDER BY dbName
    ,[Last backup] DESC
    ,NAME

-- For Auto DBA
SELECT database_id
    ,CONVERT(VARCHAR(25), DB.NAME) AS dbName
    ,CONVERT(VARCHAR(10), DATABASEPROPERTYEX(NAME, 'status')) 
    AS [Status]
    ,state_desc
    ,(
        SELECT COUNT(1)
        FROM sys.master_files
        WHERE DB_NAME(database_id) = DB.NAME
            AND type_desc = 'rows'
        ) AS DataFiles
    ,(
        SELECT SUM((size * 8) / 1024)
        FROM sys.master_files
        WHERE DB_NAME(database_id) = DB.NAME
            AND type_desc = 'rows'
        ) AS [Data MB]
    ,(
        SELECT COUNT(1)
        FROM sys.master_files
        WHERE DB_NAME(database_id) = DB.NAME
            AND type_desc = 'log'
        ) AS LogFiles
    ,(
        SELECT SUM((size * 8) / 1024)
        FROM sys.master_files
        WHERE DB_NAME(database_id) = DB.NAME
            AND type_desc = 'log'
        ) AS [Log MB]
    ,user_access_desc AS [User access]
    ,recovery_model_desc AS [Recovery model]
    ,CASE compatibility_level
        WHEN 60
            THEN '60 (SQL Server 6.0)'
        WHEN 65
            THEN '65 (SQL Server 6.5)'
        WHEN 70
            THEN '70 (SQL Server 7.0)'
        WHEN 80
            THEN '80 (SQL Server 2000)'
        WHEN 90
            THEN '90 (SQL Server 2005)'
        WHEN 100
            THEN '100 (SQL Server 2008)'
        END AS [compatibility level]
    ,CONVERT(VARCHAR(20), create_date, 103) + ' ' + CONVERT(
        VARCHAR(20), create_date, 108) AS [Creation date]
    ,
    -- last backup
    ISNULL((
            SELECT TOP 1 CASE TYPE
                    WHEN 'D'
                        THEN 'Full'
                    WHEN 'I'
                        THEN 'Differential'
                    WHEN 'L'
                        THEN 'Transaction log'
                    END + ' – ' + LTRIM(ISNULL(STR(ABS(DATEDIFF(
                                    DAY, GETDATE(), 
                                    Backup_finish_date))) + 
                        ' days ago', 'NEVER')) + ' – ' + CONVERT(
                    VARCHAR(20), backup_start_date, 103) + ' ' + 
                CONVERT(VARCHAR(20), backup_start_date, 108) + 
                ' – ' + CONVERT(VARCHAR(20), backup_finish_date
                    , 103) + ' ' + CONVERT(VARCHAR(20), 
                    backup_finish_date, 108) + ' (' + CAST(
                    DATEDIFF(second, BK.backup_start_date, BK.
                        backup_finish_date) AS VARCHAR(4)) + ' ' 
                + 'seconds)'
            FROM msdb..backupset BK
            WHERE BK.database_name = DB.NAME
            ORDER BY backup_set_id DESC
            ), '-') AS [Last backup]
    ,CASE 
        WHEN is_fulltext_enabled = 1
            THEN 'Fulltext enabled'
        ELSE ''
        END AS [fulltext]
    ,CASE 
        WHEN is_auto_close_on = 1
            THEN 'autoclose'
        ELSE ''
        END AS [autoclose]
    ,page_verify_option_desc AS [page verify option]
    ,CASE 
        WHEN is_read_only = 1
            THEN 'read only'
        ELSE ''
        END AS [read only]
    ,CASE 
        WHEN is_auto_shrink_on = 1
            THEN 'autoshrink'
        ELSE ''
        END AS [autoshrink]
    ,CASE 
        WHEN is_auto_create_stats_on = 1
            THEN 'auto create statistics'
        ELSE ''
        END AS [auto create statistics]
    ,CASE 
        WHEN is_auto_update_stats_on = 1
            THEN 'auto update statistics'
        ELSE ''
        END AS [auto update statistics]
    ,CASE 
        WHEN is_in_standby = 1
            THEN 'standby'
        ELSE ''
        END AS [standby]
    ,CASE 
        WHEN is_cleanly_shutdown = 1
            THEN 'cleanly shutdown'
        ELSE ''
        END AS [cleanly shutdown]
INTO AllDatabaseInfo
FROM sys.databases DB
ORDER BY dbName
    ,[Last backup] DESC
    ,NAME

User Level to execute

300

    Hope you will like How to find All Database Information.

    If you really like reading my blog and understood at least few thing then please don’t forget to subscribe my blog.

If you want daily link and analysis or interesting link go to following website which will give @ your inbox please subscribe our following link resource blog :

Link Resource Website

For More information related to BI World visit my Mentalist Blog

SQL Server Mentalist >> SQL Learning Blog

Business Intelligence Mentalist >> BI World

Infographic Mentalist >> Image worth explaining thousand Words

Microsoft Mentalist >> MVC,ASP.NET, WCF & LinQ

DBA Mentalist >>Advance SQL Server Blog

Microsoft BI Mentalist >> MS BI Development Update

Connect With me on

| FaceBook |Twitter | linkedIn| Google+ | WordPress | RSS |

Advertisements
  1. March 29, 2014 at 3:44 pm

    Great Script but you need to add this below part for this first select
    WHEN 110
    THEN ‘110 (SQL Server 2012’
    also about the Second Select it’s return this below error
    Msg 2714, Level 16, State 6, Line 121
    There is already an object named ‘AllDatabaseInfo’ in the database.

    thanks

    • April 6, 2014 at 10:24 am

      Sure Eng.Motafa Elmasry ! Thanks for adding this.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: