在程序中动态查询,我如何获得OUT值?

时间:2014-03-20 14:58:24

标签: mysql stored-procedures dynamic return-value

我想用特定的time_stamp和pro_id调用动态过程。

在第一步中,我想知道表中是否存在特定的pro_idConcat声明中有什么不对吗?我没有得到所需的OUT

DELIMITER ;;
CREATE PROCEDURE 'ADDCONSENSUS'(IN time_stamp int(10), IN pro_id INT(10), OUT cnt INT(11))
BEGIN
    SET @sql1 = CONCAT('SELECT COUNT(pid) INTO @cnt  FROM ',time_stamp,' WHERE pid = ,pro_id);
    PREPARE stmt from @sql1;
    EXECUTE stmt;
END

1 个答案:

答案 0 :(得分:0)

您可以尝试以下操作:

DELIMITER $$

DROP PROCEDURE IF EXISTS `ADDCONSENSUS`$$

CREATE PROCEDURE `ADDCONSENSUS`(
    IN `time_stamp` INT,
    IN `pro_id` INT,
    OUT `cnt` INT)
BEGIN
    SET @sql1 := CONCAT('
        SELECT COUNT(`pid`) INTO @`cnt`
        FROM `', CAST(`time_stamp` AS CHAR), '`
        WHERE `pid` = ', CAST(`pro_id` AS CHAR));
    PREPARE stmt FROM @sql1;
    EXECUTE stmt;
    SET `cnt` := @`cnt`;
    DEALLOCATE PREPARE stmt;
END$$

DELIMITER ;

CALL `ADDCONSENSUS`(1395395302, 3, @`_count`);

SQL Fiddle demo