我想请你帮忙。插入后我有两个触发器,它检查名称和姓氏的插入。如果名称或姓氏以小写字母开头,则触发更改名称和姓氏为正确格式。是否有可能从这两个触发器中产生一个触发器?
CREATE TRIGGER check_first_letter1
AFTER INSERT ON MEMBERS
WHEN (SUBSTR(NEW.name,1,1) = LOWER(SUBSTR(NEW.name,1,1))) BEGIN
UPDATE MEMBERS SET NAME=(select (UPPER(substr(NEW.name,1,1))||LOWER(substr(NEW.name,2,10)))) WHERE ID=NEW.ID;
END;
CREATE TRIGGER check_first_letter2
AFTER INSERT ON MEMBERS
WHEN (SUBSTR(NEW.surname,1,1) = LOWER(SUBSTR(NEW.surname,1,1))) BEGIN
UPDATE MEMBERS SET SURNAME=(select (UPPER(substr(NEW.surname,1,1))||LOWER(substr(NEW.surname,2,10)))) WHERE ID=NEW.ID;
END;
答案 0 :(得分:1)
我相信以下会做你想做的事: -
CREATE TRIGGER check_first_letter1
AFTER INSERT ON MEMBERS
BEGIN
UPDATE MEMBERS SET NAME=(select (UPPER(substr(NEW.name,1,1))||LOWER(substr(NEW.name,2,10))))
WHERE ID=NEW.ID AND (SUBSTR(NEW.name,1,1) = LOWER(SUBSTR(NEW.name,1,1)));
UPDATE MEMBERS SET SURNAME=(select (UPPER(substr(NEW.surname,1,1))||LOWER(substr(NEW.surname,2,10))))
WHERE ID=NEW.ID AND (SUBSTR(NEW.surname,1,1) = LOWER(SUBSTR(NEW.surname,1,1)));
END
即。
但是,以下更简单的触发器也会这样做: -
CREATE TRIGGER check_first_letter_always
AFTER INSERT ON MEMBERS
BEGIN
UPDATE MEMBERS SET
NAME=(select (UPPER(substr(NEW.name,1,1))||LOWER(substr(NEW.name,2,10)))),
SURNAME=(select (UPPER(substr(NEW.surname,1,1))||LOWER(substr(NEW.surname,2,10))))
WHERE ID=NEW.ID;
END;