2013-07-25 44 views
0

我已經XML存儲到數據庫中與不同的標記名稱,但相同的屬性名稱中使用節點名稱:讀取XML節點屬性沒有在SQL Server查詢

<book Category="Hobbies And Interests" PropertyName="C#" CategoryID="44" /> 
<sport Category="Hobbies And Interests" PropertyName="Cricket" CategoryID="46" /> 

這只是兩個例子,但是標籤名稱可以是任何東西。我想讀取所有節點的「PropertyName」屬性。

可能嗎?如果是的話,請任何人指導我。

回答

3
declare @xml xml 

set @xml = '<book Category="Hobbies And Interests" PropertyName="C#" CategoryID="44" /> 
<sport Category="Hobbies And Interests" PropertyName="Cricket" CategoryID="46" />' 

select T.c.value('@PropertyName', 'varchar(100)') 
from @xml.nodes('/*') T(c) 

如果用戶希望可以有沒有屬性名屬性的元素,你可以使用:

select T.c.value('@PropertyName', 'varchar(100)') 
from @xml.nodes('/*[@PropertyName]') T(c) 

如果你還期望元素可以嵌套,你可以使用:

select T.c.value('@PropertyName', 'varchar(100)') 
from @xml.nodes('//*[@PropertyName]') T(c) 
+0

謝謝!它爲我工作! –

+1

@ i-one:看看這個查詢'DECLARE @x XML; SET @ X = N '<書範疇= 「愛好和興趣」 類別ID = 「44」><章屬性名= 「111」/><體育類別= 「愛好和興趣」 類別ID = 「46」/>'; SELECT T.c.value('@ PropertyName','VARCHAR(100)')FROM @ x.nodes('/ *')T(c);'只返回空值,認爲有'PropertyName'屬性。 –

+2

@BogdanSahlean,你說得對。但在原始數據中沒有嵌套元素,這就是假設。如果需要,可以通過將'.nodes('/ *')'改爲'.nodes('// * [@ PropertyName]')'來進行概括。無論如何,我對接受標記沒有影響。 –

4

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); 
:如果你想從所有節點讀取 PropertyName

嘗試這種解決方案

結果:

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 

但是,如果你想顯示有至少一個PropertyName屬性XML framents所有行,那麼你可以嘗試:

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 ... 
+0

感謝您的時間和幫助,但上面的解決方案爲我工作! –

+3

然後根據所選解決方案來看這個例子:'DECLARE @x XML; SET @ x = N''; ('@ PropertyName','VARCHAR(100)')FROM @ x.nodes('/ *')T(c);'。您將看到一個具有「PropertyName =」111「'屬性的元素,但結果僅顯示NULL。 –

+0

喔!我看到...當節點不包含該屬性時,它將返回NULL,這將成爲未來的問題。我需要使用這個解決方案!非常感謝您的澄清和指導! –