在具有多个if / Case条件的sqlite更新触发器中

时间:2018-01-10 13:01:23

标签: sqlite if-statement triggers switch-statement case

在触发器中如何获得多个条件检查,如

CREATE TRIGGER record_big_order AFTER INSERT ON orders 
BEGIN 
IF new.value > 1000000 THEN 
  INSERT INTO big_orders (customer_name, salesman_id, value,Mark) 
                  VALUES (new.customer_name, new.salesman_id, new.value,'1') 
END IF; 
IF new.value > 2000000 THEN 
  INSERT INTO big_orders (customer_name, salesman_id, value,Mark) 
                  VALUES (new.customer_name, new.salesman_id, new.value,'2') 
END IF; 
IF new.value > 3000000 THEN 
  INSERT INTO big_orders (customer_name, salesman_id, value,Mark) 
                  VALUES (new.customer_name, new.salesman_id, new.value,'3')
END IF; 
END;

1 个答案:

答案 0 :(得分:2)

您可以尝试以下INSERT语句 -

INSERT INTO big_orders (customer_name, salesman_id, value,Mark) 
VALUES (new.customer_name, new.salesman_id, new.value,
        CASE WHEN new.value > 3000000 THEN '3'
             WHEN new.value > 2000000 THEN '2'
             WHEN new.value > 1000000 THEN '1' END)