简体   繁体   English

在插入触发器后如何编写触发器以根据第一个表的值将数据插入到另一个表中

[英]how to writer after insert triggers to insert data into another table based on the values of first table

I want to create a trigger after insert on one table which would insert values into another table based upon the values of the first table.Here is what i have done and its not working. 我想在一个表上插入后创建一个触发器,该触发器将根据第一个表的值将值插入到另一个表中。这是我已经做过的并且不起作用。

CREATE TRIGGER add_into_alerts
AFTER INSERT ON in_sims_responses
FOR EACH ROW 
IF new.sim_response_code='102'
THEN
INSERT INTO bf_alters(company_id,description,event_datetime)
VALUES (NEW.sim_company_id,CONCAT(new.sim_msisdn,'Not Subscribed'),NOW());
END IF

For multiple-statements triggers you need to wrap your statements between BEGIN and END . 对于多语句触发器,您需要将语句包装在BEGINEND之间。 But you can avoid that by making it a single-statement trigger. 但是您可以通过使其成为单语句触发器来避免这种情况。 In your case you can use a conditional insert: 在您的情况下,您可以使用条件插入:

INSERT INTO bf_alters(company_id,description,event_datetime)
    SELECT NEW.sim_company_id, CONCAT(new.sim_msisdn,'Not Subscribed'),NOW()
    FROM dual
    WHERE new.sim_response_code='102';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM