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

发布于 2024-11-19 05:13:47 字数 889 浏览 1 评论 0原文

我对 SQL 相当陌生,我正在尝试过滤和更改保存购买订单的 XML 文档的列内的值。 下面是 XML 文档的外观以及我正在搜索的内容的示例。

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

我有 6000 多行,但并非所有行都具有相同的enderTypeId。我想过滤掉enderTypeId中具有“S0001-00000001”的值并将它们更改为“2”

到目前为止,这就是我想出的。

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")')

查询已成功执行,但没有任何变化,我想知道你们中是否有人可以阅读此内容并可能给我提示。

感谢您抽出宝贵的时间,谨致以诚挚的问候,瓦尔迪。

PS 我正在使用 Microsoft SQL Server 2008 R2 - Express 版

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.
Here's an example of what the XML document looks like and what I'm searching for.

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

I've got 6000+ rows and not all of them have the same tenderTypeId. I want to filter out the values in tenderTypeId that have 'S0001-00000001' and change them to '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.

P.S. I'm using Microsoft SQL Server 2008 R2 - Express Edition

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

场罚期间 2024-11-26 05:13:47
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

要测试的内容:

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

请注意,这只会替换每一行 XML 中的enderTypeId 的一个值。如果 xml 中有多个enderTypeId(在一行中)并且您想要替换它们,则需要将更新语句放入 while 循环中,并替换为 where XMLCol.exist ('/TenderLines/TenderLineItem/tenderTypeId[.= sql:variable("@redundant")]') = 1

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. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文