我有两个表公司(id,company_name)和用户(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;
请帮帮我们。
全部谢谢。
答案 0 :(得分:0)
please refer this link, i think this will help you
http://stackoverflow.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 ;