SQL Server Backup File info using T-SQL Query

Few days back, I was asked this question. Using T-SQL is there a way to find the backup details about a database? Including physical location of the database where the backup of a particular database was performed. I did recollect that I wrote such a T-SQL query sometime back to find the backup locations of database backup. I came up with the below query that helped finds the backup information of a particular database.

Find the T- SQL Script at the end of the post. Remember to change the value of the local variable @DBNAME when running in your system. The query has been tested in SQL Server 2008.

The output of the query was wide enough not to fit in the image, hence it has been broken into 2 and displayed. The query displays the details in the reverse chronology, i.e the most recent backup first.

T-SQL Query used in the article

DECLARE @DBNAME VARCHAR(128)

SET @DBNAME = 'DATABASENAME'

SELECT A.database_name, B.physical_device_name
,A.media_set_id,A.backup_size,
A.backup_start_date,A.backup_finish_date
FROM msdb.dbo.backupset A
INNER JOIN msdb.dbo.backupmediafamily B
ON A.media_set_id = B.media_set_id
WHERE A.Database_Name= @DBNAME
ORDER BY A.backup_finish_date DESC

Do you like this site? Like our FB page @ Facebook.com\LearnSQLWithBru so that, you know when there is a new blog post.

— Bru Medishetty

Find Transaction Log Space Used

To query the space used by the transaction logs use the command, use DBCC SQLPERF(LOGSPACE). The command displays the Database Name, Log Size in Mega Bytes, the % of  log file used, remember the value is not the size it is the percentage used from the actual space of the log files. The results displayed is for all databases on the SQL Server instance.

Find the script below…

DBCC SQLPERF(LOGSPACE)

Monitoring transaction log files for the free space is a one of the very important tasks for a DBA.

Do you like this site? Like our FB page @ Facebook.com\LearnSQLWithBru so that, you know when there is a new blog post.

— Bru Medishetty

How to know the SQL Server Version

In order to know the SQL Server and Edition details you would be possibly looking at the SQL Server properties and find out the details. Occasionally it might be needed to check the same as part of a job / or a script, you need to query the SQL Server for this info. In order to do this you can use @@Version to retrieve the installation information of SQL Server.

The following images displays the output from the same T-SQL command when run on different types of SQL installations.

The above result indicates the SQL Server is 2008 Developer Edition running 64-bit version of the SQL Server. Note that it displays some details about the operating system too. Here in the above case it is Windows Server 2008 and the machine is a Virtual Machine.

The above result is displayed when executed on an instance running SQL Server 2000 Enterprise Edition hosted by a Windows Server 2003. Note that it displays the Service Pack 2 for the Windows Server 2003 and the service pack information is not for the SQL Server.

It is through experience we know how to identify the Windows Server Version (2003 or 2008) by looking at the Windows NT 5.2 (for 2003) and 6.1 (for 2008) etc.

Use the following script.

SELECT @@VERSION AS [VERSION DETAILS]

Do you like this site? Like our FB page @ Facebook.com\LearnSQLWithBru so that, you know when there is a new blog post.

— Bru Medishetty