0
在這裏我有一個工作的MySQL觸發器:Mysql觸發器優化。多個表列的IF(OLD.data <> NEW.data)
DROP TRIGGER IF EXISTS trg_after_update_tbl1;
DELIMITER //
CREATE TRIGGER trg_after_update_tbl1
AFTER UPDATE ON db1.tbl1 FOR EACH ROW
BEGIN
DECLARE strOld VARCHAR(255);
DECLARE strNew VARCHAR(255);
SET strOld = '';
SET strNew = '';
IF (NEW.colA <> OLD.colA) THEN
SET strOld = CONCAT(strOld, 'colA: ',OLD.colA,'; ');
SET strNew = CONCAT(strNew, 'colA: ',NEW.colA,'; ');
END IF;
IF (NEW.colB <> OLD.colB) THEN
SET strOld = CONCAT(strOld, 'colB: ',OLD.colB,'; ');
SET strNew = CONCAT(strNew, 'colB: ',NEW.colB,'; ');
END IF;
IF (NEW.colC <> OLD.colC) THEN
SET strOld = CONCAT(strOld, 'colC: ',OLD.colC,'; ');
SET strNew = CONCAT(strNew, 'colC: ',NEW.colC,'; ');
END IF;
-- AND SO MANY COLUMN CLAUSES ON....
INSERT INTO db1.changes (user, tableName, oldData, newData, date)
VALUES (
user(),
'tableA',
strOld,
strNew,
now()
);
END//
DELIMITER ;
是否有可能表的列名分配給一個變量(可以說X)從列表,數組或類似的東西,遍歷它並使用一個IF子句?像這樣(僞):
BEGIN LOOP
IF (NEW.X <> OLD.X) THEN
SET strOld = CONCAT(strOld, X, ': ',OLD.X,'; ');
SET strNew = CONCAT(strNew, X, ': ',NEW.X,'; ');
END IF;
END LOOP
「這麼多專欄......」 - 255會夠嗎? –
是的,它實際上:D –