插入语句在存储过程中不起作用

时间:2016-01-07 10:57:15

标签: php mysql amazon-web-services stored-procedures ubuntu-14.04

我最近在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实例却没有呢?

1 个答案:

答案 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实例却不能。这对我来说真的没有意义。