2
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;
4

3 回答 3

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 ;
于 2013-02-01T13:14:05.583 回答
0

您忘记了:,,delimiter希望 对您有所帮助thenend 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 ;
于 2013-02-01T13:14:37.943 回答
0

我认为 IF not exists 在 mysql 中不存在然后

于 2013-02-01T13:16:15.937 回答