Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6924118
  • 博文数量: 700
  • 博客积分: 10821
  • 博客等级: 上将
  • 技术积分: 12011
  • 用 户 组: 普通用户
  • 注册时间: 2005-12-02 10:41
个人简介

大数据、ML、AI、云计算openstack、Linux、SpringCloud。

文章分类

全部博文(700)

分类: Oracle

2011-02-11 18:47:19

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);  

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

阅读(2445) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2011-03-06 09:11:00

很好的, 收藏了 推荐一个博客,提供很多免费软件编程电子书下载: http://free-ebooks.appspot.com