我现在有一张表:
Table1 :
ID TONO          Factory  ChargeN          ChargeCd Charge Currency
1 1909030068 Factory1 Trucking        100177 1 CNY
2 1909030068 Factory1 Barge        100029 0 CNY

Table2
ID   Factory
1    Factory1
2    Factory2
3    Factory3
4    Factory4
5    Factory5我要对Table1做以下处理
如果Table1.Factory 在 table2中存在, 将用 Trucking (100177) 所对应的 Charge ,Currency 更新  Barge (100029) 对应的 Charge,Currency.
同时将 Trucking (100177) 所对应的 Charge ,Currency 分别设为 空.
请用一条SQL 解决(不考虑用触发事件或存储过程)

解决方案 »

  1.   


    --> 测试数据: @Table1
    declare @Table1 table (ID int,TONO int,Factory varchar(8),ChargeN varchar(8),ChargeCd int,Charge int,Currency varchar(3))
    insert into @Table1
    select 1,1909030068,'Factory1','Trucking',100177,1,'CNY' union all
    select 2,1909030068,'Factory1','Barge',100029,0,'CNY'
    --> 测试数据: @Table2
    declare @Table2 table (ID int,Factory varchar(8))
    insert into @Table2
    select 1,'Factory1' union all
    select 2,'Factory2' union all
    select 3,'Factory3' union all
    select 4,'Factory4' union all
    select 5,'Factory5'--修改
    update b set charge=a.charge,currency=a.currency from @table1 a,@table1 b
    where a.tono=b.tono and a.chargecd=100177 and b.chargecd=100029
    and a.Factory=b.Factory and exists(select 1 from @table2 where factory=a.factory)
    --查看结果:
    select * from @Table1
      

  2.   

    update Table1 t
    set Charge = null, Currency = null
    where exists(select 1 from Table2 where Factory = t.Factory)
      

  3.   

    Table1如果Factory1    有多行呢
      

  4.   

     to: llxlett (小米) 
     相同的TONo只会对应一个Factory
      

  5.   

    pt1314917和lihan6415151528都是正确的,可否将其在一条语句中实现
      

  6.   


    这是一条语句啊,上面只不过做了一些测试数据,,用来测试用的--直接这样就OK乐
    update b set charge=a.charge,currency=a.currency from table1 a,table1 b
    where a.tono=b.tono and a.chargecd=100177 and b.chargecd=100029
    and a.Factory=b.Factory and exists(select 1 from @table2 where factory=a.factory)