zoukankan      html  css  js  c++  java
  • SQL批量更新

    如下代码,怎样写成一句,或中间用什么字符隔开才能同时运行?
    update Yao_Article set Author='/1/35/' where Author='山东 - 历下'  
    update Yao_Article set Author='/1/36/' where Author='山东 - 市中'  
    update Yao_Article set Author='/1/37/' where Author='山东 - 槐荫'  
    update Yao_Article set Author='/1/38/' where Author='山东 - 天桥'  
    update Yao_Article set Author='/1/39/' where Author='山东 - 历城'  
    update Yao_Article set Author='/1/40/' where Author='山东 - 长清'  
    update Yao_Article set Author='/1/41/' where Author='山东 - 平阴'  
    update Yao_Article set Author='/1/42/' where Author='山东 - 济阳'
     
     
     
     
    用case语句试试:
     
    update Yao_Article set Author=(case Author when '山东 - 历下'  then '/1/35/' when '山东 - 市中' then '/1/36/' ...... when '山东 - 济阳' then '/1/42/' else Author end) where Author like '山东 - %'
     
    ***************************
     
    SQL数据库中有1个表名叫 ASC001
    表中有item_no,item_subo 两个字段
    这个表中有5万行数据
    其中有8000行数据的item_no<>item_subo
    现在要将item_no<>item_subo的这8000行数据修改为item_no=item_subo
    同时还有45个表,并且这45个表中都有item_no字段,要将这45个表中的item_no同步更新,有什么办法可以解决吗?
     
     
    一、临时表法
    1、select item_no, item_subo into TT_diff from ASC001 where item_no <> item_subo
    必要时,在TT_diff上建立索引,如create index I_TT_item_no on TT_diff( item_no )
    2、针对ASC001以及另外的45个表,各自执行如下命令:
    update 表 set t.item_no = x.item_subo from 表 t, TT_diff x where t.item_no = x.item_no
    3、drop table TT_diff
    二、触发器法
    1、在ASC001上建立如下触发器
    create trigger TR_update_item_no
    on ASC001
    for update
    as
    begin
    if update( item_no ) then
    begin
    -- 针对45个表,重复如下框架的命令
    update 表
    set t.item_no = i.item_no
    from 表 t, inserted i, deleted d
    where t.item_no = i.item_no
    and i.item_no <> d.item_no
    end
    end
    2、执行命令,使触发器逻辑发生作用
    update ASC001 set item_no = item_subo where item_no <> item_subo
    3、删除触发器
    drop trigger TR_update_item_no
     
    说明:若采用第二种方法,当ASC001上已经有触发器的时候,需要先保存其脚本,干完这个活儿后再恢复,并且,所有改变发生在一个事务中,要求日志空间得足够大。
  • 相关阅读:
    利用python进行数据分析之数据聚合和分组运算
    利用python进行数据分析之绘图和可视化
    利用python进行数据分析之数据规整化
    利用python进行数据分析之pandas库的应用(二)
    利用python进行数据分析之pandas库的应用(一)
    利用python进行数据分析之数据加载存储与文件格式
    git 本地无版本仓库推送到远程新仓库命令
    Docker 部署Dotnet Core MVC项目
    css 单行图片文字水平垂直居中汇总
    IL命令初学者要知道的事
  • 原文地址:https://www.cnblogs.com/huapox/p/3251430.html
Copyright © 2011-2022 走看看