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 ;
答案 0 :(得分:1)
一种选择是制作UPSERT。
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 ;