MySQL:过程参数的问题

时间:2016-02-09 12:06:50

标签: mysql stored-procedures

我的手术似乎有问题。当我执行它时,它应该创建一个条目,直接修改一个列,然后返回它的id。

但是没有创建任何东西,它也没有返回任何内容(甚至没有显示错误)

当我使用硬编码值而不是使用参数时,会创建条目,但不会对其进行编辑,但仍不会返回ID。

你能看看吗?这是使用phpMyAdmin的导出函数的整个过程结果:

CREATE DEFINER=`dbuser`@`localhost` PROCEDURE `createRecruitment`(IN `p_user_id` INT(11), IN `p_platform` ENUM('uplay','steam','ps4','xb1') CHARSET utf8, IN `p_activity` ENUM('pve','pvp') CHARSET utf8, IN `p_description` TEXT CHARSET utf8)
    MODIFIES SQL DATA
    DETERMINISTIC
    SQL SECURITY INVOKER
BEGIN
    DECLARE v_id INT(11);

    INSERT INTO
        recruitments (
            user_id,
            creationDate,
            platform_id,
            activity,
            description
        )
    SELECT
        p_user_id,
        CURRENT_TIMESTAMP,
        platforms.id,
        p_activity,
        p_description
    FROM
        platforms
    WHERE
        platforms.platform = p_platform;

    SET v_id = LAST_INSERTED_ID();

    UPDATE
        recruitments
    SET
        lastActivity = creationDate
    WHERE
        id = v_id;

    SELECT v_id;
END

修改

似乎主要问题是“p_platform”参数。我想将输入限制为给定的ENUM,但似乎WHERE platforms.platform = p_platform对此不适用。

1 个答案:

答案 0 :(得分:0)

使用以下SP。我使用OUT v_id字段并分配值

CREATE DEFINER=`dbuser`@`localhost` PROCEDURE `createRecruitment`(IN `p_user_id` INT(11), IN `p_platform` ENUM('uplay','steam','ps4','xb1') CHARSET utf8, IN `p_activity` ENUM('pve','pvp') CHARSET utf8, IN `p_description` TEXT CHARSET utf8, OUT `v_id` INT(11))
        MODIFIES SQL DATA
        DETERMINISTIC
        SQL SECURITY INVOKER
    BEGIN


        INSERT INTO
            recruitments (
                user_id,
                creationDate,
                platform_id,
                activity,
                description
            )
        SELECT
            p_user_id,
            CURRENT_TIMESTAMP,
            platforms.id,
            p_activity,
            p_description
        FROM
            platforms
        WHERE
            platforms.platform = p_platform;

        SET v_id = LAST_INSERTED_ID();

        UPDATE
            recruitments
        SET
            lastActivity = creationDate
        WHERE
            id = v_id;


    END