I'm trying to write a kind of update SQL to add new columns if they don't already exist.
I've already tried a few things, but nothing works.
If I try an IF NOT EXISTS I always get a syntax error that I can't understand.
IF NOT EXISTS(
SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'nutzer'AND table_schema = 'restdb' AND column_name = 'api_nutzer')
THEN
ALTER TABLE `nutzer` ADD `api_nutzer` TINYINT;
END IF;
[SQL] IF NOT EXISTS( SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'nutzer'
AND table_schema = 'restdb'
AND column_name = 'api_nutzer') THEN
ALTER TABLE `nutzer` ADD `api_nutzer` TINYINT;
[Err] 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 NOT EXISTS( SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
' at line 1
If I execute the SELECT directly, I get a result
Does anyone have an idea or know where my mistake is?
I use MySQL Server Version 8.0.22
question from:
https://stackoverflow.com/questions/65649436/if-not-exists-syntaxerror-in-mysql 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…