mysql stored procedure determines duplicate non insert data

  • 2021-06-28 14:16:30
  • OfStack

mysql stored procedure

Below is a more common scenario to determine if a value exists for a column in a table and, if so, to perform an action

It should be noted that if exists cannot be used;

exists can be used after where or in create object, but it is not used in if statements, only in workarounds.


delimiter $$
create procedure proc_add_book(in $bookName varchar(200),in $price float)
begin
  declare $existsFlag int default 0;
  select bookId into $existsFlag from book where bookName = $bookName limit 1;
  if bookId > 0 then
  #if not exists (select * from book where bookNumber = $bookName) then
    insert into book(bookNumber,price) values($bookName,$price);
  end if;
end$$
delimiter ;


Related articles: