mysql>delimiter//mysql>createprocedureoldest(outoldestint)->begin->declarethis_namechar;->declarethis_ageint;->declarel_namechar;->declarel_ageintdefault0;->declaredonein...
mysql> delimiter //
mysql> create procedure oldest(out oldest int)
-> begin
-> declare this_name char;
-> declare this_age int;
-> declare l_name char;
-> declare l_age int default 0;
-> declare done int default 0;
-> declare a1 cursor for select name,age from boom;
-> declare continue handler for sqlstate '02000' set done=1
-> open a1;
-> repeat
-> fetch a1 into this_name,this_age;
-> if not done then
-> if this_age>l_age then
-> set l_age=this_age;
-> set l_name=this_name;
-> end if;
-> end if;
-> until done end repeat;
-> close a1;
-> set oldest=l_name;
-> end
-> //
Query OK, 0 rows affected (0.04 sec)
mysql> call oldest(@q)//
ERROR 1406 (22001): Data too long for column 'this_name' at row
mysql> describe boom;
-> //
+---------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+------------------+------+-----+---------+-------+
| name | char(20) | NO | PRI | NULL | |
| age | int(10) unsigned | NO | | NULL | |
| address | char(100) | NO | | NULL | |
| email | char(50) | NO | | NULL | |
+---------+------------------+------+-----+---------+-------+
展开