嘿,伙计们,有人能告诉我mysql代码中的错误吗
我试图创建一个触发器中调用的存储过程
如果用户在书表中插入一本新书,BooQQTY表插入如果不存在CaldNoT和更新,但在某个时候插入查询不工作
但更新查询工作正常
提前谢谢你

use librarydb;
drop procedure if exists intoBooksQty;
delimiter $$

    create procedure intoBooksQty(in newcallNumber varchar(10))

        begin
            if not exists (select * from books where callNumber = newcallNumber) then
                insert into librarydb.bookqty(callNumber,bookQty,bookqtyOut) values (newcallNumber, 1,0);
            else
                update bookqty set bookQty = bookQty + 1 where callNumber = newCallNumber;
            end if;
        end$$

delimiter ;

最佳答案

一个选择是做一个升级

DELIMITER //

CREATE PROCEDURE `intoBooksQty`(`newcallNumber` VARCHAR(10))
BEGIN
  INSERT INTO `bookqty` (`callNumber`, `bookQty`, `bookqtyOut`)
  SELECT `newcallNumber`, 1, 0
  FROM `books`
  WHERE `callNumber` = `newcallNumber`
    ON DUPLICATE KEY UPDATE `bookQty` = `bookQty` + 1;
END//

DELIMITER ;

SQL Fiddle demo

关于mysql - 如果不存在并插入mysql存储过程,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25598003/

10-12 05:20