MySQL Check if table exists error

Posted by Max van Heiningen on Stack Overflow See other posts from Stack Overflow or by Max van Heiningen
Published on 2012-12-10T10:56:28Z Indexed on 2012/12/10 11:04 UTC
Read the original article Hit count: 128

Filed under:

I'm trying to check if a table already exists, however I can't get this working.

IF EXISTS (SELECT 1 
    FROM sysobjects 
    WHERE xtype='u' AND name='tablename') 
        SELECT 'table already exists.' 
ELSE 
BEGIN
     CREATE TABLE Week_(
             id INT(10)AUTO_INCREMENT PRIMARY KEY (id),
             ...
             ...)
             END; 

My error:

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IF EXISTS (SELECT 1 FROM sysobjects WHERE xtype='u' AND name' at line 1

Can someone help me with this?

Thanks in advance

© Stack Overflow or respective owner

Related posts about mysql