我感到很尴尬,我必须来这里寻求帮助,但是在我学习之前肯定有很多,似乎mySQL语法错误信息与灰熊的教皇帽子一样有用。附件是我第一次尝试为汽车公司数据库编写触发器。表can_lease将员工的ID与汽车模型的ID相关联。预计触发器将实施两个规则:1)最多可以有10个与1名员工相关的汽车模型,2)员工必须属于租赁类型(有一个“租赁”栏目必须等于“Y”)。
因此,目标是触发器捕获违反此规则的行为并发送信号和解释违规的消息。我只是不确定错误是什么,但我也会附上相关的错误消息。
create procedure can_lease_check (eid int)
begin
declare can_lease_too_many_models condition for sqlstate '90001';
if ((select count(rent_model_id) from can_lease where emp_id = eid) >= 10)
then signal sqlstate '90001' set message_text = 'employee can lease at most 10 rent models.';
declare can_lease_not_leaser for sqlstate '90002';
if not (select leasing from employer where employer.emp_id = eid) == 'Y'
then signal sqlstate '90002' set message_text = 'employee must be of type "leasing"';
end;
delimiter $$
create trigger can_lease_insert_trigger
after insert on can_lease
for each row begin
call can_lease_check(new.emp_id);
end;
$$
create trigger can_lease_update_trigger
after update on can_lease
for each row begin
call can_lease_check(new.emp_id);
end;
$$
以下是错误消息:
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 3
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'if ((select count(rent_model_id) from can_lease where emp_id = eid) >= 10)
then' at line 1
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'end' at line 1
感谢您的帮助!我也非常感谢你对调试这类事情提出的任何建议。来自gcc告诉我至少某些关于我的代码错误的原因,这是一个非常外国的过程!
编辑:我意识到我应该将分隔符更改移动到程序之上。我没有得到它,但除了其中一个错误之外的所有错误。目前,错误是ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'if (select count(rent_model_id) from can_lease where emp_id = eid) == 10
then s' at line 4
答案 0 :(得分:2)
第一个;
和begin
关键字之间的分号(end
)是罪魁祸首。只需将原始create
块与DELIMITER
括起来,如下所示。我在我的示例中使用#
作为分隔符,并且我认识到您使用$$
,但不会产生任何差异。
DELIMITER #
create procedure can_lease_check (eid int)
begin
declare can_lease_too_many_models condition for sqlstate '90001';
if ((select count(rent_model_id) from can_lease where emp_id = eid) >= 10)
then signal sqlstate '90001' set message_text = 'employee can lease at most 10 rent models.';
declare can_lease_not_leaser for sqlstate '90002';
if not (select leasing from employer where employer.emp_id = eid) == 'Y'
then signal sqlstate '90002' set message_text = 'employee must be of type "leasing"';
end#
此外,如果您像我一样(end#
)结束,或者像您一样,使用end
关键字后面的分号结束,则没有任何区别:
end;
#