我的语法有问题。 我需要在sqlite中使用IF,我替换IF - CASE 我的陈述不起作用,我不知道,有什么不对?
CASE
WHEN EXISTS
(SELECT sql FROM sqlite_master WHERE name = 'TEST_TABLE' AND type = 'table' AND sql LIKE '%aaaa%')
THEN
INSERT INTO atabela_new(id, idMain, idHistory, aNew) SELECT id, idMain, idHistory, aNew FROM a_tabela
ELSE
INSERT INTO atabela_new(id, idMain, idHistory) SELECT id, idMain, idHistory FROM a_tabela;
答案 0 :(得分:3)
假设aNew
的默认值为NULL:
INSERT INTO atabela_new(id, idMain, idHistory, aNew)
SELECT id,
idMain,
idHistory,
CASE
WHEN EXISTS
(SELECT sql FROM sqlite_master WHERE name = 'TEST_TABLE' AND type = 'table' AND sql LIKE '%aaaa%')
THEN aNew
ELSE NULL
END
FROM a_tabela
否则,您需要将该逻辑放在应用程序代码中。
答案 1 :(得分:-2)
如果您要做的只是插入一条语句(如果为true)或另一条语句(如果为false),为什么不使用if语句呢?
IF EXISTS(SELECT sql FROM sqlite_master WHERE name = 'TEST_TABLE' AND type = 'table' AND sql LIKE '%aaaa%')
BEGIN
INSERT INTO atabela_new(id, idMain, idHistory, aNew) SELECT id, idMain, idHistory, aNew FROM a_tabela
END
ELSE
INSERT INTO atabela_new(id, idMain, idHistory) SELECT id, idMain, idHistory FROM a_tabela;
END