Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create database if db not exist

Tags:

sql

sql-server

I want to make SQL Server scritp for creating database if not exist.

IF NOT EXISTS(SELECT * FROM sys.databases WHERE name = 'DataBase')
  BEGIN
    CREATE DATABASE DataBase

    USE DataBase

    CREATE TABLE TableName (
        Id INT PRIMARY KEY IDENTITY (1, 1),
        Name VARCHAR(100)
    )

    --more tables here
    --some procedures here too

  END

From code above i getting this error:

Msg 911, Level 16, State 1, Line 5 Database 'DataBase' does not exist. Make sure that the name is entered correctly.

How to make database with tables and procedures when database not exist? I want to make it in one query

like image 745
michasaucer Avatar asked Jan 08 '20 08:01

michasaucer


People also ask

Which clause is used to create a database only if it doesn't already exist?

To create a database only if it doesn't already exist, which clause is used? Explanation: The 'CREATE DATABASE' statement supports many optional values. To create a database named 'my_db' only if it doesn't already exist, we write 'CREATE DATABASE IF NOT EXISTS my_db'.

Does Sequelize create database if not exists?

Sequelize + MySQL Database Wrapper Connects to MySQL server using the mysql2 db client and executes a query to create the database if it doesn't already exist.


3 Answers

Could you check the following script :

    IF NOT EXISTS(SELECT * FROM sys.databases WHERE name = 'DataBase')
  BEGIN
    CREATE DATABASE [DataBase]


    END
    GO
       USE [DataBase]
    GO
--You need to check if the table exists
IF NOT EXISTS (SELECT * FROM sysobjects WHERE name='TableName' and xtype='U')
BEGIN
    CREATE TABLE TableName (
        Id INT PRIMARY KEY IDENTITY (1, 1),
        Name VARCHAR(100)
    )
END
like image 108
Esat Erkec Avatar answered Oct 17 '22 19:10

Esat Erkec


Obviously you have to start with (and mind the GO here):

USE master
GO

But you have to do it like this:

IF NOT EXISTS (SELECT * FROM sys.databases WHERE name = 'MyTestDataBase')
BEGIN
  CREATE DATABASE MyTestDataBase;
END;
GO

Mind the GO again. If you don't use GO SSMS (or any other client) will still think your batch is not completed, your database is still not created and therefore not available for further use, and you get the error message you posted.

Now you can start using your just created database with:

USE MyTestDataBase;
GO

Again, mind the GO statement. In this case it is inadmissible because it is not possible to combine CREATE DATABASE and CREATE TABLE statements in one batch. So after the GO continue with:

IF OBJECT_ID('MyTestTable', 'U') IS NULL
BEGIN
  CREATE TABLE dbo.MyTestTable
  (
    Id   INT PRIMARY KEY IDENTITY(1, 1)
  , Name VARCHAR(100)
  );
END;

As already mentioned by others it is good practice to check if every table exists and do a create action if it doesn't and alter action if it does (or just do nothing). But if you really don't want to check if each table exists, for instance when you are sure you need to start from scratch, you could start with dropping the database if it exists:

IF EXISTS (SELECT * FROM sys.databases WHERE name = 'MyTestDataBase')
BEGIN
    DROP DATABASE MyTestDataBase;  
END;
CREATE DATABASE MyTestDataBase;
GO
like image 35
Thailo Avatar answered Oct 17 '22 18:10

Thailo


You can try this.

IF NOT EXISTS (
        SELECT *
        FROM sys.databases
        WHERE name = 'DataBase'
        )
BEGIN
    CREATE DATABASE [DataBase]
END
GO

USE [DataBase]
GO

IF NOT EXISTS (
        SELECT 1
        FROM sys.tables
        WHERE name = 'TableName'
            AND type = 'U'
        )
BEGIN
    CREATE TABLE TableName (
        Id INT PRIMARY KEY IDENTITY(1, 1)
        ,Name VARCHAR(100)
        )
END

enter image description here

like image 31
MOHIT AGARWAL Avatar answered Oct 17 '22 18:10

MOHIT AGARWAL