展平 XML 结构(SQL 服务器)
Flatten XML Structure (SQL Server)
我有以下 XML 通过 Web 服务传递给存储过程
<Company>
<Records>
<Person>
<ServicePostalCodes>
<PostalCode>80017</PostalCode>
<PostalCode>80018</PostalCode>
<PostalCode>80019</PostalCode>
<PostalCode>80020</PostalCode>
<PostalCode>80021</PostalCode>
<PostalCode>80022</PostalCode>
</ServicePostalCodes>
<Employee>
<EmployeeId>123456-789</EmployeeId>
<Street1>http</Street1>
<City>www</City>
<State>Whosebug</State>
<Zip>DotCom</Zip>
</Employee>
</Person>
<Person>
<ServicePostalCodes>
<PostalCode>90031</PostalCode>
<PostalCode>90032</PostalCode>
<PostalCode>90033</PostalCode>
<PostalCode>90034</PostalCode>
<PostalCode>90035</PostalCode>
<PostalCode>90036</PostalCode>
</ServicePostalCodes>
<Employee>
<EmployeeId>222222-789</EmployeeId>
<Street1>http</Street1>
<City>www</City>
<State>Whosebug</State>
<Zip>DotCom</Zip>
</Employee>
</Person>
</Records>
</Company>
我正在寻找一种方法将其 "flatten" 转换为 SQL 结果集,如下所示:
Person | ServicePostalCode
123456-789 | 80017
123456-789 | 80018
123456-789 | 80019
222222-789 | 90031
222222-789 | 90032
222222-789 | 90033
在此先感谢您的帮助
我相信:
SELECT
People.col.value('Employee[1]/EmployeeId[1]','nvarchar(100)') AS Person,
PostalCodes.col.value('(text())[1]','nvarchar(100)')
FROM
@xml.nodes('/Company/Records/Person') AS People(col) CROSS APPLY
col.nodes('ServicePostalCodes/PostalCode') AS PostalCodes(col)
假设您的 XML 在 @xml
中,将执行您想要的操作
基本上,它所做的是从每个 /Company/Records/Person 记录中创建一个 table,然后对每个记录执行 CROSS APPLY(类似于 JOIN,但执行 row-by-row)在相应的 ServicePostalCodes/PostalCode 记录中记录(例如它是 children)。
我有以下 XML 通过 Web 服务传递给存储过程
<Company>
<Records>
<Person>
<ServicePostalCodes>
<PostalCode>80017</PostalCode>
<PostalCode>80018</PostalCode>
<PostalCode>80019</PostalCode>
<PostalCode>80020</PostalCode>
<PostalCode>80021</PostalCode>
<PostalCode>80022</PostalCode>
</ServicePostalCodes>
<Employee>
<EmployeeId>123456-789</EmployeeId>
<Street1>http</Street1>
<City>www</City>
<State>Whosebug</State>
<Zip>DotCom</Zip>
</Employee>
</Person>
<Person>
<ServicePostalCodes>
<PostalCode>90031</PostalCode>
<PostalCode>90032</PostalCode>
<PostalCode>90033</PostalCode>
<PostalCode>90034</PostalCode>
<PostalCode>90035</PostalCode>
<PostalCode>90036</PostalCode>
</ServicePostalCodes>
<Employee>
<EmployeeId>222222-789</EmployeeId>
<Street1>http</Street1>
<City>www</City>
<State>Whosebug</State>
<Zip>DotCom</Zip>
</Employee>
</Person>
</Records>
</Company>
我正在寻找一种方法将其 "flatten" 转换为 SQL 结果集,如下所示:
Person | ServicePostalCode
123456-789 | 80017
123456-789 | 80018
123456-789 | 80019
222222-789 | 90031
222222-789 | 90032
222222-789 | 90033
在此先感谢您的帮助
我相信:
SELECT
People.col.value('Employee[1]/EmployeeId[1]','nvarchar(100)') AS Person,
PostalCodes.col.value('(text())[1]','nvarchar(100)')
FROM
@xml.nodes('/Company/Records/Person') AS People(col) CROSS APPLY
col.nodes('ServicePostalCodes/PostalCode') AS PostalCodes(col)
假设您的 XML 在 @xml
中,将执行您想要的操作基本上,它所做的是从每个 /Company/Records/Person 记录中创建一个 table,然后对每个记录执行 CROSS APPLY(类似于 JOIN,但执行 row-by-row)在相应的 ServicePostalCodes/PostalCode 记录中记录(例如它是 children)。