我最近在AWS EC2 Ubuntu实例上上传了我的MySQL数据库,现在我遇到了一个问题,即我开发的存储过程在实例上无法正常工作。它在我的Windows上的XAMPP实例上运行得非常好,所以我不完全确定问题是什么,因为我不熟悉Linux和AWS。
这是存储过程:
DELIMITER $$
CREATE PROCEDURE sp_PopulateAbstract
(conID INT, eID INT, abs TEXT)
BEGIN
DECLARE existID INT;
DECLARE abID INT;
SELECT a.contributorID INTO existID
FROM abstract a
WHERE a.contributorID = conID and a.eventID = eID;
IF conID = existID
THEN
--UPDATE statement works fine on Ubuntu instance
UPDATE abstract
SET abstract = abs
WHERE eventID = eID and contributorID = conID;
ELSE
--This statement doesn't work
INSERT INTO abstract
(eventID, contributorID, abstract)
VALUES (eID, conID, abs);
END IF;
SELECT a.abstractID INTO abID
FROM abstract a
WHERE a.eventID = eID and a.contributorID = conID;
SELECT abID as abstractID;
END$$
这很奇怪,因为UPDATE
语句在Ubuntu实例上运行正常,但INSERT
语句没有。 INSERT
语句不插入任何内容,最后SELECT
语句返回NULL
。如果执行了UPDATE
语句,则最后SELECT
语句会返回一些内容,并且数据会相应更新。
我已将INSERT
语句作为独立语句运行并且它工作正常,但它在存储过程中不起作用。 XAMPP实例可以在哪里工作,而Ubuntu实例却没有呢?
答案 0 :(得分:0)
我发现导致INSERT
语句不起作用的原因,这与我在原始问题中遗漏的存储过程部分有关,认为它不是原因问题。这是完整的程序:
DELIMITER $$
BEGIN
DECLARE existID INT;
DECLARE abID INT;
SELECT a.contributorID INTO existID
FROM eventcontributor a
WHERE a.contributorID = conID and a.eventID = eID;
IF conID = existID
THEN
--cause of problem
SELECT a.contributorID INTO existID
FROM abstract a
WHERE a.contributorID = conID and a.eventID = eID;
IF conID = existID
THEN
UPDATE abstract
SET abstract = abs
WHERE eventID = eID and contributorID = conID;
ELSE
INSERT INTO abstract
(eventID, contributorID, abstract)
VALUES (eID, conID, abs);
END IF;
SELECT a.abstractID INTO abID
FROM abstract a
WHERE a.eventID = eID and a.contributorID = conID;
DELETE
FROM abstractattachments
WHERE abstractID = abID;
SELECT abID as abstractID;
ELSE
SELECT -1 as abstractID;
END IF;
END$$
在这个完整的过程中,问题实际上是在我重新选择变量existID
的值时引起的。出于某种原因,现在是真正的问题,MySQL的Ubuntu实例如果已经包含一个变量值,则不会覆盖它。也许这与MySQL的版本与Ubuntu版本不同,而且在我的Windows XAMPP上。
结果,变量existID
保留了它的初始值,嵌套的IF
语句测试为true
,这意味着UPDATE
语句实际上正在执行而不是INSERT
声明。
解决此问题的方法是DECLARE
另一个名为existID2
的变量,然后使用此变量而不是重新使用existID
。
解决方案:
DELIMITER $$
BEGIN
DECLARE existID INT;
DECLARE existID2 INT;
DECLARE abID INT;
SELECT a.contributorID INTO existID
FROM eventcontributor a
WHERE a.contributorID = conID and a.eventID = eID;
IF conID = existID
THEN
--using a new, empty variable
SELECT a.contributorID INTO existID2
FROM abstract a
WHERE a.contributorID = conID and a.eventID = eID;
IF conID = existID2
THEN
UPDATE abstract
SET abstract = abs
WHERE eventID = eID and contributorID = conID;
ELSE
INSERT INTO abstract
(eventID, contributorID, abstract)
VALUES (eID, conID, abs);
END IF;
SELECT a.abstractID INTO abID
FROM abstract a
WHERE a.eventID = eID and a.contributorID = conID;
DELETE
FROM abstractattachments
WHERE abstractID = abID;
SELECT abID as abstractID;
ELSE
SELECT -1 as abstractID;
END IF;
END$$
为什么我的本地XAMPP MySQL 实例可以重用变量,我真的很难以理解,但我的AWS Ubuntu MySQL实例却不能。这对我来说真的没有意义。