SQLite Forum

Error with upsert and trigger WHEN clause?
Login
Your trigger is in error.  It refers to a column `name` that does not exist.  One presumes that you meant:

```
CREATE TRIGGER uppercase_name
  AFTER UPDATE ON Items
  WHEN lower(new.name) != upper(new.name)
  BEGIN
    UPDATE Items SET name = upper(new.name);
  END;
```

However this is an ill-conceived trigger.  

Firstly, it fires after any update of the `Items` table whenever the lowercase version is not equal to the uppercase version of the new name.  Since this condition is ALWAYS True unless the new name is NULL, it does not really do anything at all to restrict when the trigger fires -- it will always fire on all updates of anything in the `Items` table except when the `name` is updated to NULL.

Secondly, it updates every row of the `Items` table and sets every `name` to the uppercased name.

If recursive triggers are in effect, this will cause an endless loop which will be terminated only by the heat death of the multiverse.

What you probably meant is:

```
CREATE TRIGGER uppercase_name
  AFTER UPDATE OF name ON Items
  WHEN new.name IS NOT upper(new.name)
  BEGIN
    UPDATE Items SET name = upper(new.name) where rowid == new.rowid;
  END;
```

which will only fire when the column `name` of the table `Items` has been updated, and then only when the new name is not the uppercase of the new name (which means it will not fire if the new name is the uppercase of the new name or if the new name is NULL) and then it will ONLY update the column `name` of the `Items` table to be the uppercase of the new name for only the row which was updated and caused the trigger to fire.

It will work even if recursive triggers are enabled without causing the heat death of the multiverse.