我在創建觸發器時遇到了問題。我的「tb_pessoa」表存儲我的用戶數據。我希望在創建一個新插入時,它會在「tb_email」表中存儲用戶的電子郵件,檢查它是否已經存在,但是當用光標執行查詢時,表示光標未打開,而且是。它會是什麼?觸發錯誤1326 MySQL
CREATE DEFINER=`root`@`localhost` TRIGGER `db_agenda`.`tb_pessoa_AFTER_INSERT` AFTER INSERT ON `tb_pessoa` FOR EACH ROW
BEGIN
declare id_email, id_tipo_email int(11);
declare nm_email varchar(100);
declare cr_email cursor for
select id_email, nm_email, id_tipo_email
from tb_email
where nm_email like new.nm_email;
declare continue handler for sqlstate '02000'
open cr_email;
lp_email: loop
fetch cr_email into id_email, nm_email, id_tipo_email;
if (id_email = null) then
insert into tb_email set
id_email = (select max(id_email)+1 from tb_email),
nm_email = nm_email,
id_tipo_email = 1;
close cr_email;
leave lp_email;
end if;
end loop lp_email;
close cr_email;
END
'id_email'是一個自動增量列嗎? – Barmar
不,它是通過max(id_email)+ 1的子查詢插入的 –
爲什麼不將它更改爲auto_increment?然後它會自動完成。 – Barmar