2

来て助けを求めなければならないのは恥ずかしいと思いますが、確かに多くの人が学ぶ前に、mySQL構文エラーメッセージはハイイログマの教皇の帽子と同じくらい役立つようです。添付されているのは、自動車会社のデータベースのトリガーを作成する最初の試みです。テーブルcan_leaseは、従業員のIDと車のモデルのIDを関連付けます。トリガーは2つのルールを適用することが期待されます:1)1人の従業員に関連付けられる最大10台の車種、および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から来て、私のコードが間違っている理由について少なくとも何かを教えてくれました。これは非常に異質なプロセスです。

編集:区切り文字の変更も手順の上に移動する必要があることに気付きました。わかりませんが、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 '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 に答える