SQLite在创建触发的条件下有条件

发布于 2025-01-23 02:13:31 字数 885 浏览 4 评论 0原文

我正在使用sqlite创建一个触发器,我想比较旧值是否与新值不同,因此我可以执行insert

这是我创建的触发器,但似乎MySQL语法没有在sqlite中工作。 等效是什么?

CREATE TRIGGER document_handler AFTER UPDATE ON document
FOR EACH ROW
BEGIN
    IF NEW.type <> OLD.type THEN
        INSERT INTO DocumentUpdates (id_document, attribute_changed, lastvalue, newvalue, modification_date)
        VALUES (id, "type", OLD.type, NEW.type, NOW());
    END IF;
    
    IF NEW.title <> OLD.title THEN
        INSERT INTO DocumentUpdates (id_document, attribute_changed, lastvalue, newvalue, modification_date)
        VALUES (id, "title", OLD.title, NEW.title, NOW());
    END IF;
    
    IF NEW.path <> OLD.path THEN
        INSERT INTO DocumentUpdates (id_document, attribute_changed, lastvalue, newvalue, modification_date)
        VALUES (id, "path", OLD.path, NEW.path, NOW());
    END IF;
END;

I'm creating a trigger using SQLite and I want to compare if the OLD value is different than the NEW value so I can execute an INSERT

This is the trigger I created but seems like the MySQL syntax does not work in SQLite.
What would be the equivalent?

CREATE TRIGGER document_handler AFTER UPDATE ON document
FOR EACH ROW
BEGIN
    IF NEW.type <> OLD.type THEN
        INSERT INTO DocumentUpdates (id_document, attribute_changed, lastvalue, newvalue, modification_date)
        VALUES (id, "type", OLD.type, NEW.type, NOW());
    END IF;
    
    IF NEW.title <> OLD.title THEN
        INSERT INTO DocumentUpdates (id_document, attribute_changed, lastvalue, newvalue, modification_date)
        VALUES (id, "title", OLD.title, NEW.title, NOW());
    END IF;
    
    IF NEW.path <> OLD.path THEN
        INSERT INTO DocumentUpdates (id_document, attribute_changed, lastvalue, newvalue, modification_date)
        VALUES (id, "path", OLD.path, NEW.path, NOW());
    END IF;
END;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤城病女 2025-01-30 02:13:31

在sqlite中,没有如果条件语句。

在这种情况下,您可以使用简单where条款:

CREATE TRIGGER document_handler AFTER UPDATE ON document
FOR EACH ROW
BEGIN
  INSERT INTO DocumentUpdates (id_document, attribute_changed, lastvalue, newvalue, modification_date)
  SELECT NEW.id, 'type', OLD.type, NEW.type, CURRENT_TIMESTAMP
  WHERE NEW.type IS NOT OLD.type;

  INSERT INTO DocumentUpdates (id_document, attribute_changed, lastvalue, newvalue, modification_date)
  SELECT NEW.id, 'title', OLD.title, NEW.title, CURRENT_TIMESTAMP
  WHERE NEW.title IS NOT OLD.title;
    
  INSERT INTO DocumentUpdates (id_document, attribute_changed, lastvalue, newvalue, modification_date)
  SELECT NEW.id, 'path', OLD.path, NEW.path, CURRENT_TIMESTAMP
  WHERE NEW.path IS NOT OLD.path;
END;

我更改了所有&lt;&gt;使用不是,它也可以比较null值。
另外,now()在sqlite中的等效词是current_timestamp

请参阅简化的 demo

In SQLite there is no IF conditional statement.

In this case you can use simple WHERE clauses:

CREATE TRIGGER document_handler AFTER UPDATE ON document
FOR EACH ROW
BEGIN
  INSERT INTO DocumentUpdates (id_document, attribute_changed, lastvalue, newvalue, modification_date)
  SELECT NEW.id, 'type', OLD.type, NEW.type, CURRENT_TIMESTAMP
  WHERE NEW.type IS NOT OLD.type;

  INSERT INTO DocumentUpdates (id_document, attribute_changed, lastvalue, newvalue, modification_date)
  SELECT NEW.id, 'title', OLD.title, NEW.title, CURRENT_TIMESTAMP
  WHERE NEW.title IS NOT OLD.title;
    
  INSERT INTO DocumentUpdates (id_document, attribute_changed, lastvalue, newvalue, modification_date)
  SELECT NEW.id, 'path', OLD.path, NEW.path, CURRENT_TIMESTAMP
  WHERE NEW.path IS NOT OLD.path;
END;

I changed all <> with IS NOT which can compare also NULL values.
Also, the equivalent of NOW() in SQLite is CURRENT_TIMESTAMP.

See a simplified demo.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文