存储过程 - 选择后,OUT参数返回null

时间:2015-01-07 02:32:00

标签: mysql sql stored-procedures

我有一个带有几个IN参数和一个OUT的MySQL存储过程。

DELIMITER $$
DROP PROCEDURE IF EXISTS create_or_update_post;
CREATE PROCEDURE create_or_update_post(IN user_login_id INT, IN postid VARCHAR(50), IN subject VARCHAR(150), IN postdate VARCHAR(50), IN body LONGTEXT, IN slug VARCHAR(50), OUT wp_post_id VARCHAR(50))
BEGIN
    IF EXISTS (SELECT @ID := `ID` FROM `wp_posts`, `wp_postmeta` WHERE `ID` = `post_id` AND `meta_key` = 'old_cs_id' AND `meta_value` = postid) THEN
        UPDATE `wp_posts`
        SET `post_title`=subject, `post_content`=body, `post_date`=postdate, `post_name`=slug, `post_author`=user_login_id, `post_modified`=(SELECT NOW())
        WHERE `ID` = @ID;
        SET wp_post_id = @ID;
    ELSE
        INSERT INTO `wp_posts` (`post_title`, `post_content`, `post_excerpt`, `to_ping`, `pinged`, `post_content_filtered`, `post_date`, `post_name`, `post_author`, `post_modified`, `post_status`)
        VALUES (subject, body, '', '', '', '', postdate, slug, user_login_id, (SELECT NOW()),'draft');

        /* Create metadata */
        SET wp_post_id = LAST_INSERT_ID();

        INSERT INTO `wp_postmeta` (`post_id`,`meta_key`,`meta_value`) VALUES(wp_post_id, 'old_cs_id', postid);

    END IF;
END$$
DELIMITER ;

当我调用该程序并尝试SELECT该变量时,我得到null

CALL create_or_update_post(@user_login_id, '123', 'Some title', '2010-09-03 12:39:00', 'Some content', 'Blog Post', @wp_post_id);
SELECT @wp_post_id AS get_returned_post;

返回:

@wp_post_id
NULL

为什么我的OUT参数总是NULL

2 个答案:

答案 0 :(得分:0)

试试这个:

SET @wp_post_id = 0;
CALL create_or_update_post('abc@abc.com', '123', 'Some title', '2010-09-03 12:39:00', 'Some content', 'Blog Post', @wp_post_id);
SELECT @wp_post_id AS get_returned_post;

答案 1 :(得分:0)

当程序员尝试创建MySQL存储过程时会发生这种情况。此问题是尝试从@ID语句中的查询设置IF EXISTS变量的结果。

我重写了以下内容,现在我可以获得wp_post_id

DELIMITER $$
DROP PROCEDURE IF EXISTS create_or_update_post;
CREATE PROCEDURE create_or_update_post(IN user_login_id INT, IN postid VARCHAR(50), IN subject VARCHAR(150), IN postdate VARCHAR(50), IN body LONGTEXT, IN slug VARCHAR(50), OUT wp_post_id VARCHAR(50))
BEGIN
    IF EXISTS (SELECT * FROM `wp_posts`, `wp_postmeta` WHERE `ID` = `post_id` AND `meta_key` = 'old_cs_id' AND `meta_value` = postid) THEN
        SELECT @ID := `ID` FROM `wp_posts`, `wp_postmeta` WHERE `ID` = `post_id` AND `meta_key` = 'old_cs_id' AND `meta_value` = postid;
        UPDATE `wp_posts`
        SET `post_title`=subject, `post_content`=body, `post_date`=postdate, `post_name`=slug, `post_author`=user_login_id, `post_modified`=(SELECT NOW())
        WHERE `ID` = @ID;
        /* Update categories */
        SET wp_post_id = @ID;
    ELSE
        INSERT INTO `wp_posts` (`post_title`, `post_content`, `post_excerpt`, `to_ping`, `pinged`, `post_content_filtered`, `post_date`, `post_name`, `post_author`, `post_modified`, `post_status`)
        VALUES (subject, body, '', '', '', '', postdate, slug, user_login_id, (SELECT NOW()),'draft');

        /* Create metadata */
        SET wp_post_id = LAST_INSERT_ID();

        INSERT INTO `wp_postmeta` (`post_id`,`meta_key`,`meta_value`) VALUES(wp_post_id, 'old_cs_id', postid);
    END IF;
END$$
DELIMITER ;

*请注意我在IF EXISTS语句中复制查询并在其中设置@ID时所做的更改。