简体   繁体   English

SQL server 2008 r2 中的 XML 查询/修改问题

[英]XML query/modify problem in SQL server 2008 r2

I'm fairly new to SQL and I'm trying to filter and change the values inside of column that holds an XML document for a purchased order.我对 SQL 相当陌生,我正在尝试过滤和更改包含已购买订单的 XML 文档的列内的值。 Here's an example of what the XML document looks like and what I'm searching for.这是 XML 文档的外观和我正在搜索的示例。

 <TenderLines>
   <TenderLineItem>
     <tenderTypeId>S0001-00000001</tenderTypeId>
     ..
     ..
    </TenderLineItem>
  <TenderLines>

I've got 6000+ rows and not all of them have the same tenderTypeId.我有 6000 多行,但并非所有行都具有相同的tenderTypeId。 I want to filter out the values in tenderTypeId that have 'S0001-00000001' and change them to '2'我想过滤掉tenderTypeId中具有'S0001-00000001'的值并将它们更改为'2'

So far this is what I've come up with.到目前为止,这就是我想出的。

USE LSPOS80
DECLARE @replacement as varchar(50)
DECLARE @redundant as varchar(50)
SET @replacement = '2'
SET @redundant = 'S0001-00000001'

Update dbo.POSISTRANSACTIONTABLE
SET TRANSACTIONXML.modify
('replace value of(/RetailTransaction/TenderLines/TenderLineItem/tenderTypeId/@redundant) [1] with sql:variable("@replacement")')

The query is executed successfully but nothing changes and I was wondering if any of you could read over this and possibly give me tips.查询已成功执行,但没有任何变化,我想知道你们中的任何人是否可以阅读此内容并可能给我提示。

Thanks for your time, with best regards, Valdi.感谢您抽出宝贵时间,向您致以最诚挚的问候,瓦尔迪。

PS I'm using Microsoft SQL Server 2008 R2 - Express Edition PS 我正在使用 Microsoft SQL Server 2008 R2 - Express Edition

update dbo.POSISTRANSACTIONTABLE set
  TRANSACTIONXML.modify('replace value of (/TenderLines/TenderLineItem/tenderTypeId/text())[1] with (sql:variable("@replacement"))')
where XMLCol.exist('/TenderLines/TenderLineItem/tenderTypeId[. = sql:variable("@redundant")]') = 1

Something to test on:要测试的东西:

declare @T table(XMLCol xml)
insert into @T values 
('<TenderLines>
   <TenderLineItem>
     <tenderTypeId>S0001-00000001</tenderTypeId>
    </TenderLineItem>
 </TenderLines>'),
('<TenderLines>
   <TenderLineItem>
     <tenderTypeId>S0003-00000003</tenderTypeId>
    </TenderLineItem>
 </TenderLines>')

DECLARE @replacement as varchar(50)
DECLARE @redundant as varchar(50)
SET @replacement = '2'
SET @redundant = 'S0001-00000001'

update @T set
  XMLCol.modify('replace value of (/TenderLines/TenderLineItem/tenderTypeId/text())[1] with (sql:variable("@replacement"))')
where XMLCol.exist('/TenderLines/TenderLineItem/tenderTypeId[. = sql:variable("@redundant")]') = 1

select *
from @T

Note that this will only replace one value of tenderTypeId in the XML for each row.请注意,这只会替换 XML 中每一行的一个tenderTypeId 值。 If you have multiple tenderTypeId's in the xml (in one row) and you want to replace them all you need to put the update statement in a while loop and replace as long as where XMLCol.exist('/TenderLines/TenderLineItem/tenderTypeId[. = sql:variable("@redundant")]') = 1 .如果您在 xml(在一行中)中有多个tenderTypeId,并且您想要替换它们,您需要将更新语句放在一个while循环中并替换只要where XMLCol.exist('/TenderLines/TenderLineItem/tenderTypeId[. = sql:variable("@redundant")]') = 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM