SQL根据条件批量update更新字段案例

今天学会批量update语句,留下纪念一下:

SQL导入一张城市表,其中有个字段是电话区号,当前只有地级市有区号信息,但要求其下属的区县都需加入区号信息,手工处理太痛苦了。

这段SQL代码由此而生,希望对有同样需求的新手有帮助:

   1:  update t_CityCode set TelCode = b.TelCode
   2:  from t_CityCode,(select * from t_CityCode where CityCode !='00' and CountyCode='00' )as b 
   3:  where t_CityCode.City=b.City and t_CityCode.TelCode is null

 

逻辑很简单:

update 表1 set 区号 =  虚拟表2的区号 from 表1,(找出地级市) as 虚拟表2 where 表1的地级市=虚拟表2的城市 并且 表1的区位为空

posted @ 2012-09-10 02:33  易水胡言  阅读(4432)  评论(0编辑  收藏  举报
无觅