2

我感到很尴尬,我必须来寻求帮助,但正如我之前的许多人所了解的那样,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
4

1 回答 1

2

第一个和关键字之间的分号 ( ;)是罪魁祸首。只需用 将原始块括起来,如下所示。我在示例中使用分隔符,并且我知道您使用,尽管不会产生任何差异。beginendcreateDELIMITER#$$

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;
#
于 2012-10-04T19:56:56.277 回答