2011-08-04 91 views
1

我正在運行以下查詢以從xml列中提取數據...問題是如何提取數據![CDATA [「」]]?XML列提取![CDATA []]

select   
    CAST(xml as xml).value('(//@nodeName)[1]','nvarchar(20)') as NodeName,  
    CAST(xml as xml).value('(//![CDATA [prdDetDesc]])[1]','nvarchar(225)') as DetDesc, 
    CAST(xml as xml).value('(//prdImg)[1]','nvarchar(1000)') as prdImage 
from [dbo].[cmsContentXml]) 

我需要提取數據是[ 「]」]

感謝之間存在提前

+0

您能否提供一些示例XML數據,以顯示您的XML看起來像什麼以及您想要的輸出應該是什麼。 –

+0

<![CDATA [此考試的考生是通常追求數據庫管理員,數據庫開發人員或商業智能開發人員職業的專業人員。此外,他們可能是那些不使用Microsoft SQL Server作爲其主要工作職能的一部分,但希望展示他們廣泛的技術經驗的人員,例如開發人員,系統管理員和其他人員。 ]]> /m/967558/40.jpg Slmnoasp - 我想提取![CDATA []] – Gallop

回答

4

沒有什麼特別有CDATA節。

declare @xml xml = 
'<productDetailsDescription> 
    <![CDATA[Candidates for this exam are professionals who typically pursue careers as database administrators, database developers, or business intelligence developers. Additionally, they can be people who do not work with Microsoft SQL Server as a part of their primary job functions but who want to show their breadth of technology experience, such as developers, systems administrators, and others. ]]> 
</productDetailsDescription> 
<productImage>/m/967558/40.jpg</productImage> 
<application>Slmnoasp</application>' 

select @xml.value('/productDetailsDescription[1]', 'nvarchar(225)') 

它也處理混合值。

declare @xml xml = 
'<root>123<![CDATA[ABD]]>456</root>' 

select @xml.value('/root[1]', 'nvarchar(10)') 

結果:

(No column name) 
123ABD456 

編輯

從表而不是用強制轉換爲XML的變量:

select cast(xml as xml).value('/productDetailsDescription[1]', 'nvarchar(max)') as productDetailsDescription 
from YourTable 

這裏試試:http://data.stackexchange.com/stackoverflow/q/108293/

編輯2

您需要在查詢中指定節點名稱。您還必須決定是否應該在同一列中使用不同的節點,或者應該使用不同的列。下面我向你展示如何做到這一點。

declare @T table(xml nvarchar(max)) 

insert into @T values 
('<productDetailsDescription> 
    <![CDATA[Candidates for this exam are professionals who typically pursue careers as database administrators, database developers, or business intelligence developers. Additionally, they can be people who do not work with Microsoft SQL Server as a part of their primary job functions but who want to show their breadth of technology experience, such as developers, systems administrators, and others. ]]> 
</productDetailsDescription> 
<productImage>/m/967558/40.jpg</productImage> 
<application>Slmnoasp</application>') 

insert into @T values 
('<detailDescription> 
    <![CDATA[Candidates for this exam are professionals who typically pursue careers as database administrators, database developers, or business intelligence developers. Additionally, they can be people who do not work with Microsoft SQL Server as a part of their primary job functions but who want to show their breadth of technology experience, such as developers, systems administrators, and others. ]]> 
</detailDescription> 
<productImage>/m/967558/40.jpg</productImage> 
<application>Slmnoasp</application>') 

-- Get detailDescription in a column of its own 
select 
    cast(xml as xml).value('/productDetailsDescription[1]', 'nvarchar(max)') as productDetailsDescription, 
    cast(xml as xml).value('/detailDescription[1]', 'nvarchar(max)') as detailDescription 
from @T 

-- Get detailDescription in the same column as productDetailsDescription 
select 
    cast(xml as xml).value('/*[local-name()=("productDetailsDescription","detailDescription")][1]', 'nvarchar(max)') as detailDescription 
from @T 
+0

之間的數據謝謝你的迴應。 ..我沒有聲明任何變量...所以只是看着通過現有的查詢提取數據...請幫助編輯CAST(xml as xml).value('(//![CDATA [productDetailsDescription]])[1 ]','nvarchar(225)')作爲查詢中的prDetDesc行,因爲我得到null作爲record.Thanks – Gallop

+0

數據返回爲NULL爲productDetailsDescription字段我正在使用此語句 - CAST(xml as xml)。 value('/ productDetailsDescription [1]','varchar(max)')as productDetailsDescription – Gallop

+0

@user如果你得到NULL,這可能意味着你的XML看起來不像這個樣例。路徑中的第一個單獨的'/'指定了根,所以'productDetailsDescription'必須是一個根節點。如果不是這樣,爲了提高性能,最好的辦法就是添加完整路徑。接下來最好的方法是使用兩個'//'而不是隻有一個在節點的XML中進行搜索。 –