一个触发器中的Sqlite3两个条件initcap

时间:2018-02-06 17:43:14

标签: sqlite triggers

我想请你帮忙。插入后我有两个触发器,它检查名称和姓氏的插入。如果名称或姓氏以小写字母开头,则触发更改名称和姓氏为正确格式。是否有可能从这两个触发器中产生一个触发器?

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;

1 个答案:

答案 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

即。

  • 如果 f 红色并且插入了Bloggs,则触发器会将其更改为 F 红色和Bloggs。
  • 如果插入了Fred和 bloggs ,则Trigger会将其更改为Fred和 B loggs。
  • 如果插入 f 红色 b ,则触发器会将其更改为 F 红色和 B loggs。< / LI>

但是,以下更简单的触发器也会这样做: -

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;