34,590
社区成员
发帖
与我相关
我的任务
分享
create table people (ready1 xml)
insert into people
select
'<ExtPhoto>
<isOpenInfo>0</isOpenInfo>
<isOpenPic>1</isOpenPic>
<takeCity>
<takeCityInfo>
<cityName>福建;福州市</cityName>
<cityCode>3502</cityCode>
</takeCityInfo>
<takeCityInfo>
<cityName>江苏;南京</cityName>
<cityCode>3201</cityCode>
</takeCityInfo>
</takeCity>
<winningMark>1</winningMark>
</ExtPhoto>'
alter table people add cityCodes varchar(max)
update people
set cityCodes= cast(ready1.query('for $c in //cityCode where string($c)!="" return if(count((//takeCityInfo)[.>>($c/..)[1]])=0) then string($c) else concat($c,",")') as varchar(max)) from people
select cityCodes from people
/*
(1 行受影响)
cityCodes
3502, 3201
(1 行受影响)
*/
declare @xml xml
set @xml='<ExtPhoto>
<isOpenInfo>0</isOpenInfo>
<isOpenPic>1</isOpenPic>
<takeCity>
<takeCityInfo>
<cityName>福建;福州市</cityName>
<cityCode>3502</cityCode>
</takeCityInfo>
<takeCityInfo>
<cityName>江苏;南京</cityName>
<cityCode>3201</cityCode>
</takeCityInfo>
</takeCity>
<winningMark>1</winningMark>
</ExtPhoto>'
select (tb.c.value('(./cityName)[1]','varchar(20)')) as cityName,
(tb.c.value('(./cityCode)[1]','varchar(20)')) as cityCode
from @xml.nodes('//takeCityInfo') as tb(c)
/*
cityName cityCode
-------------------- --------------------
福建;福州市 3502
江苏;南京 3201
(2 行受影响)
create table people (ready1 xml)
insert into people
select
'<ExtPhoto>
<isOpenInfo>0</isOpenInfo>
<isOpenPic>1</isOpenPic>
<takeCity>
<takeCityInfo>
<cityName>福建;福州市</cityName>
<cityCode>3502</cityCode>
</takeCityInfo>
<takeCityInfo>
<cityName>江苏;南京</cityName>
<cityCode>3201</cityCode>
</takeCityInfo>
</takeCity>
<winningMark>1</winningMark>
</ExtPhoto>'
go
alter table people add cityCodes varchar(max)
update people
set cityCodes=
replace(replace(replace(cast(ready1.query('//cityName') as varchar(max)),'</cityName><cityName>',','),
'<cityName>',''),'</cityName>','')
select cityCodes from people
/*
cityCodes
-------------------------
福建;福州市,江苏;南京
*/