Mysql游标无法获取数据

时间:2014-09-09 10:52:47

标签: mysql cursor

我正在创建程序来获取乘客姓名和PNR号码使用游标获取数据,这里的代码是:

mysql> create procedure GetName(out PNR_No varchar(20), out Name varchar(20))
    -> begin
    -> declare PNR varchar(20);
    -> declare Names varchar(20);
    -> declare finished int default 0;
    -> declare c1 cursor for select T.PNRNo, P.Name
    -> from Passenger P, Ticket T
    -> where P.PPNO=T.PPNO;
    -> declare continue handler for not found set finished=1;
    -> open c1;
    -> get_Names: loop
    -> fetch c1 into PNR, Names;
    -> if finished=1 then
    -> leave get_Names;
    -> end if;
    -> set PNR_No=concat(PNR,";",PNR_No);
    -> set Name=concat(Names,";",Name);
    -> end loop get_Names;
    -> close c1;
    -> end/

但是当我打电话给程序时:

mysql> call GetName(@PNR_No,@Name);
    -> /
Query OK, 0 rows affected (0.07 sec)

mysql> select @PNR_No,@Name;
    -> /
+---------+-------+
| @PNR_No | @Name |
+---------+-------+
| NULL    | NULL  |

但是当我执行查询时:

mysql> select T.PNRNo, P.Name
            -> from Passenger P, Ticket T
            -> where P.PPNO=T.PPNO;
            -> /
        +----------+---------+
        | PNRNo    | Name    |
        +----------+---------+
        | PNRO0006 | NAME001 |
        | PNRO0003 | NAME002 |
        | PNRO0007 | NAME002 |
        | PNRO0002 | NAME003 |
        | PNRO0009 | NAME003 |
        | PNRO0004 | NAME004 |
        | PNRO0008 | NAME004 |
        | PNRO0001 | NAME005 |
        | PNRO0005 | NAME005 |
        +----------+---------+
        9 rows in set (0.00 sec)

我没有得到我错的地方!

1 个答案:

答案 0 :(得分:0)

尝试预设PNR_No和名称变量IE

SET PNR_No = '' ;
SET Name = '' ;

这是因为如果concat中的任何项都为NULL,则返回NULL。