日期:2014-05-18  浏览次数:20478 次

今日问题:MSSQL数据更新,求指教,求思路
我有一个最终数据表,tableEnd
字段1 字段2(唯一) 字段3 字段4 字段5
name No Desc classs stu
某某 1 123,456,1254,123456 102
小明 2 5321,123

和一个我已经处理好的表tableRaw

字段1 字段2(唯一) 字段3  
name No Desc  
某某 1 123
小明 2 5321
某某 1 555
某某 1 666
小黑 3 456

最终得到tableEed里面的数据变为
字段1 字段2(唯一) 字段3 字段4 字段5
name No Desc classs stu
某某 1 123,456,1254,123456,555,666 102
小明 2 5321,123
小黑 3 456
就是,如果,tableRaw中的No在tableEnd的No中存在,则将tableRaw的Desc添加到tableEnd中,不存在则添加一行新的数据.
我本来是使用select into.但是,这样只能够将我要的数据创建到一个新表中,而不能够将数据更新到我的数据表,求指教

------解决方案--------------------
SQL code

create table tableEnd
(name varchar(6), Nos int, Descs varchar(50))

insert into tableEnd
select '某某', 1, '123,456,1254,123456' union all
select '小明', 2, '5321,123' 

create table tableRaw
(name varchar(6), Nos int, Descs varchar(50))
   
insert into tableRaw
select '某某', 1, '123' union all
select '小明', 2, '5321' union all
select '某某', 1, '555' union all
select '某某', 1, '666' union all
select '小黑', 3, '456'

select * from tableEnd

name   Nos         Descs
------ ----------- --------------------------------------------------
某某     1           123,456,1254,123456
小明     2           5321,123

select * from tableRaw

name   Nos         Descs
------ ----------- --------------------------------------------------
某某     1           123
小明     2           5321
某某     1           555
某某     1           666
小黑     3           456


;with t as
(select a.name,a.Nos,substring(a.Descs,b.number,charindex(',',a.Descs+',',b.number)-b.number) Descs 
from tableEnd a
inner join master.dbo.spt_values b
on b.[type]='P' and substring(','+a.Descs,b.number,1)=','
union
select name,Nos,Descs from tableRaw
)
select name,Nos,
stuff(
(select ','+Descs from t t2 
where t2.name=t.name and t2.Nos=t.Nos for xml path('')),
1,1,'') Descs
from t
group by name,Nos

name   Nos         Descs
------ ----------- -------------------------------
某某     1           123,123456,1254,456,555,666
小明     2           123,5321
小黑     3           456

(3 row(s) affected)

------解决方案--------------------
SQL code

create table tableEnd
(name varchar(6), Nos int, Descs varchar(50))

insert into tableEnd
select '某某', 1, '123,456,1254,123456' union all
select '小明', 2, '5321,123' 

create table tableRaw
(name varchar(6), Nos int, Descs varchar(50))
   
insert into tableRaw
select '某某', 1, '123' union all
select '小明', 2, '5321' union all
select '某某', 1, '555' union all
select '某某', 1, '666' union all
select '小黑', 3, '456'

select * from tableEnd

name   Nos         Descs
------ ----------- --------------------------------------------------
某某     1           123,456,1254,123456
小明     2           5321,123

select * from tableRaw

name   Nos         Descs
------ ----------- --------------------------------------------------
某某     1           123
小明     2           5321
某某     1           555
某某     1           666
小黑     3           456


;with t as
(select a.name,a.Nos,substring(a.Descs,b.number,charindex(',',a.Descs+',',b.number)-b.number) Descs 
from tableEnd a
inner join master.dbo.spt_values b
on b.[type]='P' and substring(','+a.Descs,b.number,1)=','
union
select name,Nos,Descs from tableRaw
)
merge tableEnd as o
using
(select name,Nos,
stuff(
(select ','+Descs from t t2 
where t2.name=t.name and t2.Nos=t.Nos for xml path('')),
1,1,'') Descs
from t group by name,Nos) as s
on o.Nos=s.Nos and o.name=s.name
when matched then
 update set o.Descs=s.Descs
when not matched then
 insert(n