0

Error: Syntax Error at line 4

CREATE TABLE `${tempVars("id")}` AS
SELECT 'UNIXTIME', 'TODO', 'IMPORTANCE' 
FROM `${tempVars("id")}`
IF NOT EXISTS `${tempVars("id")}`;

Error: Syntax Error at line 1

IF (EXISTS (SELECT *
 FROM INFORMATION_SCHEMA.TABLES
 WHERE TABLE_SCHEMA = 'todolists'
 AND TABLE_NAME = `${tempVars("id")}`))
BEGIN
 SELECT * FROM `${tempVars("id")}`
END
ELSE
BEGIN
 CREATE TABLE `${tempVars("id")}` AS
   SELECT UNIXTIME, TODO, IMPORTANCE
   FROM `${tempVars("id")}`
END

I've tried a lot of things from existing questions, yet everything seems to error entirely. Not sure what's wrong at all, and haven't been able to fix, despite help from people much more experienced than me.

1 Answers1

0

The IF NOT EXISTS option goes at the beginning of CREATE TABLE, not at the end.

CREATE TABLE IF NOT EXISTS new_table
SELECT UNIXTIME, TODO, IMPORTANCE
FROM old_table

Note also that you shouldn't put single quotes around the column names in SELECT, that will return literal strings, not the contents of the columns. Use backticks if you need to quote the column names. See When to use single quotes, double quotes, and backticks in MySQL

Barmar
  • 741,623
  • 53
  • 500
  • 612