0

除了 notes 表之外,我还将这个相同的触发器应用于大约 15 个其他表。不确定,但似乎存储过程会消除重复的代码。这是存储过程的好应用程序吗?如果是这样,如何实施?

CREATE TRIGGER tg_notes_ins BEFORE INSERT ON notes FOR EACH ROW
BEGIN
    IF NEW.created_by_user IS NULL OR NEW.created_by_user = '' THEN
        SET NEW.created_by_user = @users_id;
    END IF;
    IF NEW.modified_by_user IS NULL OR NEW.modified_by_user = '' THEN
        SET NEW.modified_by_user = @users_id;
    END IF;
END$$

CREATE TRIGGER tg_notes_upd BEFORE UPDATE ON notes FOR EACH ROW
BEGIN
    IF NEW.modified_by_user = OLD.modified_by_user THEN
        SET NEW.modified_by_user = @users_id;
    END IF;
END$$
4

1 回答 1

0

我并不是说这是如何做到的,但它似乎有效,并且会感谢任何意见。谢谢

DELIMITER $$

CREATE PROCEDURE `createRecord` ()
BEGIN
    IF NEW.created_by_user IS NULL OR NEW.created_by_user = '' THEN
        SET NEW.created_by_user = @users_id;
    END IF;
    IF NEW.modified_by_user IS NULL OR NEW.modified_by_user = '' THEN
        SET NEW.modified_by_user = @users_id;
    END IF;
END$$

CREATE PROCEDURE `modifyRecord` ()
BEGIN
    IF NEW.modified_by_user = OLD.modified_by_user THEN
        SET NEW.modified_by_user = @users_id;
    END IF;
END$$

CREATE TRIGGER tg_notes_upd BEFORE UPDATE ON notes FOR EACH ROW
BEGIN
    CALL createRecord();
END$$

CREATE TRIGGER tg_notes_ins BEFORE INSERT ON notes FOR EACH ROW
BEGIN
    CALL modifyRecord();
END$$

DELIMITER ;
于 2013-04-18T01:11:10.243 回答