Fetch Connected DB Details | SSMS

Fetch Connected DB Details | SSMS

SELECT 
    @@SERVERNAME AS ServerName,
    DB_NAME() AS DatabaseName,
    CONVERT(VARCHAR(25), DATABASEPROPERTYEX(DB_NAME(), 'CreateDate'), 120) AS CreateDate,
    CONVERT(VARCHAR(25), DATABASEPROPERTYEX(DB_NAME(), 'LastBackupDate'), 120) AS LastBackupDate,
    DATABASEPROPERTYEX(DB_NAME(), 'Recovery') AS RecoveryModel,
    DATABASEPROPERTYEX(DB_NAME(), 'Status') AS Status,
    DATABASEPROPERTYEX(DB_NAME(), 'Updateability') AS Updateability,
    DATABASEPROPERTYEX(DB_NAME(), 'UserAccess') AS UserAccess,
    DATABASEPROPERTYEX(DB_NAME(), 'Version') AS Version,
    (SELECT SUM(size * 8 / 1024) FROM sys.database_files) AS TotalSizeMB,
    (SELECT SUM(CASE WHEN type_desc = 'ROWS' THEN size * 8 / 1024 ELSE 0 END) FROM sys.database_files) AS DataSizeMB,
    (SELECT SUM(CASE WHEN type_desc = 'LOG' THEN size * 8 / 1024 ELSE 0 END) FROM sys.database_files) AS LogSizeMB
Styled Table
ServerName DatabaseName CreateDate LastBackupDate RecoveryModel Status Updateability UserAccess Version TotalSizeMB DataSizeMB LogSizeMB
DEV_SRVR RepUAT3 NULL NULL FULL ONLINE READ_WRITE MULTI_USER 869 144597 125597 19000

You can also query sysdatabase to fetch Master DB file location –

select * from master.sys.sysdatabases
where 1=1 
and dbid = 13

If you know the DBID use the required id, else query using db_name ‘Name‘.

Styled Table
Name Dbid Sid Mode Status Status2 Crdate Reserved Category Cmptlevel Filename Version
RappUAT3 13 0x010500 0 65536 1090519040 2024-07-24 11:49:52.667 1900-01-01 00:00:00.000 0 140 F:\MSSQL\Data\RappUAT3.mdf 869

Leave a Reply

Your email address will not be published. Required fields are marked *