我在我的db中创建了这个触发器:
CREATE TRIGGER `mytesttrigger`
AFTER INSERT ON `x`
FOR EACH ROW
INSERT INTO test (test.y) VALUES (NEW.r)
实际上我想从这个触发器中将这些数据添加到特定的表中,如(test1,test2等)取决于NEW.r值。
创建触发器mytesttrigger
插入后x
对于每一行
INSERT INTO(test.y)VALUES(NEW.r)
注意:假设所有表都存在(table1,table2等)
答案 0 :(得分:0)
execute
子句可以帮助您进行动态SQL。
假设您的表格X
只有列r
CREATE TRIGGER `mytesttrigger`
AFTER INSERT ON `x`
FOR EACH ROW
IF NEW.r = "SOLUTION1" THEN
SET @query = "insert into TABLE1 values (NEW.r)" ;
ELSIF NEW.r = "SOLUTION2" THEN
SET @query = "insert into TABLE2 values (NEW.r)" ;
END IF;
PREPARE stmt FROM @query;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;