服务器之家:专注于服务器技术及软件下载分享
分类导航

Mysql|Sql Server|Oracle|Redis|MongoDB|PostgreSQL|Sqlite|DB2|mariadb|

服务器之家 - 数据库 - Mysql - mysql 存储过程判断重复的不插入数据

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

2020-07-08 16:12MYSQL教程网 Mysql

这篇文章主要介绍了下面是一个较常见的场景,判断表中某列是否存在某值,如果存在执行某操作,需要的朋友可以参考下

mysql存储过程

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

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

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

?
1
2
3
4
5
6
7
8
9
10
11
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 ;

延伸 · 阅读

精彩推荐