MySQL错误代码1241

时间:2016-06-05 05:48:34

标签: mysql sql

我只是尝试使用mySQL的IF-ELSE with SELECT。我知道这里有语法错误但找不到它。它在mySQL Workbench上返回Error Code 1241. Operand should contain 1 column(s)

我没有看到任何错误或者我找不到它。可能导致这种情况的原因是什么?

CREATE DEFINER=`root`@`localhost` PROCEDURE `addCurriculum`(    
    IN p_curcName varchar(100), 
    IN p_description TEXT, 
    IN p_yearLevel VARCHAR(50),
    IN p_syStart INT, 
    IN p_syEnd INT, 
    IN p_creator VARCHAR(50)            )
BEGIN
    IF 
        (SELECT name,description,yearLevel,syStart,syEnd
        FROM curriculum 
        WHERE 
            name        =       p_curcName 
        AND description =       p_description
        AND yearLevel   =       p_yearLevel 
        AND syStart     =       p_syStart 
        AND syEnd       =       p_syEnd 
        AND creator     =       p_creator )

    THEN --

    BEGIN
        SELECT 'Curriculum you are trying to add already exists';
    END;

        ELSE

            BEGIN
                INSERT INTO curriculum(name, description, yearLevel, syStart, syEnd, creator)
                VALUES(p_curcName,p_description,p_yearLevel,p_syStart,p_syEnd,p_creator);
            END;

    END IF;

END

我很感激任何帮助,因为我不熟悉这门语言。我刚开始学习交易和程序。

感谢。

3 个答案:

答案 0 :(得分:1)

我认为你正在寻找IF EXISTS,但是有很多方法可以使用IF和Merge进行upuping(UPdatinSERT),很多都是在SO :)

答案 1 :(得分:0)

IF 
        (SELECT name,description,yearLevel,syStart,syEnd

没有意义;它只需要一列来测试if。

也许你想要IF (SELECT COUNT(*) FROM ...)

答案 2 :(得分:0)

好的,我只需要添加EXISTS

IF EXISTS
        (SELECT name,description,yearLevel,syStart,syEnd
        FROM curriculum 
        WHERE 
            name        =       p_curcName 
        AND description =       p_description
        AND yearLevel   =       p_yearLevel 
        AND syStart     =       p_syStart 
        AND syEnd       =       p_syEnd 
        AND creator     =       p_creator )

我不知道MERGE是如何运作的,所以我想我必须阅读更多内容。