sql 组织结构插入时的MYSQL触发器

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql 组织结构插入时的MYSQL触发器相关的知识,希望对你有一定的参考价值。

DELIMITER |
CREATE TRIGGER insert_organization_data
AFTER INSERT ON organization_structures
FOR EACH ROW BEGIN
    INSERT INTO organization_structure_trees (parent_id, child_id, depth, path)
    VALUES (NEW.id, NEW.id, 0, CONCAT(NEW.name,"/"));

    INSERT INTO organization_structure_trees (parent_id, child_id, depth, path)
        SELECT parent_id, NEW.id, depth + 1, CONCAT(path, NEW.name, "/")
        FROM organization_structure_trees
        WHERE child_id = NEW.parent_id;
END; |
DELIMITER ;

BEGIN

    INSERT INTO organization_structure_trees (parent_id, child_id, depth, path, sequence_path)
    VALUES (NEW.id, NEW.id, 0, CONCAT(NEW.name,"/"), CONCAT(LPAD(CONCAT(NEW.id,'00'),12,'0'),"/ ")); 

    INSERT INTO organization_structure_trees (parent_id, child_id, depth, path, sequence_path)
        SELECT parent_id, NEW.id, depth + 1, CONCAT(path, NEW.name, "/"), CONCAT(sequence_path, CONCAT(LPAD(CONCAT(NEW.id,'00'),12,'0'),"/ "))
        FROM organization_structure_trees
        WHERE child_id = NEW.parent_id;
END

以上是关于sql 组织结构插入时的MYSQL触发器的主要内容,如果未能解决你的问题,请参考以下文章

插入另一个表时的MySQL触发器问题

由 CROSS APPLY 触发时的顺序 SQL 插入

如何使用触发器(MySQL)在插入时更改同一表的字段值

sql插入的操作

SQL数据库中一个表突然不能修改和插入,怎么解决?

触发器处理表更新