oracle update from 问题! 

  1. update t_tmprpt_firstreplycosttime t  
  2.    set (t.firstreplytime,  
  3.        t.dealstaff,  
  4.        t.firstreplyfailcontent)  
  5.        = (select a.suggesttime,  
  6.                  a.suggester,  
  7.                  substr(a.remark,instr(a.remark,'】',1)+2)  
  8.             from t_wf_suggesthis a  
  9.            where t.serialno = a.serialno  
  10.              and t.serviceclassid = a.serviceclassid);  

想把t_tmprpt_firstreplycosttime表中的3个字段数据更新为t_wf_suggesthis表中的suggesttime, suggester,substr(a.remark,instr(a.remark,'】',1)+2)的值。条件是t表的sreialno和serviceclassid都与a表中的相等。

问题:当我执行这条更新时候会把t表中的所有数据都更新了。

解决:oracle中没update from 这样的更新,可以考虑2种解决办法。

一,

   

  1. update (select a.suggesttime atime,  
  2.                b.firstreplytime btime,  
  3.                a.suggester astaff,  
  4.                b.dealstaff bstaff,  
  5.                substr(a.remark,instr(a.remark,'】',1)+2) acontent,  
  6.                b.firstreplyfailcontent bcontent  
  7.           from t_wf_suggesthis a, t_tmprpt_firstreplycosttime b)   
  8.     set btime = atime,  
  9.         btaff = astaff,  
  10.         bcontent = acontent;  

这是类视图的更新方法,这也是oracle所独有的。

先把对应的数据全部抽取出来,然后更新表一样更新数据,这里需要注意的是,必须保证表的数据唯一性。

这就要求a,b两表的关联字段上都是具备唯一性的。

二,

  1. update t_tmprpt_firstreplycosttime t  
  2.    set (t.firstreplytime,  
  3.        t.dealstaff,  
  4.        t.firstreplyfailcontent)  
  5.        = (select a.suggesttime,  
  6.                  a.suggester,  
  7.                  substr(a.remark,instr(a.remark,'】',1)+2)  
  8.             from t_wf_suggesthis a  
  9.            where t.serialno = a.serialno  
  10.              and t.serviceclassid = a.serviceclassid)  
  11.   where t.serialno in (select b.serialno  
  12.                          from t_wf_suggesthis b  
  13.                         where b.serialno = t.serialno  
  14.                           and b.serviceclassid = t.serviceclassid);  

这种就是使用子查询来确定更新的数据。

11-14 01:05
查看更多