2013-12-21 138 views
1
DELIMITER // 
CREATE TRIGGER never_update_with_null 
BEFORE UPDATE 
ON `table_name` 
FOR EACH ROW 
BEGIN 
    IF OLD.name IS NOT NULL AND NEW.name IS NULL THEN 
     SET NEW.name = OLD.name; 
    IF OLD.phone IS NOT NULL AND NEW.name IS NULL THEN 
     SET NEW.phone = OLD.phone; 
    IF OLD.place IS NOT NULL AND NEW.place IS NULL THEN 
     SET NEW.place = OLD.place; 
END; // 
DELIMITER ; 

我努力實現以下目標: 如果有人UPDATE SA值name,在表table_namephoneplace(列),並將其值(更新值)NULL那麼該值保持舊的(不會更新)。 也許有點短:從不用NULL更新。的MySQL創建觸發器語法

上述SQL不會創建觸發器,但它不會引發任何錯誤(phpMyAdmin)。它缺少語法嗎?每OLD.column;之後已經嘗試過ENDIF; - 沒有任何改進。謝謝你的解釋。

+1

嘗試結束。如果不是ENDIF – Mihai

回答

3

嘗試

DELIMITER // 
CREATE TRIGGER never_update_with_null 
BEFORE UPDATE 
ON `table_name` 
FOR EACH ROW 
BEGIN 
    IF OLD.name IS NOT NULL AND NEW.name IS NULL THEN 
     SET NEW.name = OLD.name; 
    END IF; 
    IF OLD.phone IS NOT NULL AND NEW.phone IS NULL THEN 
     SET NEW.phone = OLD.phone; 
    END IF; 
    IF OLD.place IS NOT NULL AND NEW.place IS NULL THEN 
     SET NEW.place = OLD.place; 
    END IF; 
END; // 
DELIMITER ; 
+0

對不起不一致,固定它。見問題。 TY – Thommy