如何在MySQL中编写存储过程,在调用时显示两条消息

时间:2015-12-10 08:06:41

标签: mysql mysql-workbench

此处的目标是编写脚本尝试将新类别插入类别表中,如果插入成功,则显示消息:

  

插入了1行

如果更新失败,则程序应显示以下消息:

  

未插入行 - 重复条目

每当我运行这个脚本时,无论我运行多少次,我都会继续收到第二条消息,实际上它应该显示第一条消息,然后是第二条消息。这是我的剧本。有人可以帮我看一下我在俯瞰的东西吗?谢谢。

    use my_guitar_shop;
   /*drop procedure if exists add_category;*/
   DELIMITER //
  CREATE PROCEDURE add_category(

   in categories VARCHAR(100)

  )
   begin
   declare duplicate_entry_for_key TinyINT DEFAULT FALSE;
   declare continue handler for 1062
   SET duplicate_entry_for_key = TRUE;

  insert into categories values (5, 'Electric');

   select '1 row was inserted.' as message;

   if duplicate_entry_for_key = true then
   select 'Row was not inserted - duplicate entry.' as message;
   end if;
   end //
   DELIMITER ;

   /* call the stored procedure with 'Gibson' */
   call add_category('Gibson');
   call add_category('Gibson');

1 个答案:

答案 0 :(得分:0)

您应该将GetScores PROC STDCALL, wArray:PTR WORD, arraySize : WORD ; push ebp ; superfluous and harmful prolog ; mov ebp, esp pushad mov esi, wArray movzx ecx, arraySize cmp ecx, 0 ; ECX < 0 ? jle L2 ; yes: skip over loop L1 : call ReadInt mov[esi], eax add esi, TYPE WORD loop L1 L2 : popad ; pop ebp ; superfluous epilog ret ; becomes `ret 8` due to "STDCALL" in the PROC header GetScores ENDP 设置为True。如果数据插入成功。

插入操作后执行以下操作。

duplicate_entry_for_key

OR 请参阅以下示例进行位检查。

例如:

SET duplicate_entry_for_key = last_insert_id();