如果存储过程中的其他功能不起作用

时间:2013-02-01 13:01:09

标签: mysql sql stored-procedures

CREATE PROCEDURE p1 
(
    IN name_val VARCHAR(255),
    OUT iJobID  INT
)


BEGIN

    IF NOT EXISTS (SELECT id FROM test WHERE id='11')

        BEGIN

            INSERT INTO test(name) VALUES(name_val);
            SET iJobID :=  LAST_INSERT_ID();
        END

    ELSE
        BEGIN

             UPDATE test SET name=name_val WHERE id = 11; 
        END 

    INSERT INTO vasu2(vname) VALUES(name_val);
    SET @ivD :=  LAST_INSERT_ID();

    INSERT INTO vasu(id, id2) VALUES(iJobID, @ivD); 
END;

3 个答案:

答案 0 :(得分:1)

您正在使用的IF语法最有可能是T-SQL。此外,您需要更改delimiter

DELIMITER $$
CREATE PROCEDURE p1 
(
    IN name_val VARCHAR(255),
    OUT iJobID  INT
)
BEGIN

    SET @recCount := (SELECT COUNT(*) FROM test WHERE id = 11);
    IF @recCount > 0 THEN
        INSERT INTO test(name) VALUES(name_val);
        SET iJobID :=  LAST_INSERT_ID();
    ELSE
        UPDATE test SET name=name_val WHERE id = 11; 
    END IF;

    INSERT INTO vasu2(vname) VALUES(name_val);
    SET @ivD :=  LAST_INSERT_ID();

    INSERT INTO vasu(id, id2) VALUES(iJobID, @ivD); 
END $$
DELIMITER ;

答案 1 :(得分:0)

您忘了:delimiterthenend if 希望它有所帮助

delimiter $$


CREATE PROCEDURE test.p1 
(
    IN name_val VARCHAR(255),
    OUT iJobID  INT
)
BEGIN
    IF NOT EXISTS (SELECT id FROM test WHERE id='11') THEN
            INSERT INTO test(name) VALUES(name_val);
            SET iJobID :=  LAST_INSERT_ID();
    ELSE
             UPDATE test SET name=name_val WHERE id = 11; 
    END IF;
    INSERT INTO vasu2(vname) VALUES(name_val);
    SET @ivD :=  LAST_INSERT_ID();
    INSERT INTO vasu(id, id2) VALUES(iJobID, @ivD); 
END
$$

DELIMITER ;

答案 2 :(得分:0)

我认为如果不存在则不存在于mysql中