gpt4 book ai didi

mysql - 插入触发器后创建 View

转载 作者:行者123 更新时间:2023-11-30 22:38:09 30 4
gpt4 key购买 nike

我有两个表公司 (id,company_name) 和用户 (id,company_id,user_name);

现在我想在公司表中插入数据时创建一个 View 。

我使用了以下两种方法,但在两种情况下都出现了相同的错误。错误是:

#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;

请大家帮帮我。

谢谢大家

最佳答案

    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 ;

关于mysql - 插入触发器后创建 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31823949/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com