日期:2014-05-17  浏览次数:20805 次

新人求助!PLSQL游标问题
本帖最后由 murphy0410 于 2013-05-28 16:20:25 编辑
 declare
  cursor test_Cursor is 
 select etpcode,max(rptdate) from vRptComFlag  where etpcode in (select orgcode from tOrgDrpDisp where chainjymode ='1') and hzflag='1' group by etpcode;
  v_orgcode varchar2(10);
  v_rptdate varchar2(10);
  begin
  open test_Cursor;
  fetch test_Cursor into v_orgcode,v_rptdate;
  while test_Cursor%found loop
   update Cmp_test_drp_mf set kcost=(select sum(kccost) from vRptComHzDisSale where orgcode=v_orgcode and rptdate=v_rptdate and orgcode = Cmp_test_drp_mf.orgcode);
  fetch test_Cursor into  v_orgcode,v_rptdate;
  end loop;
  close test_Cursor;   
  end;

Cmp_test_drp_mf 是一个我简历的临时表,
我UPDATE只有最后一行更新了数据,不知道为何,请大神们指教下,谢谢
PL/SQL

------解决方案--------------------
引用:
Quote: 引用:

你不是只UPDATE了最后一列么?看了下逻辑,好像没必要用游标吧,直接用SQL语句不行吗

由于计算数据过大。。我直接用游标省略了我一次一次执行的过程,我刚才描述有问题,我是更新那一列的最后一行有更新数据,之前的都是空白的,但是我游标初始化时表的第一行啊,为什么不是一次遍历过去,而是最后一列有。


update Cmp_test_drp_mf set kcost=(select sum(kccost) from vRptComHzDisSale where orgcode=v_orgcode and rptdate=v_rptdate and orgcode = Cmp_test_drp_mf.orgcode)
where exists(select 1 from vRptComHzDisSale where orgcode=v_orgcode and rptdate=v_rptdate and orgcode = Cmp_test_drp_mf.orgcode)
;

update如果不加WHERE条件都是UPDATE整个表的记录,所以你那个是最后一行满足了条件,其他行不满足,全部UPDATE成NULL了。