简体   繁体   English

如何从 SQL 服务器中的 XML 列中提取值

[英]How do I extract a value from an XML column in SQL Server

I have a column SCORERESULTS in table CSE_ARCHIVEDCREDITSCORE.我在表 CSE_ARCHIVEDCREDITSCORE 中有一个列 SCORERESULTS。

Score results is an XML column.评分结果是 XML 列。

<ScoringEngine>
  <Profile id="Navigation" version="1" num="4" uniqueId="8bcf8a8b9efc4e5dad1d87510cfe6a64">
.
.
.
    <Tool id="Payment To Income Ratio" version="1" old_id="Pmt_To_Income">
      <Rule id="PaymentIncomeRatio" version="1" old_id="PTI">
        <Row uniqueId="0fb11598c4224e4c97cf2afcc4e34b54" order="6" id="0">
          <Column order="1" op="RNG2" start="0.18" end="0.2" title="Payment To Income Ratio">0.190325139</Column>
          <Action name="Record Value" value="1.42085235920852" fieldName="LO_R_PMT_TO_INCOME" />
        </Row>
      </Rule>
      <RecordedValue>
        <Value value="1.42085235920852" fieldName="LO_R_PMT_TO_INCOME" />
      </RecordedValue>
    </Tool>
    <Tool id="RecentLoans" version="2">
      <Rule id="RecentLoans" version="2" old_id="RecentLoans" />
    </Tool>
.
.
.
  </Profile>
</ScoringEngine>

I am trying to get the value 0.190325139 out of the XML line: <Column order="1" op="RNG2" start="0.18" end="0.2" title="Payment To Income Ratio">0.190325139</Column> I am clueless on how to pull it.我试图从 XML 行中获取值 0.190325139: <Column order="1" op="RNG2" start="0.18" end="0.2" title="Payment To Income Ratio">0.190325139</Column>我对如何拉它一无所知。 I'm not familiar with XML, or really how to navigate it well.我不熟悉 XML,或者真的不熟悉如何很好地导航它。

You can use the below query.您可以使用以下查询。 Make sure the column SCORERESULTS is of type XML.确保 SCORERESULTS 列的类型为 XML。

SELECT SCORERESULTS.value('(ScoringEngine/Profile/Tool/Rule/Row/Column)[1]', 'nvarchar(MAX)') AS result
FROM CSE_ARCHIVEDCREDITSCORE

If the column is not of type XML use the below query如果该列不是 XML 类型,请使用以下查询

SELECT CAST(SCORERESULTS AS XML).value('(ScoringEngine/Profile/Tool/Rule/Row/Column)[1]', 'nvarchar(MAX)') AS result
FROM CSE_ARCHIVEDCREDITSCORE

You can try the following approach.您可以尝试以下方法。

XPath predicate is filtering out not needed <Tool> elements. XPath 谓词正在过滤掉不需要的<Tool>元素。

SQL SQL

-- DDL and sample data population, start
DECLARE @tbl TABLE (ID INT IDENTITY PRIMARY KEY, xmldata XML);
INSERT INTO @tbl (xmldata)
VALUES
(N'<ScoringEngine>
    <Profile id="Navigation" version="1" num="4"
             uniqueId="8bcf8a8b9efc4e5dad1d87510cfe6a64">
        <Tool id="Payment To Income Ratio" version="1" old_id="Pmt_To_Income">
            <Rule id="PaymentIncomeRatio" version="1" old_id="PTI">
                <Row uniqueId="0fb11598c4224e4c97cf2afcc4e34b54" order="6"
                     id="0">
                    <Column order="1" op="RNG2" start="0.18" end="0.2"
                            title="Payment To Income Ratio">0.190325139</Column>
                    <Action name="Record Value" value="1.42085235920852"
                            fieldName="LO_R_PMT_TO_INCOME"/>
                </Row>
            </Rule>
            <RecordedValue>
                <Value value="1.42085235920852" fieldName="LO_R_PMT_TO_INCOME"/>
            </RecordedValue>
        </Tool>
        <Tool id="RecentLoans" version="2">
            <Rule id="RecentLoans" version="2" old_id="RecentLoans"/>
        </Tool>
    </Profile>
</ScoringEngine>');
-- DDL and sample data population, end

SELECT tbl.ID
    , c.value('(Rule/Row/Column/text())[1]','DECIMAL(10,8)') AS [value]
FROM @tbl AS tbl
    CROSS APPLY xmldata.nodes('/ScoringEngine/Profile/Tool[@id="Payment To Income Ratio"]') AS t(c);

Output Output

+----+------------+
| ID |   value    |
+----+------------+
|  1 | 0.19032514 |
+----+------------+

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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