2012-04-06 15 views
1

を使用してXMLからフィールドを取得する私は、次のSQLコードを持っている:私はすべてのRuleID年代をretriveする必要がは、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) 

を。しかし、次のクエリはエラーを生成します: XQuery [value()]: 'value()' requires a singleton (or empty sequence), found operand of type 'xdt:untypedAtomic *'。何が間違っているのでしょうか?

答えて

2

あなたは近いです - あなたのXPathは、XML断片として、すべての<RuleID>ノードのリストを返します - 今、あなたは、実際の要素の値を抽出したいので、あなたはこれを達成するために、このSQL XQueryを使用する必要があります。

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

.さんの言葉:私は要素の内容を教えてください - それはあなたが探しているものですよね?