create or replace procedure zytest is
  cursor numcursor is select dhhm from gj_haoyuan where state='N';
  temp_hy varchar2(7);
  temp_yh varchar2(7);
  num_yh varchar2(7);
  num_yh_mid varchar2(7);
  num_hy varchar2(7);
  num_hy_mid varchar2(7);
  
begin
num_yh:='8420003';
num_yh_mid:=substr(num_yh,4);
  dbms_output.put_line(num_yh);
  dbms_output.put_line(num_yh_mid);
open numcursor;
loop
fetch numcursor into num_hy;
exit when numcursor%notfound;
num_hy_mid:=substr(num_hy,4);
if num_hy_mid=num_yh_mid then
    dbms_output.put_line(num_hy);
temp_hy:=num_hy;
temp_yh:=num_yh;
end if;
end loop;
close numcursor;
update gj_haoyuan set state='Y' where dhhm=temp_hy;
commit;
end zytest;第一次执行 exec zytest;  输出 8420003,0003,8430003,8440003
马上再执行一次 exec zytest; 输出 8420003,0003,8430003
接着执行 exec zytest; 输出 8420003,0003
以后执行 exec zytest; 均输出 8420003,0003如果我 update gj_haoyuan(存储过程中有的这张表) 则,运行 exec zytest 时,sqlplus会死掉什么原因啊?请大家指点一下!   谢谢!!!!

解决方案 »

  1.   

    update gj_haoyuan set state='Y' where dhhm=temp_hy;
    要放在循环里面吧,
    按你现在这样写%0003有1000条不得要执行1000次存储过程
      

  2.   

    很明顯,樓主在過程中結尾時用了update gj_haoyuan set state='Y' where dhhm=temp_hy;
    而在過程的開始時用了cursor numcursor is select dhhm from gj_haoyuan where state='N';
    這就說明,在你每次執行完你的過程後,gj_haoyuan 表的符合dhhm=temp_hy的條件的state列的值都被你給改掉了,而條件的值temp_hy是一個變量,因此每次執行後,表中被修改到的行都不一樣(因為temp_hy在循環退出時有不同的值),而在過程開始時打開遊標時cursor numcursor is select dhhm from gj_haoyuan where state='N'又用到了上一次過程執行完時被改過的列來作為遊標條件,遊標中包含的行肯定每次執行時都不一樣,因此每次執行過程後,所得到的結果肯定不一樣了,終上所述,樓主的過程其實是一個錯誤的過程,你應該說明你的意思,然後讓大家來幫你改正確!!!