ITPub博客

首页 > IT职业 > IT职场 > mysql 存储过程判断重复的不插入数据

mysql 存储过程判断重复的不插入数据

IT职场 作者:penny_liang 时间:2021-09-09 09:40:40 0 删除 编辑

mysql存储过程中

下面是一个较常见的场景,判断表中某列是否存在某值,如果存在执行某操作

需要注意的是不能用if exists;

exists可以在where后面或者在create object是使用,但是在if语句中不可以使用,只能用变通的方法。

?


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 ;


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/714/viewspace-2805933/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-04-12

  • 博文量
    189
  • 访问量
    1735