您好,登錄后才能下訂單哦!
在ITPUB: http://www.itpub.net/viewthread.php?tid=1134085&pid=13049789&page=1&extra=#pid13049789上有人發(fā)貼說游標(biāo)不能嵌套,
自己搞了一下,答案確實是..
[@more@]create table heyf_6 (empid int , deptid int );
insert into heyf_6 values (1,20),(2,30),(5,20),(4,50),(6,20);
--按DEPTID從小到大打印各個部分里的EMPID
DROP PROCEDURE IF EXISTS proc_empid;
DELIMITER //
CREATE PROCEDURE proc_empid()
BEGIN
declare done int default 0;
declare v_dept int ;
declare cur_dept cursor For select distinct deptid from heyf_6 order by deptid ;
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = 1;
open cur_dept;
cursor_loop:loop
fetch cur_dept into v_dept;
set @dept = v_dept;
if done=1 then leave cursor_loop ;
end if ;
select 'DEPT: ' ,v_dept ;
begin
declare done1 int default 0;
declare v_emp int;
declare cur_emp cursor For select empid from heyf_6 where deptid =@dept ;
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done1 = 1;
open cur_emp;
cur_loop:loop
fetch cur_emp into v_emp ;
if done1=1 then leave cur_loop ;
end if ;
select '------------',@dept,v_emp ;
end loop cur_loop;
close cur_emp;
end;
end loop cursor_loop ;
close cur_dept;
END ;//
DELIMITER ;
call proc_empid;
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。