要更新 XML 数据类型列,重复使用键,首选避免使用游标的方法
我有一个带有 ID(int 类型)和 XML 数据 blob(1 个 XML 类型列)的表。 表中有 10 行,我需要单独更新每个 XML blob/行。 有没有办法做到这一点:
而不是:
--Works ok to update XML by explicitly entering XML, or one-by-one:
declare @XMLNODE table (id int identity, doc xml)
insert @XMLNODE (doc) values (
'
<Root>
<Elements>
<Items>
<OldItem>
<ID>1</ID>
<Show Pointer="yes" />
<Display Pointer="Display">
<Detail1>some Details</Detail1>
</DisplayDetails>
</OldItem>
</Items>
</Elements>
</Root>'
)
--This is just an XML variable to get the output, but not related to the ID in table:
DECLARE @XMLOutput XML
SELECT @XMLOutput = (
SELECT a.value('(ID)[1]','int') as ID,
a.value('(Show/@Pointer)[1]', 'varchar(5)') AS ShowItem,
a.value('Display[1]/@Pointer[1]="Display"', 'varchar(10)') as DisplayDetails, -- Set to 'true' or 'false'
a.value('DisplayDetails[1][@Pointer[1]="Display"]/Detail1[1]', 'varchar(max)') as Detail1
FROM @XMLNODE t
cross apply
t.doc.nodes('//OldItem') x(a)
FOR XML PATH ('Items'),
ROOT('Elements')
)
相反,我想做这样的事情:
Declare @tempTable table(ID int not null, XMLData xml)
INSERT INTO @tempTable
SELECT OriginalTable.ID,
(--Perform above XML modification per row for each ID and put into a new table)
--每个 id 执行此操作以使用 ID、XML 填充 @tempTable,而不仅仅是返回 XML。 关于让它工作的任何想法(甚至可以使用光标)。
I have a table with an ID (int type) and a blob of XML data (1 XML type column).
I have 10 rows in the table, and I need to update each XML blob/row seperately.
Is there a way to do this:
Instead of:
--Works ok to update XML by explicitly entering XML, or one-by-one:
declare @XMLNODE table (id int identity, doc xml)
insert @XMLNODE (doc) values (
'
<Root>
<Elements>
<Items>
<OldItem>
<ID>1</ID>
<Show Pointer="yes" />
<Display Pointer="Display">
<Detail1>some Details</Detail1>
</DisplayDetails>
</OldItem>
</Items>
</Elements>
</Root>'
)
--This is just an XML variable to get the output, but not related to the ID in table:
DECLARE @XMLOutput XML
SELECT @XMLOutput = (
SELECT a.value('(ID)[1]','int') as ID,
a.value('(Show/@Pointer)[1]', 'varchar(5)') AS ShowItem,
a.value('Display[1]/@Pointer[1]="Display"', 'varchar(10)') as DisplayDetails, -- Set to 'true' or 'false'
a.value('DisplayDetails[1][@Pointer[1]="Display"]/Detail1[1]', 'varchar(max)') as Detail1
FROM @XMLNODE t
cross apply
t.doc.nodes('//OldItem') x(a)
FOR XML PATH ('Items'),
ROOT('Elements')
)
Instead, I want to do something like this:
Declare @tempTable table(ID int not null, XMLData xml)
INSERT INTO @tempTable
SELECT OriginalTable.ID,
(--Perform above XML modification per row for each ID and put into a new table)
--Do this per id to populate @tempTable with ID, XML not just return XML.
Any ideas on getting this to work (is it possible even using a cursor).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以下是如何更新 XML 列的简化示例。
修改前的表@T
修改后的表@T
Here is a simplified sample of how you can update the XML column.
Table @T before modify
Table @T after modify