2

I have a trigger that I want to insert the same random value into two tables. How do I do this?

CREATE TRIGGER insertTrigger AFTER INSERT ON TableAB 
BEGIN
    INSERT INTO TableA(id, num) VALUES(RANDOM(), 1);
    INSERT INTO TableB(id, num) VALUES(??, 1);
END;

I am not really using Random, but my own custom sqlite function which essentially does the same thing, but I need to remember that value to insert into TableB. How do I do that?

4

1 回答 1

2

SQLite 没有变量之类的东西,但您可以从刚刚插入第一个表的记录中读取值:

CREATE TRIGGER insertTrigger
AFTER INSERT ON TableAB
BEGIN
    INSERT INTO TableA(id, num) VALUES(RANDOM(), 1);
    INSERT INTO TableB(id, num) SELECT id, 1
                                FROM TableA
                                WHERE rowid = last_insert_rowid();
END;
于 2012-11-30T11:12:14.807 回答