1

在 SQL Server 2005 中,我试图将下面的 XML 解析为以下格式的详细信息,但我无法做到。感谢任何帮助。我想要我的选择输出

1 TagSold Tag        0   101 2
2 TagReg  Customer   101 0   4
3 TagAdj  Tag        0   105 2



<TagDistributor>
<stbLedgerEntryTypes>
    <stbLedgerEntryType type="TagSold">
        <vcMapping>Tag</vcMapping>
        <tiCustTrxnTypeID>0</tiCustTrxnTypeID>
        <tiTagTrxnTypeID>101</tiTagTrxnTypeID>
        <tiPaymentTypeID>2</tiPaymentTypeID>
    </stbLedgerEntryType>
    <stbLedgerEntryType type="TagReg">
        <vcMapping>Customer</vcMapping>
        <tiCustTrxnTypeID>101</tiCustTrxnTypeID>
        <tiTagTrxnTypeID>0</tiTagTrxnTypeID>
        <tiPaymentTypeID>4</tiPaymentTypeID>
    </stbLedgerEntryType>
    <stbLedgerEntryType type="TagAdj">
        <vcMapping>Tag</vcMapping>
        <tiCustTrxnTypeID>0</tiCustTrxnTypeID>
        <tiTagTrxnTypeID>105</tiTagTrxnTypeID>
        <tiPaymentTypeID>2</tiPaymentTypeID>
    </stbLedgerEntryType>
</stbLedgerEntryTypes></TagDistributor>

我尝试了以下操作。我得到了输出。但在第一列中,我没有得到区分的属性值。我对第一行有疑问。

SELECT 
         a.b.query('.').value('@type', 'varchar(128)'),
         a.b.query('vcMapping').value('.', 'varchar(128)'),
         a.b.query('tiCustTrxnTypeID').value('.', 'int'),
         a.b.query('tiTagTrxnTypeID').value('.', 'int'),
         a.b.query('tiPaymentTypeID').value('.', 'int')
FROM    @ipv_xmlDistributorInfo.nodes('TagDistributor/stbLedgerEntryTypes/stbLedgerEntryType') a(b)
4

1 回答 1

1

像这样的东西应该工作:

SELECT 
   x.value('(@type)[1]', 'varchar(100)') AS 'Type',
   x.value('(vcMapping)[1]', 'varchar(100)') AS 'vcMapping',
   x.value('(tiCustTrxnTypeID)[1]', 'int') AS 'tiCustTrxnTypeID',
   x.value('(tiTagTrxnTypeID)[1]', 'int') AS 'tiTagTrxnTypeID',
   x.value('(tiPaymentTypeID)[1]', 'int') AS 'tiPaymentTypeID'
FROM XMLTable x
CROSS APPLY x.myXMLField.nodes('/TagDistributor/stbLedgerEntryTypes/stbLedgerEntryType') 
  n(x)

这是SQL Fiddle

祝你好运。

于 2013-02-03T06:40:22.817 回答