在RETURN SELECT存储过程

时间:2016-02-17 12:43:03

标签: sql json mariadb

我有一个带字段TEXT的数据库,其中存储了一个JSON字符串。 这个数据库是UTF-8,当我选择JSON字段时,通过正常选择,我读取了正确的JSON,就像存储一样。 但是,在存储过程中,我将字段读取到变量,然后,变量在SELECT之后被破坏。

DROP PROCEDURE IF EXISTS sp_G;
DELIMITER ;;
CREATE PROCEDURE sp_G(
    OUT eCode           CHAR(5),
    OUT eDescription    TEXT,
    IN  uID             INT UNSIGNED
    )
BEGIN
    DECLARE other_right         TEXT;
    DECLARE EXIT HANDLER FOR SQLEXCEPTION
    BEGIN
        GET DIAGNOSTICS CONDITION 1
            eCode = RETURNED_SQLSTATE, eDescription = MESSAGE_TEXT;
        ROLLBACK;
    END;

    SET eCode='00000';
    SET eDescription='';

    SET other_right         = "";

    -- (...code...)
    SELECT y
    INTO other_right
    FROM X
    WHERE z=1
    -- (...code...)

    CALL SP_SaveVariableContent_to_test(other_right)
    -- IN THIS PLACE other_right is OK 
    SELECT other_right;
    CALL SP_SaveVariableContent_to_test(other_right)
    -- IN THIS PLACE other_right is WRONG
END ;;
DELIMITER ;

字段内容: Correct Content

选择后的CURRUPT内容: enter image description here

  

更新:此存储过程仅用于测试,“打印”   变量的内容

    CREATE PROCEDURE SP_SaveVariableContent_to_test(IN  description         TEXT)
BEGIN
        INSERT INTO tmp_prints(description) values(description);
END

1 个答案:

答案 0 :(得分:0)

已解决

我解决了这个问题,将返回值从SELECT更改为OUTPUT参数。 像这样:

DROP PROCEDURE IF EXISTS sp_G;
DELIMITER ;;
CREATE PROCEDURE sp_G(
    OUT eCode           CHAR(5),
    OUT eDescription    TEXT,
    IN  uID             INT UNSIGNED,
    OUT other_right     TEXT
    )
BEGIN
    -- [DISABLED] DECLARE other_right           TEXT; [DISABLED]
    DECLARE EXIT HANDLER FOR SQLEXCEPTION
    BEGIN
        GET DIAGNOSTICS CONDITION 1
            eCode = RETURNED_SQLSTATE, eDescription = MESSAGE_TEXT;
        ROLLBACK;
    END;

    SET eCode='00000';
    SET eDescription='';

    SET other_right         = "";

    -- (...code...)
    SELECT y
    INTO other_right
    FROM X
    WHERE z=1
    -- (...code...)

    CALL SP_SaveVariableContent_to_test(other_right)
    -- IN THIS PLACE other_right is OK 
    -- [DISABLE] SELECT other_right; [DISABLE]
    CALL SP_SaveVariableContent_to_test(other_right)
    -- IN THIS PLACE other_right is OK too :D
END ;;
DELIMITER ;

我认为这是一个MySQL错误,我在MariaDB 10.1中使用CentOS