Check if a SQL Server database exists or not (TSQL)

by Jon Hibbins on 1 August 2008
Filed under: Code, Database, MSSQL2005, Software Development, SQL Server, TSQL

Here is a usefull TSQL Script for checking if a database exists :

– Check if the Database Exists
DECLARE @DatabaseCount AS int
SELECT @DatabaseCount=COUNT(*) FROM master.dbo.sysdatabases WHERE name=‘master’
SELECT @DatabaseCount AS [DatabaseCount]

Leave a Reply