---
title: "How to check  database exists or not in sqlserver"  
description: "How to check  database exists or not in sqlserver"  
author: "Anonymous User"  
published: 2016-01-17  
updated: 2016-01-17  
canonical: https://www.mindstick.com/forum/33887/how-to-check-database-exists-or-not-in-sqlserver  
category: "database"  
tags: ["sql server"]  
reading_time: 1 minute  

---

# How to check  database exists or not in sqlserver

I want to [check](https://yourviews.mindstick.com/story/2248/never-forget-to-check-these-specifications-before-buying-a-mobile-phone) [database](https://www.mindstick.com/articles/12226/use-of-database-in-sencha-extjs-and-insert-record-from-user-form-using-ajax) [exists](https://www.mindstick.com/forum/158947/how-do-you-use-the-exists-operator-to-check-for-the-existence-of-records-in-a-subquery) or not in [sqlserver](https://www.mindstick.com/forum/33737/between-condition-in-sqlserver) please help me.

## Replies

### Reply by Anonymous User

if we want to check database exists or not we can know four way.

```
-- Solution number (1)DECLARE @Database_Name varchar(100)SET @Database_Name='forum'IF EXISTS(SELECT * FROM sys.sysdatabases where name=@Database_Name)PRINT 'The database already exists'elsePRINT 'The database does not exist'-- Solution number (2)DECLARE @Database_Name varchar(100)SET @Database_Name='forum'IF EXISTS(SELECT * FROM master..sysdatabases WHERE name=@Database_Name)PRINT 'The database already exists'elseprint 'The database does not exist'-- Solution number (3)DECLARE @Database_Name varchar(100)SET @Database_Name='forum'DECLARE @sql varchar(1000)SET @sql='if ''?''='''+@Database_Name+''' print ''the database already alexists'''EXEC sp_msforeachdb @sql -- Solution number (4)DECLARE @Database_Name varchar(100)SET @Database_Name='forum'DECLARE @sql varchar(1000)SET @sql='if exists(select * from ?.information_schema.schemata wherecatalog_name='''+@Database_Name+''') print ''the database already exists'''EXEC sp_msforeachdb @sql
```

![How to check  database exists or not in sqlserver](https://www.mindstick.com/mindstickforums/80d41c68-d1da-4740-8ae2-2134ec88d204/images/52008038-8045-4c1a-8476-04caabaad29a.png)


---

Original Source: https://www.mindstick.com/forum/33887/how-to-check-database-exists-or-not-in-sqlserver

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
