MySQL PLSQL Demo - 003.静态游标

 

drop procedure if exists p_hello_world;

create procedure p_hello_world()
begin
    declare id integer;
    declare username varchar(256);
    declare result varchar(4000) default '';
    /* don't work */
    /*declare cur_user cursor for select id from p_user where id is not null and name is not null order by id;*/
    declare cur_user cursor for select t.id, t.name from p_user t where t.id is not null and t.name is not null order by t.id;
    declare continue handler for SQLSTATE '02000' set id = null; 
    open cur_user;
    fetch cur_user into id, username;
    while (id is not null) do
        set result = concat(result, 'id:', id, 'username:', username, ';');
        fetch cur_user into id, username;
    end while;
    close cur_user;
    select result;
end;

call p_hello_world();

 

posted @ 2015-07-01 18:42  nick_huang  阅读(530)  评论(0)    收藏  举报