Wednesday, June 7, 2017

How can I get databse names in SQL Server Exist or Not.



Before the creating new database, here I want to check that any database is not created or not previously same name.

Generally we have used a simple command to create database like :

Syntax:
Create Database DB_Name;

Example:
Create Database TestDB


The important thing is this before this, let it check that the database I am going to create, is already exist or not in the master database or in our database previously.
For this I am writing below query to check database name exist or not :

Example:
Select * From master.dbo.sysdatabases
By above query you see all the database that you have created in your database.
Now finally, I am writing exact query that never give you error while the executing the query.


If Not Exists (Select * From master.dbo.sysdatabases Where Name=' TestDB')
Begin
                Create Database TestDB
End
Go

No comments:

Post a Comment