SQL Server 2008 r2 中的 XML 查询/修改问题
我对 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要测试的内容:
请注意,这只会替换每一行 XML 中的enderTypeId 的一个值。如果 xml 中有多个enderTypeId(在一行中)并且您想要替换它们,则需要将更新语句放入
while
循环中,并替换为where XMLCol.exist ('/TenderLines/TenderLineItem/tenderTypeId[.= sql:variable("@redundant")]') = 1
。Something to test on:
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 aswhere XMLCol.exist('/TenderLines/TenderLineItem/tenderTypeId[. = sql:variable("@redundant")]') = 1
.