使用从xml属性中提取的串联字符串更新MSSQL db表

时间:2017-10-27 23:19:15

标签: sql-server xml

我有一个包含2列,ID和Value的mssql db表。

Id     Value (xml string)
1      <DAMP fullMedia="">
        <mediaItem>
           <Image id="1262" parentID="1244" level="4" creatorID="1" sortOrder="17" createDate="2014-01-28T10:47:06" updateDate="2014-01-29T11:15:27" nodeName="Juno 3D Side Terrasync" urlName="juno-3d-side-terrasync" path="-1,1236,1359,1244,1262" isDoc="" nodeType="1032" writerName="norgeodesi" writerID="1" version="6ac0d7fc-8482-4ddc-9d22-765c19586b01" template="0" nodeTypeAlias="Image">
  <umbracoFile>/media/3521/Juno_3D-Side_TerraSync.jpg</umbracoFile>
  <umbracoWidth>986</umbracoWidth>
  <umbracoHeight>1179</umbracoHeight>
  <umbracoBytes>202612</umbracoBytes>
  <umbracoExtension>jpg</umbracoExtension>
</Image>
  </mediaItem>
</DAMP>

2 <DAMP fullMedia="">
    <mediaItem>
      <Image id="1392" version="68d274be-5f13-45e9-a5c7-18e7ec221ca9" parentID="1244" level="4" writerID="1" nodeType="1032" template="0" sortOrder="60" createDate="2014-01-29T15:07:52" updateDate="2014-01-29T15:07:52" nodeName="Terrasync Update" urlName="terrasyncupdate" writerName="norgeodesi" nodeTypeAlias="Image" path="-1,1236,1359,1244,1392">
        <umbracoFile>/media/5396/TerraSync_update.jpg</umbracoFile>
        <umbracoWidth>240</umbracoWidth>
        <umbracoHeight>294</umbracoHeight>
        <umbracoBytes>25084</umbracoBytes>
        <umbracoExtension>jpg</umbracoExtension>
      </Image>
    </mediaItem>
  <mediaItem>
    <Image id="1391" version="4f3013cf-5485-469d-b403-c84b10901d9b" parentID="1244" level="4" writerID="1" nodeType="1032" template="0" sortOrder="59" createDate="2014-01-29T15:07:51" updateDate="2014-01-29T15:07:51" nodeName="Terrasync Screensnap Submeter" urlName="terrasyncscreensnapsubmeter" writerName="norgeodesi" nodeTypeAlias="Image" path="-1,1236,1359,1244,1391">
      <umbracoFile>/media/5395/TerraSync_screensnap_submeter.jpg</umbracoFile>
      <umbracoWidth>240</umbracoWidth>
      <umbracoHeight>320</umbracoHeight>
      <umbracoBytes>47988</umbracoBytes>
      <umbracoExtension>jpg</umbracoExtension>
    </Image>
  </mediaItem>
</DAMP>

我想编写一个更新查询,将Value列中的字符串转换为xml,然后从包含Image Id的xml构建一个逗号分隔的字符串

我希望将该表更新为以下内容:

Id     Value (string)
1      1262
2      1392, 1391

由于

ALI

2 个答案:

答案 0 :(得分:0)

据我所知,您希望将Image Id的逗号分隔值与XML值(存储为VARCHAR)一起尝试使用query()的以下查询:

CREATE TABLE  #temp (id int, value varchar(max))
insert into #temp(id,value) values

(1,      '<DAMP fullMedia="">
        <mediaItem>
           <Image id="1262" parentID="1244" level="4" creatorID="1" sortOrder="17" createDate="2014-01-28T10:47:06" updateDate="2014-01-29T11:15:27" nodeName="Juno 3D Side Terrasync" urlName="juno-3d-side-terrasync" path="-1,1236,1359,1244,1262" isDoc="" nodeType="1032" writerName="norgeodesi" writerID="1" version="6ac0d7fc-8482-4ddc-9d22-765c19586b01" template="0" nodeTypeAlias="Image">
  <umbracoFile>/media/3521/Juno_3D-Side_TerraSync.jpg</umbracoFile>
  <umbracoWidth>986</umbracoWidth>
  <umbracoHeight>1179</umbracoHeight>
  <umbracoBytes>202612</umbracoBytes>
  <umbracoExtension>jpg</umbracoExtension>
</Image>
  </mediaItem>
</DAMP>'),

(2, '<DAMP fullMedia="">
    <mediaItem>
      <Image id="1392" version="68d274be-5f13-45e9-a5c7-18e7ec221ca9" parentID="1244" level="4" writerID="1" nodeType="1032" template="0" sortOrder="60" createDate="2014-01-29T15:07:52" updateDate="2014-01-29T15:07:52" nodeName="Terrasync Update" urlName="terrasyncupdate" writerName="norgeodesi" nodeTypeAlias="Image" path="-1,1236,1359,1244,1392">
        <umbracoFile>/media/5396/TerraSync_update.jpg</umbracoFile>
        <umbracoWidth>240</umbracoWidth>
        <umbracoHeight>294</umbracoHeight>
        <umbracoBytes>25084</umbracoBytes>
        <umbracoExtension>jpg</umbracoExtension>
      </Image>
    </mediaItem>
  <mediaItem>
    <Image id="1391" version="4f3013cf-5485-469d-b403-c84b10901d9b" parentID="1244" level="4" writerID="1" nodeType="1032" template="0" sortOrder="59" createDate="2014-01-29T15:07:51" updateDate="2014-01-29T15:07:51" nodeName="Terrasync Screensnap Submeter" urlName="terrasyncscreensnapsubmeter" writerName="norgeodesi" nodeTypeAlias="Image" path="-1,1236,1359,1244,1391">
      <umbracoFile>/media/5395/TerraSync_screensnap_submeter.jpg</umbracoFile>
      <umbracoWidth>240</umbracoWidth>
      <umbracoHeight>320</umbracoHeight>
      <umbracoBytes>47988</umbracoBytes>
      <umbracoExtension>jpg</umbracoExtension>
    </Image>
  </mediaItem>
</DAMP>')

  SELECT Id ,
       REPLACE(
                  CAST(Value AS XML).query('data(DAMP/mediaItem/Image/@id)').value(
                                                                           '.' ,
                                                                           'varchar(100)'
                                                                       ) ,
                  ' ' ,
                  ', '
              ) AS Value
FROM  #temp;

drop table #temp

结果:

Id  Value
1   1262
2   1392, 1391

答案 1 :(得分:0)

我不确定您是否真的想要WebView列中的update数据,但此解决方案可以做到这一点。你可以在任何Value停留(见下文)。这是一个有效的例子。

step