1

我有以下 sql 代码:

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)

我需要检索所有RuleID的。但是以下查询会产生错误: XQuery [value()]: 'value()' requires a singleton (or empty sequence), found operand of type 'xdt:untypedAtomic *'. 可能有什么问题?

4

1 回答 1

2

你已经接近了——你的 XPath 将所有<RuleID>节点的列表作为 XML 片段返回——现在你想提取实际的元素值,所以你需要使用这个 SQL XQuery 来实现这一点:

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

.说:只要给我元素的内容-这就是你要找的,对吧?

于 2012-04-06T08:11:47.523 回答