1

我有一个带有 XML 列的表,如下所示。

<option>
            <OptionName>Option 1</OptionName>
            <grant>
                            <GrantName>Grant 1</Name>
                            <schedules>
                                            <schedule>
                                                            <scheduleID></ID>
                                                            <scheduleName></Name>
                                                            <scheduleDate>1/1/2018</Date>
                                                            <scheduleAmount></Amount>
                                            </schedule>
                                            <schedule>
                                                            <scheduleID></ID>
                                                            <scheduleName></Name>
                                                            <scheduleDate>2/1/2018</Date>
                                                            <scheduleAmount></Amount>
                                            </schedule>
                                            <schedule>
                                                            <scheduleID></ID>
                                                            <scheduleName></Name>
                                                            <scheduleDate>3/1/2018</Date>
                                                            <scheduleAmount></Amount>
                                            </schedule>
                            </schedules>
            </grant>
            <grant>
                            <GrantName>Grant 2</Name>
                            <schedules>
                                            <schedule>
                                                            <scheduleID></ID>
                                                            <scheduleName></Name>
                                                            <scheduleDate>1/1/2019</Date>
                                                            <scheduleAmount></Amount>
                                            </schedule>
                                            <schedule>
                                                            <scheduleID></ID>
                                                            <scheduleName></Name>
                                                            <scheduleDate>2/1/2019</Date>
                                                            <scheduleAmount></Amount>
                                            </schedule>
                                            <schedule>
                                                            <scheduleID></ID>
                                                            <scheduleName></Name>
                                                            <scheduleDate>3/1/2019</Date>
                                                            <scheduleAmount></Amount>
                                            </schedule>
                            </schedules>
            </grant>

我想在表上运行查询以获取以下格式的数据。

OptionName | GrantName | ScheduleDate    

Option 1   | Grant 1   | 1/1/2018    
Option 1   | Grant 1   | 2/1/2018    
Option 1   | Grant 1   | 3/1/2018    
Option 1   | Grant 2   | 1/1/2019    
Option 1   | Grant 2   | 2/1/2019    
Option 1   | Grant 2   | 3/1/2019    

下面是我正在尝试的查询。但如果是子节点,它并没有给出所有的价值。因为我必须将索引指定为 [1]

select 
   FactChange.Fact.value('(Option/OptionName)[1]','varchar(max)') OptionName
   ,FactChange.Fact.value('(Option/Grant/GrantName)[1]', 'varchar(max)') grantName
from FactChange(nolock) 
4

1 回答 1

4

我在您的 XML 中进行了一些更改,主要是匹配有效的标签,例如开始标签名称必须有一个有效的结束标签名称。

我已将 xml 插入到表中(但这不是强制性的)见下文

DECLARE @tbl TABLE (XmlCol xml)
INSERT INTO @tbl VALUES 
('<option>
            <OptionName>Option 1</OptionName>
            <grant>
                    <GrantName>Grant 1</GrantName>
                            <schedules>
                                            <schedule>
                                                            <scheduleID></scheduleID>
                                                            <scheduleName></scheduleName>
                                                            <scheduleDate>1/1/2018</scheduleDate>
                                                            <scheduleAmount></scheduleAmount>
                                            </schedule>
                                            <schedule>
                                                            <scheduleID></scheduleID>
                                                            <scheduleName></scheduleName>
                                                            <scheduleDate>2/1/2018</scheduleDate>
                                                            <scheduleAmount></scheduleAmount>
                                            </schedule>
                                            <schedule>
                                                            <scheduleID></scheduleID>
                                                            <scheduleName></scheduleName>
                                                            <scheduleDate>3/1/2018</scheduleDate>
                                                            <scheduleAmount></scheduleAmount>
                                            </schedule>
                            </schedules>
            </grant>
            <grant>
                            <GrantName>Grant 2</GrantName>
                            <schedules>
                                            <schedule>
                                                            <scheduleID></scheduleID>
                                                            <scheduleName></scheduleName>
                                                            <scheduleDate>1/1/2019</scheduleDate>
                                                            <scheduleAmount></scheduleAmount>
                                            </schedule>
                                            <schedule>
                                                            <scheduleID></scheduleID>
                                                            <scheduleName></scheduleName>
                                                            <scheduleDate>2/1/2019</scheduleDate>
                                                            <scheduleAmount></scheduleAmount>
                                            </schedule>
                                            <schedule>
                                                            <scheduleID></scheduleID>
                                                            <scheduleName></scheduleName>
                                                            <scheduleDate>3/1/2019</scheduleDate>
                                                            <scheduleAmount></scheduleAmount>
                                            </schedule>
                            </schedules>
            </grant>
        </option>'
)



SELECT e.XmlCol.value('(/option/OptionName)[1]', 'varchar(100)'),
        t.x.value('../.././GrantName[1]','varchar(100)') GrantName,
       t.x.value('(./scheduleDate)[1]', 'varchar(100)') scheduleDate
 FROM @tbl e
    cross apply e.XmlCol.nodes ('/option/grant/schedules/schedule') t(x)
于 2018-05-17T10:44:54.577 回答