如果你知道你的元素的順序始終是相同的,你可以使用position()
得到的值:
select X.N.value('data[1]', 'nvarchar(255)') as Number,
X.N.value('data[2]', 'nvarchar(255)') as Descritpion,
X.N.value('data[3]', 'nvarchar(255)') as Quanatity
from @T as T
cross apply T.XMLColumn.nodes('/items/item') as X(N)
否則,你需要使用alias
來獲取值。你的第四行有一個別名"Catalouge Number"
是從其他行不同,因此你可以有,作爲一個單獨的列:
select X.N.value('data[@alias="Number"][1]', 'nvarchar(255)') as Number,
X.N.value('data[@alias="Catalouge Number"][1]', 'nvarchar(255)') as CatalougeNumber,
X.N.value('data[@alias="Description"][1]', 'nvarchar(255)') as Descritpion,
X.N.value('data[@alias="Standard Packing Quantity"][1]', 'nvarchar(255)') as Quanatity
from @T as T
cross apply T.XMLColumn.nodes('/items/item') as X(N)
或者你可以在同一列中結合兩種:
select X.N.value('data[@alias=("Number","Catalouge Number")][1]', 'nvarchar(255)') as Number,
X.N.value('data[@alias="Description"][1]', 'nvarchar(255)') as Descritpion,
X.N.value('data[@alias="Standard Packing Quantity"][1]', 'nvarchar(255)') as Quanatity
from @T as T
cross apply T.XMLColumn.nodes('/items/item') as X(N)
你的where子句可以添加到查詢就像這樣:
where X.N.exist('data[@alias="Catalouge Number"]') = 1
或者,如果你想檢查對陣雙方號別名:
where X.N.exist('data[@alias=("Number","Catalouge Number")]') = 1
測試數據:
declare @T table(XMLColumn xml)
insert into @T values
('<items>
<item>
<data alias="Number">123N</data>
<data alias="Description">4 sq.mm Feed Through Terminal block in Grey colour</data>
<data alias="Standard Packing Quantity">100</data>
</item>
<item>
<data alias="Number">234N</data>
<data alias="Description">Toy</data>
<data alias="Standard Packing Quantity">100</data>
</item>
<item>
<data alias="Number">579N</data>
<data alias="Description">Doll</data>
<data alias="Standard Packing Quantity">100</data>
</item>
<item>
<data alias="Catalouge Number">234</data>
<data alias="Description">Vehicle</data>
<data alias="Standard Packing Quantity">324234</data>
</item>
</items>
')
編輯:
我看到你投你的XML列XML。如果列已經是數據類型XML,那麼這是沒有必要的,所以我猜你有一個varchar(max)或者其中的東西。如果是這樣,你需要做這樣的應用.nodes()
功能之前,鑄就成XML:
select X.N.value('data[@alias=("Number","Catalouge Number")][1]', 'nvarchar(255)') as Number,
X.N.value('data[@alias="Description"][1]', 'nvarchar(255)') as Descritpion,
X.N.value('data[@alias="Standard Packing Quantity"][1]', 'nvarchar(255)') as Quanatity
from @T as T
cross apply (select cast(XMLColumn as xml)) as X1(XMLColumn)
cross apply X1.XMLColumn.nodes('/items/item') as X(N)
where X.N.exist('data[@alias=("Number","Catalouge Number")]') = 1
編輯2
select xml.value('data[@alias=("Number","Catalouge Number")][1]', 'nvarchar(255)') as Number,
xml.value('data[@alias="Description"][1]', 'nvarchar(255)') as Descritpion,
xml.value('data[@alias="Standard Packing Quantity"][1]', 'nvarchar(255)') as Quanatity
from [dbo].[cmsContentXml] as Hierarchy
cross apply (select cast(xml as xml)) as X1(xml)
cross apply X1.xml.nodes('/items/item') as xml(N)
where xml.N.exist('data[@alias=("Number","Catalouge Number")]')=1
選擇xml.value(「數據[@alias =( 」數字「 「Catalouge號碼」)] [1]」, '爲nvarchar(255)')爲Number,xml.value( '數據[@別名= 「描述」] [1]', '爲nvarchar(255)')作爲Descritpion ,XML。作爲等級 從[dbo]。[cmsContentXml]作爲等級 交叉應用(選擇強制轉換(xml作爲xml))('data [@ alias ='標準包裝數量'] [1]','nvarchar(255)') )作爲X1(xml) 交叉應用X1(xml).nodes('/ items/item')作爲xml(N) 其中xml.N.exist('data [@alias =(「Number」,「Cataltoge Number 「)]')= 1 DB中的列加載了數據,所以我不能添加」declare @T table(XMLColumn xml)「 – Narayana
@Narayana - 您不應該使用'@ T'。你應該用你調用的表格替換@ T。 '@ T'就是我用來演示你如何做到這一點的。 –
Thanks..it正常工作 – Narayana