欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-12-15 11:16:52
mysql存储过程中 下面是一个较常见的场景,判断表中某列是否存在某值,如果存在执行某操作 需要注意的是不能用if exists; exists可以在where后面或...

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 ;

上一篇:

下一篇: