2012-04-06 11 views
1

ich die folgende SQL-Code haben:abrufen Feld von xml SQL Server 2005

declare @x xml 
set @x = 

'<ResultBlock> 
    <MatchSummary matches="1"><TotalMatchScore>900</TotalMatchScore> 
     <Rules totalRuleCount="9"> 
      <Rule ruleCount="1" isGlobal="1"> 
       <RuleID>MA_SWTEL_DEMP_DEADD</RuleID> 
       <Score>100</Score> 
      </Rule> 
      <Rule ruleCount="1" isGlobal="1"> 
       <RuleID>MA_MS</RuleID> 
       <Score>100</Score> 
      </Rule> 
      <Rule ruleCount="1" isGlobal="1"> 
       <RuleID>MA_PAS_MS</RuleID> 
       <Score>100</Score> 
      </Rule> 
      <Rule ruleCount="1" isGlobal="1"> 
       <RuleID>MA_CTEL_MS</RuleID> 
       <Score>100</Score> 
      </Rule> 
      <Rule ruleCount="1" isGlobal="1"> 
       <RuleID>MA_REF</RuleID> 
       <Score>100</Score> 
      </Rule> 
      <Rule ruleCount="1" isGlobal="1"> 
       <RuleID>MA_PAS_REF</RuleID> 
       <Score>100</Score> 
      </Rule> 
      <Rule ruleCount="1" isGlobal="1"> 
       <RuleID>MA_CTEL_REF</RuleID> 
       <Score>100</Score> 
      </Rule> 
      <Rule ruleCount="1" isGlobal="1"> 
       <RuleID>MA_MS_PER</RuleID> 
       <Score>100</Score> 
      </Rule> 
      <Rule ruleCount="1" isGlobal="1"> 
       <RuleID>MA_REF_PER</RuleID> 
       <Score>100</Score></Rule> 
     </Rules> 
     <MatchSchemes schemeCount="1"> 
      <Scheme> 
       <SchemeID>7</SchemeID> 
       <Score>900</Score> 
      </Scheme> 
     </MatchSchemes> 
    </MatchSummary> 
    <ErrorWarnings> 
     <Errors errorCount="0" /> 
     <Warnings warningCount="0" /> 
    </ErrorWarnings> 
</ResultBlock>' 

select x.value(N'RuleID', N'varchar(50)') as RuleID 
from @x.nodes(N'//RuleID') t(x) 

Ich brauche s alle RuleID‘retrive. Die folgende Abfrage generiert jedoch einen Fehler: XQuery [value()]: 'value()' requires a singleton (or empty sequence), found operand of type 'xdt:untypedAtomic *'. Was könnte falsch sein?

Antwort

2

Sie sind in der Nähe - Ihre XPath gibt eine Liste aller <RuleID> Knoten als XML-Fragmente - jetzt wollen Sie den aktuellen Elementwert extrahieren, so müssen Sie diese SQL XQuery verwenden, um dies zu erreichen:

select 
    x.value('.', 'varchar(50)') as RuleID 
from 
    @x.nodes('//RuleID') t(x) 

Die . sagt: geben Sie mir einfach den Inhalt des Elements - das ist, was Sie suchen, richtig?