40

I have a T-SQL query which create database if it does not exist yet:

IF (NOT EXISTS (SELECT name 
                FROM master.dbo.sysdatabases 
                WHERE ('[' + 'DBName' + ']' = 'DBName'
                   OR name = 'DBName')))
BEGIN 
    CREATE DATABASE DBName

    PRINT 'DATABASE_CREATED' 
END 
ELSE 
    PRINT 'DATABASE_EXIST'

When I want use this in MySQL I get an error:

'IF' is not valid input at this postion

I change this script as

IF(SELECT COUNT(*) FROM SCHEMA_NAME 
   FROM INFORMATION_SCHEMA.SCHEMATA 
   WHERE SCHEMA_NAME = 'DBName') > 0)
THEN BEGIN 
    CREATE DATABASE DBName

    PRINT 'DATABASE_CREATED'
ELSE 
    PRINT 'DATABASE_EXIST'`

but it still doesn't work

How can I create this query in MySQL?

7
  • You can see this SO posts too - stackoverflow.com/questions/838978/… Commented Oct 30, 2016 at 20:25
  • @Drew they are asking how to do it in MySQL. Commented Oct 30, 2016 at 21:11
  • @MartinSmith but look at the rest of it, master.dbo.sysdatabases ... I have no clue what they are referencing other than mister softie tables Commented Oct 30, 2016 at 21:14
  • I wrote that i have that query in MSSQL Commented Oct 30, 2016 at 21:15
  • oh ok my bad. So you want this in mysql Commented Oct 30, 2016 at 21:16

2 Answers 2

127

I'm not sure exactly how you'd check, but if you just want to create it if it doesn't exist, then you can do

CREATE DATABASE IF NOT EXISTS DBname
Sign up to request clarification or add additional context in comments.

5 Comments

I know that that I can use IF NOT EXISTS but i need return string 'DATABASE_CREATED' or 'DATABASE_EXIST'
then make a stored proc out of it and pass a parameter
@Szymson It would also help if you knew what server type you have. I appears to not even be mysql so I retagged your question to microsoft
stored proc can be added to database but I need create database first
Right. A call to a stored proc in a helper db.
11

Here is the example in a helper (permanent) database. That db's name is permanent

One time db create:

create schema permanent;

Now make sure you

USE permanent;

then

Stored Proc:

DROP PROCEDURE IF EXISTS createDB;  
DELIMITER $$
CREATE PROCEDURE createDB(IN pDbName VARCHAR(100))  
BEGIN
    DECLARE preExisted INT;
    DECLARE ret VARCHAR(50);

    SET ret='DATABASE_EXIST';
    SELECT COUNT(*) INTO preExisted
    FROM INFORMATION_SCHEMA.SCHEMATA
    WHERE SCHEMA_NAME=pDbName;
    IF preExisted=0 THEN
        SET @sql=CONCAT('CREATE SCHEMA ',pDbName); -- add on any other parts of string like charset etc
        PREPARE stmt1 FROM @sql;
        EXECUTE stmt1;
        DEALLOCATE PREPARE stmt1;
        -- right here you could assume it worked or take additional
        -- step to confirm it
        SET ret='DATABASE_CREATED';
    END IF;
    SELECT ret as 'col1';
END$$
DELIMITER ;

Test:

use permanent;
call createDB('xyz');
-- returns col1 DATABASE_CREATED
call createDB('xyz');
-- returns col1 DATABASE_EXIST

2 Comments

Late question. Do you have something to say against CREATE DATABASE IF NOT EXISTS DBname ?
@ValerioBozz if one reads all parts of Q&A's above, there was confusion as to MSSQL or mysql, and from what context it was called from. If you find success with what you suggest then certainly run with it :) Also note that the OP wanted output for DATABASE_CREATED vs DATABASE_EXIST so I was attempting to stay on-topic

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.