2個表, ip_list 和 it_audit_checklist , 表'ip_list'中有字段:ip,user_name,dept,obu,remark,ip和obu為關鍵字。表'it_audit_checklist'中有字段:ip,obu,user_name,dept,pc_plus_install,ad_install等字段,ip和obu為關鍵字。 現在要以ip_list表中的內容更新it_audit_checklist表中的相應內容,以關鍵字ip和obu為基準。有2中方法,如下:
方法1:
declare @ip char(16),@obu char(4),@user_name char(30),@dept char(20),@count int
select *,identity(int,1,1) as sn into #t --建立臨時表't',增加列'sn'表示行號
from ip_list
select @count=max(sn) --取出最大行號
from #t
while @count>0 --迴圈從臨時表中取出資料並插入到表it_audit_checklist中
begin
select @ip=ip,@obu=obu,@user_name=user_name,@dept=dept
from #t
where sn=@count
update it_audit_checklist
set user_name=@user_name,dept=@dept
where ip=@ip and obu=@obu
select @count=@count-1
end方法2:
update a
set a.user_name=b.user_name,a.dept=b.dept
from ip_list a inner join it_audit_checklist b
on a.ip=b.ip and a.obu=b.obu
方法1是比較傳統的做法,方法2就更為簡單,明了
通過sql批量更新字段內容
1.也許現在大家都不需要寫sql了,鑑於我技術比較落後,加上開發的系統還有在使用,所以還是有業務需要用到。2.伺服器搬家,導致資料庫裡,伺服器的路徑發生改變,所以要批量替換。設定網域名稱就不用管這個坑 檢索出要替換的內容條數 select count from shop item where pho...
批量更新字串列表字段
資料庫qbs.qbs dep role如下,引用的workid所在表qbs.qbs work 現在想更新qbs dep role表下所有worklist中含有workid2和4列 對應業務意義為無這兩個工作區訪問許可權 ibatis處理如下 update qbs.qbs dep role as r ...
SQL根據條件批量update更新字段案例
今天學會批量update語句,留下紀念一下 sql匯入一張城市表,其中有個字段是 區號,當前只有地級市有區號資訊,但要求其下屬的區縣都需加入區號資訊,手工處理太痛苦了。這段sql 由此而生,希望對有同樣需求的新手有幫助 1 update t citycode set telcode b.telcod...