In this SQL Tutorial I will show you how to using SQL Check if database exists. For the purpose of this tutorial I will use SQL Server 2008 R2 but the code should work in SQL Server 2005 and 2012.
The easiest way to check if database exists using T-SQL is to use the following T-SQL Statement:
DECLARE @db_name varchar(50)
SET @db_name ='AdventureWorks'
IF Exists(Select * From sys.sysdatabases where name=@db_name)
Print 'The database exists'
Print 'the database does not exist'
The code above uses sys.sysdatabases object and filters it for the database name we specified and using IF Exists we can conditionally execute additional code.
I hope this tutorial gives you a simple examples of SQL Check if database exists that you can take and use for your own purposes