首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

SQL 更新话语

2012-08-02 
SQL 更新语句表1:ID1ID2ID3ID41111334112132412343551354764……表2ID1ID2ID3ID41111NULL1121NULL1234NULL13

SQL 更新语句
表1:
ID1 ID2 ID3 ID4  
11 1 1 334  
11 2 1 324 
12 3 4 355
13 5 4 764
……  

表2
ID1 ID2 ID3 ID4  
11 1 1 NULL 
11 2 1 NULL 
12 3 4 NULL 
13 5 4 NULL 
……
各位高手,怎么把表2的ID4列更新为同表1的一样
后边还有很多数据,但是规律就是这样的……

[解决办法]

SQL code
update t2set t2.id4=t1.id4from t1where t1.id1=t2.id1 and t1.id2=t2.id2 and t1.id3=t.id3
[解决办法]
SQL code
--> 测试数据: #tab1if object_id('tempdb.dbo.#tab1') is not null drop table #tab1create table #tab1 ([ID1] int,[ID2] int,[ID3] int,[ID4] int)insert into #tab1select 11,1,1,334 union allselect 11,2,1,324 union allselect 12,3,4,355 union allselect 13,5,4,764if object_id('tempdb.dbo.#tab2') is not null drop table #tab2create table #tab2 ([ID1] int,[ID2] int,[ID3] int,[ID4] sql_variant)insert into #tab2select 11,1,1,null union allselect 11,2,1,null union allselect 12,3,4,null union allselect 13,5,4,null--更新update #tab2 set ID4=t1.ID4 from #tab1 t1,#tab2 t2 where t1.ID1=t2.ID1 and t1.ID2=t2.ID2 and t1.ID3=t2.ID3 

热点排行