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

联表更新有关问题

2012-04-25 
联表更新问题有以下两表(部分数据):表PURTHTH001TH002TH05434012011060038 200.00000034012011060039 5000

联表更新问题
有以下两表(部分数据):
表PURTH
TH001TH002 TH054
34012011060038 200.000000
34012011060039 5000.000000
34012011060044 3000.000000
34012011060045 50.000000
34012011070001 1.000000
34012011060026 10000.000000
34982011060004 56.100000
34982011060005 10150.000000
表ACPTB
TB005TB006
34982011100014 
34982012010002 
34982011120007 
34982012020001 
34982012020001 
34982011100015 
34982011110010 
34982011120006 
现要更新表PURTH中字段TH054为0,条件是TH001和TH002联合时,不在表ACPTB中的TB005和TB006的联合,因为TH001和TH002组合在一起才是唯一,分开了就都不是唯一值


[解决办法]

SQL code
--我改了一条数据declare @PURTH table (TH001 int,TH002 int,TH054 numeric(11,6))insert into @PURTHselect 3401,2011060038,200.000000 union allselect 3401,2011060039,5000.000000 union allselect 3401,2011060044,3000.000000 union allselect 3401,2011060045,50.000000 union allselect 3401,2011070001,1.000000 union allselect 3401,2011060026,10000.000000 union allselect 3498,2011060004,56.100000 union allselect 3498,2011060005,10150.000000declare @ACPTB table (TB005 int,TB006 int)insert into @ACPTBselect 3401,2011060038 union allselect 3498,2012010002 union allselect 3498,2011120007 union allselect 3498,2012020001 union allselect 3498,2012020001 union allselect 3498,2011100015 union allselect 3498,2011110010 union allselect 3498,2011120006update @PURTH set TH054=0 from @PURTH a left join @ACPTB bon a.TH001=b.TB005 and a.TH002=b.TB006 where b.TB005 is nullselect * from @PURTH/*TH001       TH002       TH054----------- ----------- ---------------------------------------3401        2011060038  200.0000003401        2011060039  0.0000003401        2011060044  0.0000003401        2011060045  0.0000003401        2011070001  0.0000003401        2011060026  0.0000003498        2011060004  0.0000003498        2011060005  0.000000*/ 

热点排行