PropertyName
すべてのノードから値を読み取りたい場合は、次の解決策を試してください。
DECLARE @MyTable TABLE(
ID INT IDENTITY(1,1) PRIMARY KEY,
XmlCol XML NOT NULL
);
INSERT @MyTable(XmlCol)
VALUES (N'<book Category="Hobbies And Interests" PropertyName="C#" CategoryID="44" />');
INSERT @MyTable(XmlCol)
VALUES (N'<sport Category="Hobbies And Interests" PropertyName="Cricket" CategoryID="46" />');
INSERT @MyTable(XmlCol)
VALUES (N'<CocoJambo PropertyName="Aircraft carrier" Category="Hobbies And Interests"> <CocoJamboChild PropertyName="Airplane"/> </CocoJambo>');
INSERT @MyTable(XmlCol)
VALUES (N'<sport CategoryID="CocoJamboID" />');
SELECT a.*,
b.Node.value('(.)','NVARCHAR(100)') AS PropertyName_Value
FROM @MyTable a
-- OUTER APPLY or CROSS APLLY
OUTER APPLY a.XmlCol.nodes('//@*[local-name(.)="PropertyName"]') b(Node);
結果:
ID XmlCol PropertyName_Value
----------- ----------------------------------------- ------------------
1 <book Category="Hobbies And Interes ... C#
2 <sport Category="Hobbies And Intere ... Cricket
3 <CocoJambo PropertyName="Aircraft c ... Aircraft carrier
3 <CocoJambo PropertyName="Aircraft c ... Airplane
4 <sport CategoryID="CocoJamboID" /> NULL
ただし、少なくとも 1 つの PropertyName
属性を持つ XML フレームを含むすべての行を表示する場合は、次のようにします。
SELECT a.*
FROM @MyTable a
WHERE a.XmlCol.exist('//@*[local-name(.)="PropertyName"]')=1;
結果:
ID XmlCol
----------- ----------------------------------------------------------------------------------
1 <book Category="Hobbies And Interests" PropertyName="C#" CategoryID="44" />
2 <sport Category="Hobbies And Interests" PropertyName="Cricket" CategoryID="46" />
3 <CocoJambo PropertyName="Aircraft carrier" Category="Hobbies And Interests"><CocoJ ...