I’m trying to set on insert, or update, an attribute of a point layer with an attribute derived from a polygon layer (12-digit watershed identifier) in the same database. So this is using SpatiaLite (a spatial extension of SQLite), but that’s a detail and this should simply be a SQL problem.
So, I created two triggers below. The UPDATED_ON portion where it sets the timestamp of the action works great. But the spatial parts always assign the same value regardless of where the point occurs as illustrated in the attached example. It seems to be performing the same test for all the rows, with input from one row, but applying to the wrong row. So, something in the SQL of the trigger must be grabbing the wrong point to perform the operation with. Is it the new.rowid part?
CREATE TRIGGER INSERT_UDT_HUC12 AFTER INSERT ON mypoints
BEGIN
UPDATE mypoints SET UPDATED_ON = DATETIME('NOW') WHERE rowid = new.rowid;
UPDATE mypoints SET WBD_HUC12 =
(SELECT HUC_12 FROM cumberland_huc12, mypoints
WHERE st_intersects(mypoints.Geometry, cumberland_huc12.Geometry))
WHERE mypoints.rowid = new.rowid;
END;
CREATE TRIGGER UPDATE_UDT_HUC12 AFTER UPDATE ON mypoints
BEGIN
UPDATE mypoints SET UPDATED_ON = DATETIME('NOW') WHERE rowid = new.rowid;
UPDATE mypoints SET WBD_HUC12 =
(SELECT HUC_12 FROM cumberland_huc12, mypoints
WHERE st_intersects(mypoints.Geometry, cumberland_huc12.Geometry))
WHERE mypoints.rowid = new.rowid;
END

The subquery that returns the new watershed identifier is as follows:
Note that this returns all watersheds that intersect any point in
mypoints.Therefore, your trigger will receive the one that happens to be the first in this query.
You have to use the filter
WHERE mypoints.rowid = new.rowidalso in the subquery.