日期:2014-05-17  浏览次数:20575 次

一条更新语句!
表A

select '1','PP,*' union all 
select '1','*' union all 
select '1','PP' union all 
select  '2','uu,*' union all 
select '2','uu' union all 
select '2','*'  union all 
select '3','' union all 
select '3','*' 

表B

select '1','PP,*' 
select '2','UU,*'
select '3','*'

根据表B的类型更新表A  A与B的第一个字段关连 结果:
1       PP,*
1       PP,*
1       PP,*
2       uu,*
2       uu,*
2       uu,*
3       *
3       *

------解决方案--------------------
--select '1' id,'PP,*'NAME INTO #A
-- union all 
--select '1','*' union all 
--select '1','PP' union all 
--select  '2','uu,*' union all 
--select '2','uu' union all 
--select '2','*'  union all 
--select '3','' union all 
--select '3','*' 



--select '1' id,'PP,*' NAME INTO #B
--union all 
--select '2','UU,*'
--union all 
--select '3','*'

SELECT * FROM #a
SELECT * FROM #b

UPDATE #a
SET #a.name=#b.name
FROM #b 
WHERE #a.id=#b.id

/*
id   NAME
---- ----
1    PP,*
1    PP,*
1    PP,*
2    UU,*
2    UU,*
2    UU,*
3    *
3    *
*/

------解决方案--------------------

select '1' id,'PP,*' descr INTO #a union all 
select '1','*' union all 
select '1','PP' union all 
select  '2','uu,*' union all 
select '2','uu' union all 
select '2','*'  union all 
select '3','' union all 
select '3','*' 

select '1' id,'PP,*' descr INTO #b union all 
select '2','UU,*' union all 
selec