存储过程输出变量

时间:2015-02-08 11:12:53

标签: mysql stored-procedures

我正在尝试运行一个带参数' table'用于查询,结果作为输出参数。但是,它显示为未声明的变量:result

我加倍检查没有拼写错误,但仍然不知道它是如何发生的。有人请提供一些帮助或指导

CREATE DEFINER=`root`@`localhost` PROCEDURE `Function`(IN table varchar(10), OUT result varchar (10))
BEGIN

    SET @q = CONCAT ('
        Select `field` from `',table,'` into result limit 1;');


    PREPARE stmt from @q;
    EXECUTE stmt;
    DEALLOCATE PREPARE stmt;

END

1 个答案:

答案 0 :(得分:0)

尝试:

...
-- SET @q = CONCAT ('Select `field` from `',table,'` into result limit 1;');
SET @`q` := CONCAT('SELECT `der`.`field`
                    FROM (SELECT `field` FROM `', `table`, '` LIMIT 1) `der`,
                         (SELECT @`result` := NULL) `init`
                         INTO @`result`;');
PREPARE `stmt` from @`q`;
EXECUTE `stmt`;
SET `result` := @`result`;
DEALLOCATE PREPARE `stmt`;
...

重要的是指出9.4. User-Defined Variables和常规参数13.1.15. CREATE PROCEDURE and CREATE FUNCTION Syntax之间的差异,是不同的变量。

SQL Fiddle demo