我有一个 Translation 表,我希望对它的每次更新都自动记录到 TranslationHistory 表中。
我为此目的创建了这个触发器。
CREATE TRIGGER TranslationUpdate AFTER UPDATE ON Translation FOR EACH ROW BEGIN
DECLARE N DATETIME;
DECLARE ActionString VARCHAR(1000);
SET N = now();
SET ActionString = '';
IF NEW.TranslatorID <> OLD.TranslatorID THEN
SET ActionString = CONCAT(ActionString, 'TranslatorID(', OLD.TranslatorID, ' -> ', NEW.TranslatorID, ') ');
END IF;
IF NEW.StartDate <> OLD.StartDate THEN
SET ActionString = CONCAT(ActionString, 'StartDate(', OLD.StartDate, ' -> ', NEW.StartDate, ') ');
END IF;
IF NEW.DueDate <> OLD.DueDate THEN
SET ActionString = CONCAT(ActionString, 'DueDate(', OLD.DueDate, ' -> ', NEW.DueDate, ') ');
END IF;
IF NEW.EndDate <> OLD.EndDate THEN
SET ActionString = CONCAT(ActionString, 'EndDate(', OLD.EndDate, ' -> ', NEW.EndDate, ') ');
END IF;
IF NEW.Notes <> OLD.Notes THEN
SET ActionString = CONCAT(ActionString, 'Notes(', OLD.Notes, ' -> ', NEW.Notes, ') ');
END IF;
INSERT INTO TranslationHistory (TranslationID, Date, Action)
VALUES (NEW.ID, N, ActionString);
END;
我不太喜欢它,因为它太长、太乏味、太重复了,而且我敢肯定它不是好的 SQL。我想要一种 DRYer 方法来做到这一点。有没有办法做到这一点而不必手动指定每列的详细信息?