欢迎来到天天文库
浏览记录
ID:57650671
大小:33.00 KB
页数:6页
时间:2020-08-30
《两表(多表)关联update.doc》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、两表(多表)关联update.txt28生活是一位睿智的长者,生活是一位博学的老师,它常常春风化雨,润物无声地为我们指点迷津,给我们人生的启迪。不要吝惜自己的爱,敞开自己的胸怀,多多给予,你会发现,你也已经沐浴在了爱河里。两表(多表)关联update--仅在where字句中的连接--这次提取的数据都是VIP,且包括新增的,所以顺便更新客户类别updatecustomersa--使用别名setcustomer_type='01'--01为vip,00为普通whereexists(select1fromtmp_cust_citybwhereb.cu
2、stomer_id=a.customer_id)3)两表(多表)关联update--被修改值由另一个表运算而来updatecustomersa--使用别名setcity_name=(selectb.city_namefromtmp_cust_citybwhereb.customer_id=a.customer_id)whereexists(select1fromtmp_cust_citybwhereb.customer_id=a.customer_id)--update超过2个值updatecustomersa--使用别名set(city_na
3、me,customer_type)=(selectb.city_name,b.customer_typefromtmp_cust_citybwhereb.customer_id=a.customer_id)whereexists(select1fromtmp_cust_citybwhereb.customer_id=a.customer_id)注意在这个语句中,=(selectb.city_name,b.customer_typefromtmp_cust_citybwhereb.customer_id=a.customer_id)与(selec
4、t1fromtmp_cust_citybwhereb.customer_id=a.customer_id)是两个独立的子查询,查看执行计划可知,对b表/索引扫描了2篇;如果舍弃where条件,则默认对A表进行全表更新,但由于(selectb.city_namefromtmp_cust_citybwherewhereb.customer_id=a.customer_id)有可能不能提供"足够多"值,因为tmp_cust_city只是一部分客户的信息,所以报错(如果指定的列--city_name可以为NULL则另当别论):01407,00000,"
5、cannotupdate(%s)toNULL"//*Cause://*Action:一个替代的方法可以采用:updatecustomersa--使用别名setcity_name=nvl((selectb.city_namefromtmp_cust_citybwhereb.customer_id=a.customer_id),a.city_name)或者setcity_name=nvl((selectb.city_namefromtmp_cust_citybwhereb.customer_id=a.customer_id),'未知')--当然这不
6、符合业务逻辑了4)上述3)在一些情况下,因为B表的纪录只有A表的20-30%的纪录数,考虑A表使用INDEX的情况,使用cursor也许会比关联update带来更好的性能:setserveroutputondeclarecursorcity_curisselectcustomer_id,city_namefromtmp_cust_cityorderbycustomer_id;beginformy_curincity_curloopupdatecustomerssetcity_name=my_cur.city_namewherecustomer_
7、id=my_cur.customer_id;/**此处也可以单条/分批次提交,避免锁表情况**/--ifmod(city_cur%rowcount,10000)=0then--dbms_output.put_line('----');--commit;--endif;endloop;end;5)关联update的一个特例以及性能再探讨在oracle的update语句语法中,除了可以update表之外,也可以是视图,所以有以下1个特例:update(selecta.city_name,b.city_nameasnew_namefromcustom
8、ersa,tmp_cust_citybwhereb.customer_id=a.customer_id)setcity_name=new_name这样
此文档下载收益归作者所有