[英]SQL Server - OPENXML how to get attribute value
I have the following XML:我有以下 XML:
<Field FieldRowId="1000">
<Items>
<Item Name="CODE"/>
<Item Name="DATE"/>
</Items>
</Field>
I need to get the FieldRowId using OPENXML.我需要使用 OPENXML 获取 FieldRowId。 The SQL i have so far:到目前为止,我拥有的 SQL:
INSERT INTO @tmpField
([name], [fieldRowId])
SELECT [Name], --Need to get row id of the parent node
FROM OPENXML (@idoc, '/Field/Items/Item', 1)
EDIT: I added a root node to the xml.编辑:我向 xml 添加了一个根节点。 and demonstrated grabbing the ID.并演示了抢ID。 I'm assuming you have more than one field element in the xml.我假设您在 xml 中有多个字段元素。 This is assuming you have the starting XML;这是假设您有起始 XML; are you given an Item and have to traverse upwards?您是否给了一个项目并且必须向上遍历?
DECLARE @T varchar(max)
SET @T =
'<root>
<Field FieldRowId="1000">
<Items>
<Item Name="CODE"/>
<Item Name="DATE"/>
</Items>
</Field>
<Field FieldRowId="2000">
<Items>
<Item Name="CODE"/>
<Item Name="DATE"/>
</Items>
</Field>
</root>'
DECLARE @X xml
SET @X = CAST(@T as xml)
SELECT Y.ID.value('../../@FieldRowId', 'int') as FieldID,
Y.ID.value('@Name', 'varchar(max)') as "Name"
FROM @X.nodes('/root/Field/Items/Item') as Y(ID)
Using nodes is the way to go.使用节点是 go 的方式。 OPENXML
takes 1/8 of the SQL server memory every time it is used. OPENXML
每次使用都占用SQL服务器memory的1/8。 Although OPENXML
and nodes will generally have the same query performance.尽管OPENXML
和节点通常会具有相同的查询性能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.