jeudi 28 janvier 2016

How exit a MySQL script if a database exists

I have a SQL script in docker, but I only want to run the script if it has not been run before. So, I though I would have something like:

IF dbname EXISTS
THEN
    exit;
END IF;

Looking around I managed to find code that will return 0 or 1 if the database exists:

SELECT IF(EXISTS(SELECT SCHEMA_NAME 
    FROM INFORMATION_SCHEMA.SCHEMATA 
    WHERE SCHEMA_NAME = 'mysql'),true,false);

but I cannot figure out how to turn this into an exit statement

IF(EXISTS (SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = 'mysql')
THEN
    exit;
END IF;

gives multiple syntax errors.

I am working with CentOS 6.7 and MySQL server 5.1.73 (forgot it is not MariaDB until CentOS 7)

Aucun commentaire:

Enregistrer un commentaire