简体   繁体   中英

TSQL - Query XML

Maybe some can help me, i have got an sql table with nRows with an XML-Column. The XML structure looks like this:

<DynamicResults>
  <RegQuery xmlns:xsi="http://www.w3.o....." xmlns:xsd="http://....." REGServer="localhost" REGHive="HKEY_LOCAL_MACHINE" REGSubKey="SOFTWARE\Microsoft\Windows\BlaBla" />
</DynamicResults>

my question: how can i query into the xml, i'd like to know if an /DynamicResults/RegQuery with an specified value in the attribute 'REGSubKey' has subnodes...

thanks a lot

The way I normally do this is to use the built-in XML functions (if you're using SQL Server 2005 or later).

Check out this MSDN page: http://msdn.microsoft.com/en-us/library/ms178030

Here is an example which returns the number of sub nodes for the specific reg key SOFTWARE\\Microsoft\\Windows\\BlaBla ...

DECLARE @x xml 
SET @x='<DynamicResults>
  <RegQuery xmlns:xsi="http://www.w3.o....." xmlns:xsd="http://....." REGServer="localhost" REGHive="HKEY_LOCAL_MACHINE" REGSubKey="SOFTWARE\Microsoft\Windows\BlaBla">
    <SubNode />
    <SubNode />
  </RegQuery>
  <RegQuery xmlns:xsi="http://www.w3.o....." xmlns:xsd="http://....." REGServer="localhost" REGHive="HKEY_LOCAL_MACHINE" REGSubKey="SOFTWARE\Microsoft\Windows\BlaBla2" />
  <RegQuery xmlns:xsi="http://www.w3.o....." xmlns:xsd="http://....." REGServer="localhost" REGHive="HKEY_LOCAL_MACHINE" REGSubKey="SOFTWARE\Microsoft\Windows\BlaBla3" />
</DynamicResults>'
SELECT @x.value('count(/DynamicResults/RegQuery[@REGSubKey="SOFTWARE\Microsoft\Windows\BlaBla"]/*)','INT')
GO
select *
from table
where XmlField.value('count(/DynamicResults/RegQuery[@REGSubKey="SOFTWARE\Microsoft\Windows\BlaBla"]/*)','int')>0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM