我有一个任务:在INSERT之前检查是否有相同ID的记录存在,为新记录设置time_dead()
BEGIN
IF NEW.id = OLD.id THEN
INSERT INTO temporary_objects(OLD.time_dead)
value(now());
END IF;
RETURN NEW;
END;
这不行,因为OLD和INSERT不能一起工作.但我没有看到其他的选择.谢谢。
你可以用几种方法处理这种情况。
INSERT INTO temporary_objects VALUES(NEW.*) ON CONFLICT DO UPDATE SET time_dead = now();
DECLARE
id_exists INTEGER;
BEGIN
SELECT id INTO id_exists FROM temporary_objects WHERE id = NEW.id;
IF NOT FOUND THEN
INSERT INTO temporary_objects VALUES(NEW.*);
ELSE
UPDATE temporary_objects SET time_dead = now() WHERE id = NEW.id;
RETURN NEW;
END IF;
RETURN NEW;
END;