简体   繁体   中英

SQL Server: How to get hold of XML attributes in a query?

I'm currently trying to get all of the attributes from some XML with an SQL query.

I've been attempting the following to retrieve it, but I must be missing something rather fundamental.

DECLARE @T varchar(max) 
SET @T = 
'<root>
    <Field FieldRowId="1000">
    <Items>
        <Item Name="CODE"/>
        <Item Name="DATE"/>
    </Items>
        <Attributes>
        <Attribute ID ="1"/>
        </Attributes>
    </Field>
    <Field FieldRowId="2000">
    <Items>
                <Item Name="CODE"/>
                <Item Name="DATE"/>
    </Items>
    <Attributes>
        <Attribute ID ="2"/>
    </Attributes>
    </Field>
</root>'

DECLARE @X xml

SET @X = CAST(@T as xml)
SELECT Y.ID.value('@FieldRowId', 'int') as FieldID, 
   Y.ID.value('/Items/@Name', 'varchar(max)') as "Name",
   Y.ID.value('/Attributes/@ID', 'int') as AttributeID
FROM @X.nodes('/root/Field') as Y(ID)

You would have to try something like this: (the @name attribute is on the "Item" element - not the "Items" !)

SET @X = CAST(@T as xml)
SELECT 
   Y.ID.value('(@FieldRowId)[1]', 'int') as FieldID,
   Y.ID.value('(Items/Item/@Name)[1]', 'varchar(max)') as "Name",
   Y.ID.value('(Attributes/Attribute/@ID)[1]', 'int') as AttributeID
FROM @X.nodes('/root/Field') as Y(ID)

Marc

SELECT t1.fieldId, name, attributeId
FROM ( SELECT Y.ID.value('../../@FieldRowId', 'int') as FieldID
     , Y.ID.value('@Name', 'varchar(max)') as Name    
FROM @T.nodes('(/root/Field/Items/Item)') as Y(ID)
) t1 -- alias the first result 
JOIN
(SELECT Y.ID.value('../../@FieldRowId', 'int') as FieldID
    , Y.ID.value('@ID', 'int') as AttributeID
FROM @T.nodes('(/root/Field/Attributes/Attribute)') as Y(ID)
) t2 -- alias the second result 
on t1.fieldid = t2.FieldID -- join them on a common column

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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