问题
this is my CASE/WHEN statement. But as you may see I get this error. I don't know why. All I want to do is detect when something in field MAJKA is changed. So if some other fields of column MAJKA are empty don't touch them, but change the value to new value of those fields of column MAJKA which are NOT empty.
SQL Error: near "UPDATE": syntax error <CREATE TRIGGER [test]
AFTER UPDATE OF [MAJKA]
ON [unos_golub]
FOR EACH ROW
BEGIN
SELECT majka,
CASE WHEN majka=''
THEN
(UPDATE unos_golub SET broj_goluba=NEW.majka WHERE broj_goluba=OLD.majka)
ELSE
(UPDATE unos_golub SET broj_goluba=NEW.majka WHERE broj_goluba=OLD.majka
UPDATE unos_golub SET majka=NEW.majka WHERE majka=OLD.majka)
END
FROM unos_golub;
回答1:
SQLite has no general mechanism to execute commands conditionally.
However, in triggers, you can use a WHEN condition on the entire trigger:
CREATE TRIGGER test_for_broj_goluba
AFTER UPDATE OF majka ON unos_golub
BEGIN
UPDATE unos_golub SET broj_goluba=NEW.majka WHERE broj_goluba=OLD.majka;
END;
CREATE TRIGGER test_for_majka
AFTER UPDATE OF majka ON unos_golub
WHEN NEW.majka <> ''
BEGIN
UPDATE unos_golub SET majka=NEW.majka WHERE majka=OLD.majka;
END;
(The first UPDATE is the same in both cases and thus does not need a WHEN.)
回答2:
You're putting data manipulation (DML) statements where you need an expression that returns a value. Consider:
SELECT majka,
CASE WHEN majka=''
THEN
'it is empty'
ELSE
'it is not empty'
END
This will give you a column where the value is either 'it is empty' or 'it is not empty', depending on the value in the previous column.
You can't put a DML statement in that spot in the SQL -- the DML statement is not an expression with a value.
来源:https://stackoverflow.com/questions/13654982/sqlite-case-when-statement