我现在有一张表:
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 解决(不考虑用触发事件或存储过程)
解决方案 »
- 新年快乐,请教个问题:select多个表,select部分和from部分中,表的顺序影响效率吗?
- 如何选出每个组中满足条件的第一条?
- 怎么样来统计有多少条日志?
- 急!!!!能不能 从左边开始截取3个后 从第四个开始截取到最前面????
- 使用存储过程中的时间参数问题,请帮助我看看,谢谢!
- sqlserver如何审核登录密码更改记录的?
- SQL Server 做restore居然找不到备份的文件
- *************数据库表建立关系的问题,大家来帮帮忙!
- 重分求解:怎样来创建一个SQL Server库(用asp语句实现)!!!!
- 不好意思,最近老是劳驾高手,帮写一个存储过程。
- 求一联合查询的sql语句 望高手指点!
- SQL2000的事件探查器,在SQL2005哪里能打开?
--> 测试数据: @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
set Charge = null, Currency = null
where exists(select 1 from Table2 where Factory = t.Factory)
相同的TONo只会对应一个Factory
这是一条语句啊,上面只不过做了一些测试数据,,用来测试用的--直接这样就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)