插入触发器后创建视图
Posted
技术标签:
【中文标题】插入触发器后创建视图【英文标题】:Create view after insert trigger 【发布时间】:2015-08-05 05:15:59 【问题描述】:我有两张表 company (id,company_name) 和 users (id,company_id,user_name);
现在我想在公司表中插入数据时创建一个视图。
我使用了以下两种方法,但在这两种情况下都出现了相同的错误。错误是:
#1422 - Explicit or implicit commit is not allowed in stored function or trigger.
第一个查询是:
DELIMITER $$
CREATE TRIGGER testInsert
AFTER INSERT ON companies
FOR EACH ROW BEGIN
CREATE VIEW test(user_name) AS
SELECT user_name
FROM users;
END$$
DELIMITER ;
然后我尝试先创建过程并像这样在触发器中调用该过程
create procedure createView()
create view vt as select * from users;
CREATE TRIGGER `testInsert3` AFTER INSERT ON `companies`
FOR EACH ROW
BEGIN
CALL createView();
END;
请帮帮我。
谢谢大家。
【问题讨论】:
【参考方案1】: please refer this link, i think this will help you
http://***.com/questions/16256250/create-view-in-a-trigger
try creating the view like this
DELIMITER $$
CREATE TRIGGER testInsert
AFTER INSERT ON companies
FOR EACH ROW BEGIN
EXECUTE('CREATE VIEW test(user_name) AS SELECT user_name
FROM users')
END$$
DELIMITER ;
【讨论】:
以上是关于插入触发器后创建视图的主要内容,如果未能解决你的问题,请参考以下文章