2013-10-16 39 views
1

我有一個XML數據字段在我的表(Sql server 2008 R2),我保存我的產品元數據的XML。以下是xml的格式。可能有不同數量的元數據與產品相關聯,因此沒有固定格式。贊在XML字段搜索 - sql server 2008 R2

<Item> 
     <Profile>170</Profile> 
     <Type>1</Type> 
     <InterfaceType DefaultImage="">Product</InterfaceType> 
     <Metadatas> 
     <Metadata> 
      <Control>2ad9cf19-2a88-4b6c-9f59-28e177679d2a</Control> 
      <ControlTypeID>5ae9a004-c0de-4692-a9bd-e8f73926f4f3</ControlTypeID> 
      <Name>QA Category</Name> 
      <Value>test</value> 
      <ControlDatas /> 
     </Metadata> 
     <Metadata> 
      <Control>8504787e-f502-4e7a-9313-3257df10b554</Control> 
      <ControlTypeID>5ae9a004-c0de-4692-a9bd-e8f73926f4f3</ControlTypeID> 
      <Name>AS240 Desc</Name> 
      <Value>240</value> 
      <ControlDatas /> 
     </Metadata> 
     <Metadata> 
      <Control>97927910-234c-4827-b7c7-3bcb21dc2a88</Control> 
      <ControlTypeID>5ae9a004-c0de-4692-a9bd-e8f73926f4f3</ControlTypeID> 
      <Name>Resolution</Name> 
      <Value>40</value> 
      <ControlDatas /> 
     </Metadata> 
     <Metadata> 
      <Control>e3d7a5c8-c8e0-4c44-97ff-4ea290df1c34</Control> 
      <ControlTypeID>5ae9a004-c0de-4692-a9bd-e8f73926f4f3</ControlTypeID> 
      <Name>Business Center</Name> 
      <Value>chicago</value> 
      <ControlDatas /> 
     </Metadata> 
     <Metadata> 
      <Control>1e3674ec-0b9f-4faa-9454-52c293b8d389</Control> 
      <ControlTypeID>f9966e48-3cf0-47f5-a602-0e47d6bc445c</ControlTypeID> 
      <Name>QA Spec Status</Name> 
      <Value>1</Value> 
      <ControlDatas /> 
     </Metadata> 
     <Metadata> 
      <Control>d96d3390-63de-414d-869a-9e0243ff4cbc</Control> 
      <ControlTypeID>5ae9a004-c0de-4692-a9bd-e8f73926f4f3</ControlTypeID> 
      <Name>QA Manager</Name> 
      <Value /> 
      <ControlDatas /> 
     </Metadata> 
     <Metadata> 
      <Control>a0fe9e68-c45c-49e2-8385-9f99f1d9e5fe</Control> 
      <ControlTypeID>5ae9a004-c0de-4692-a9bd-e8f73926f4f3</ControlTypeID> 
      <Name>Product Quality Manager</Name> 
      <Value /> 
      <ControlDatas /> 
     </Metadata> 
     <Metadata> 
      <Control>b72dd25e-ac2b-4eca-b59c-b2caec2c0e92</Control> 
      <ControlTypeID>5ae9a004-c0de-4692-a9bd-e8f73926f4f3</ControlTypeID> 
      <Name>GTIN</Name> 
      <Value /> 
      <ControlDatas /> 
     </Metadata> 
     <Metadata> 
      <Control>d9df10f0-a82f-4ba2-96f5-b71774a521c9</Control> 
      <ControlTypeID>c349aa0e-9c1d-406a-9eb9-b9ad5e561300</ControlTypeID> 
      <Name>Item Creation Date</Name> 
      <Value>27/04/2013 12:00:00 AM</Value> 
      <ControlDatas /> 
     </Metadata> 
     <Metadata> 
      <Control>dd6e875d-34b7-4b79-bb65-c3608f81f754</Control> 
      <ControlTypeID>f9966e48-3cf0-47f5-a602-0e47d6bc445c</ControlTypeID> 
      <Name>SUPC Status</Name> 
      <Value>active</Value> 
      <ControlDatas /> 
     </Metadata> 
     <Metadata> 
      <Control>a2682372-664b-4ee0-9a72-d69965c9be9f</Control> 
      <ControlTypeID>5ae9a004-c0de-4692-a9bd-e8f73926f4f3</ControlTypeID> 
      <Name>Qa Spec Desc</Name> 
      <Value /> 
      <ControlDatas /> 
     </Metadata> 
     <Metadata> 
      <Control>5c4f7fa4-f56b-4d91-a0dc-ead6f8115020</Control> 
      <ControlTypeID>5ae9a004-c0de-4692-a9bd-e8f73926f4f3</ControlTypeID> 
      <Name>Brand</Name> 
      <Value>Sysco</Value> 
      <ControlDatas /> 
     </Metadata> 
     <Metadata> 
      <Control>52fc415d-b844-472c-82d9-f205a9104825</Control> 
      <ControlTypeID>5ae9a004-c0de-4692-a9bd-e8f73926f4f3</ControlTypeID> 
      <Name>UPC</Name> 
      <Value /> 
      <ControlDatas /> 
     </Metadata> 
     </Metadatas> 
    </Item> 

我想對元數據標記中存儲在值標記中的元數據值進行類似搜索。我能夠成功通過做對

<Name>QA Category</Name> 
<Value>test</value> 

搜索上做字符串精確搜索有沒有辦法做這樣的價值標籤搜索?數據庫中可能有數百萬條產品記錄。

回答

0

query() - 用於從XML數據類型中提取XML片段。

value() - 用於從XML文檔中提取單個值。

exists() - 用於確定指定節點是否存在。如果是,則返回1;否則返回0。

modify() - 更新XML數據類型中的XML數據。

node() - 將XML數據分解爲多行(本博文未涉及)。

,你可以這樣做: Similar question

SELECT * 
FROM WebPageContent 
WHERE data.value('(/Item/Text)[1]', 'varchar(100)') LIKE 'XYZ%' 

OR

您可以使用類似:

where XMLField.exist('/Item/someitems/Name[.="QA Category"]')=1 

當你有很多的記錄,我會建議你創建該列上的xml索引以提高性能

這裏是很不錯的文章皮納爾對xml based searching ..

另一個good article