日期:2014-05-19  浏览次数:20450 次

关于游标的使用
要用到两个游标,但好像执行不了,如下:
declare   t_FlgNull_Insert   cursor   scroll   for
select  
……
open   t_FlgNull_Insert
declare……
fetch   next   from   t_FlgNull_Insert   into   ……
……
close   t_FlgNull_Insert
deallocate   t_FlgNull_Insert

declare   t_table1_Insert   cursor   scroll   for
select  
……
open   t_table1_Insert
declare……
fetch   next   from   t_table1_Insert   into   ……
……
close   t_FlgNull_Insert
deallocate   t_table1_Insert
不能这样写么?那应该怎么写呢,嵌套么?以前没用过,请大家帮帮忙了

------解决方案--------------------
--舉例如下
DECLARE CUR_EmpNo CURSOR FOR
select a.emp_no from 表
where isnull(a.email, ' ') <> ' '
OPEN CUR_EmpNo

FETCH NEXT FROM CUR_EmpNo INTO @EmpNo

WHILE @@FETCH_STATUS = 0
BEGIN
EXEC DBO.SP_ECS_Email @EmpNo 
FETCH NEXT FROM CUR_EmpNo INTO @EmpNo
END
CLOSE CUR_EmpNo
DEALLOCATE CUR_EmpNo
------解决方案--------------------
嵌套cursor

declare
cursor c1 is
select a
from b;
v_c1 c1%rowtype;

ursor c2(v_a varchar2) is
select c
from d
where e=v_c;
v_c2 c2%rowtype;

begin
open c1
begin
loop
fetch c1 into v_c1;
exit when c1%notfound;
open c2(v_c1.a)
begin
loop
fetch c2 into v_c2;
exit when c2%notfound;
...................................
end loop;
end;
close c2;
end loop;
end;
close c1;
end;