0

I have two SQLite3 tables, A and B. When column A.X is updated, I want to modify B.Y, and when B.Y is updated, I want to modify A.X.

I can use two triggers:

CREATE TRIGGER AtoB AFTER UPDATE OF X ON A BEGIN UPDATE B SET Y = ...

and

CREATE TRIGGER BtoA AFTER UPDATE OF Y ON B BEGIN UPDATE A SET X = ...

but it seems that both triggers are called once, no matter which table I modify, i.e. one always invokes the other.

I only want one of them to execute, since the updates are lossy in the direction of A to B. I don't want the loss in the reverse direction B to A, but if both triggers fire, then it makes it lossy in both directions.

A simple solution would be to implement three UDFs "increment_trigger_depth", "decrement_trigger_depth", and "get_trigger_depth", and then use "WHEN trigger_depth == 1" in the update statements. But, is there an easier way to determine trigger depth in SQLite3?

4

1 に答える 1

1

Use a new table to hold the trigger depth.

CREATE TABLE trigger_depth (depth);
INSERT INTO trigger_depth VALUES (0);

Then for increment_trigger_depth use

UPDATE trigger_depth SET depth = depth + 1;

etc.

Use

... WHEN (SELECT depth FROM trigger_depth) <= 0 BEGIN ... 

to guard your trigger actions

于 2012-06-26T02:57:41.067 に答える