将MySQL触发器转换为SQLite

时间:2016-11-11 00:08:20

标签: mysql sql sqlite

我想知道是否有人可以帮我翻译从MySQL到SQLite的更新前触发器。这是MySQL触发器:

CREATE TRIGGER trigger_name 
  BEFORE UPDATE ON `table_name` 
  FOR EACH ROW
BEGIN
  IF (NEW.fk IS NOT NULL AND NEW.status < 50) 
  THEN
    BEGIN
      DECLARE obj_count INT(11); 
      SELECT COUNT(*) 
      INTO obj_count
      FROM (SELECT id FROM table_name
                            WHERE `status` < 50 AND fk = NEW.fk
                            LIMIT 1) AS objs;
      IF (obj_count > 0) 
      THEN
        SIGNAL SQLSTATE '23000' SET MESSAGE_TEXT = "Error.";
      END IF;
    END;
  END IF;
END

我不应理解的是SIGNAL语句需要像这样重写:SELECT RAISE(ABORT, 'Error.')

但还有其他语法错误,我很难理解。

1 个答案:

答案 0 :(得分:0)

SQLite triggers既没有IF也没有DECLARE:

app2

CREATE TRIGGER trigger_name BEFORE UPDATE ON table_name FOR EACH ROW WHEN NEW.fk IS NOT NULL AND NEW.status < 50 BEGIN SELECT RAISE(ABORT, "Error.") WHERE EXISTS (SELECT id FROM table_name WHERE status < 50 AND fk = NEW.fk); END; 可能效率更高。)