【发布时间】:2020-09-11 10:08:46
【问题描述】:
正如标题所说,我对触发器 + 过程有疑问。
我正在尝试做的是一个程序 + 触发器,它仅在我尝试插入不带电子邮件的学生数据时创建包含姓名和姓氏的电子邮件。
基本上卡在哪里,代码融合了姓名和姓氏并创建了电子邮件,但是在插入表格之前如何放置它?
最好的问候, 工程师 这是我制作的代码:
use alumnesTriggers;
drop procedure if exists ex2;
delimiter //
create procedure ex2 (in nom varchar(50), in cognom1 varchar(50),
in cognom2 varchar(50), in domini varchar(50), out email varchar(50))
begin
set email := concat(nom,cognom1,cognom2,'@',domini,'.com');
#set @email := email;
#UPDATE alumnesEmail as ae SET ae.email = @email WHERE nom = @nom;
end //
delimiter ;
drop trigger if exists ex2_2_trigger;
DELIMITER $$
CREATE TRIGGER ex2_2_trigger before insert on alumnesEmail for each row begin
set @mail := new.email;
#if @mail := null or @mail='' then
set @id := new.id;
set @nom := new.nom;
set @cognom1 := new.cognom1;
set @cognom2 := new.cognom2;
call ex2(@nom,@cognom1,@cognom2,'gmail',@email);
UPDATE alumnesEmail SET new.email = @email;
#end if;
END $$
DELIMITER ;
INSERT INTO `alumnesEmail` (`id`, `nom`, `cognom1`, `cognom2`, `email`)
VALUES (80, 'a', 'a', 'a',null);
select * from alumnesEmail where id = 80;
select @email;
select * from alumnesEmail ;
【问题讨论】:
-
已解决,我必须添加“set new.email = @email;”
-
如何创建新学生?如果您使用的是存储过程,那么我会将所有相关代码保存在一个地方。离开或更改法定姓名的学生怎么办?表
alumnesEmail会保持最新吗?
标签: mysql sql triggers procedure database-trigger