为什么我的程序产生null结果?

时间:2017-01-20 10:13:09

标签: mysql stored-procedures mysql-workbench

我只创建了2个输入并提供2个输出的过程。
当我单独运行查询时它会给我正确的输出,但如果我将尝试调用程序它会产生null结果

调用Query时

SELECT field1, field2 
        INTO @var1, @var2
        FROM MyTable ID=? and Name=?

然后:

select  @var1, @var2;

但如果我在程序中结合相同的代码,它将为这两个字段提供null

程序:

DELIMITER $$

CREATE DEFINER=`user`@`localhost` PROCEDURE `My_Proc`(
    IN `ID` INT,
    IN `Name` VARCHAR(20)
    -- OUT `result` INT,
    -- OUT `result1` INT
)
BEGIN

 set @var1 = 0;
 set @var2 = 0;

    set @query := CONCAT("SELECT
                      field1, field2 
             INTO @var1, @var2
                      FROM MyTable ID=? and Name=?");

    PREPARE stmt FROM @query;
    Execute stmt USING @Id,@Name;
    DEALLOCATE PREPARE `stmt`;

select @var1,@var2;


END

我尝试在外部程序中调用select @var1,@var2;,但它也会给出空字段。

我在Mysql Workbench 6.0中工作

1 个答案:

答案 0 :(得分:1)

你的手术中有一些错误已被删除..........看看

DELIMITER $$

CREATE DEFINER=`user`@`localhost` PROCEDURE `My_Proc`(
    IN `ID` INT,
    IN `Name` VARCHAR(20)
    -- OUT `result` INT,
    -- OUT `result1` INT
)
BEGIN

 set @var1 = 0;
 set @var2 = 0;

    set @query := CONCAT("SELECT
                      field1,field2 
             INTO @var1, @var2
                      FROM MyTable where ID=? and Name=?");

    PREPARE stmt FROM @query;
    Execute stmt USING @Id,@Name;
    DEALLOCATE PREPARE stmt ;

select @var1,@var2;


END