【发布时间】:2020-10-09 12:19:26
【问题描述】:
我是 XML 新手,我被要求编写一个 T-SQL 查询,该查询将使用 XML 文件并将其转换为带有 SQL Server 的表/视图。虽然实际的 XML 是客户端数据,但我尝试将 XML 文件简化为以下示例。
我认为这个想法类似于子查询,但对于 XML,我将仅在一行中显示每个案例的所有步骤。
<testcases>
<testcase>
<testcasename> first </testcasename>
<MediaType> Voice </MediaType>
<steps>
<CallSteps>
<Step>
<StepNo>1</StepNo>
<description> first step </description>
<StepNo>2</StepNo>
<description> second step </description>
</Step>
</CallSteps>
</steps>
</testcase>
<testcase>
<testcasename> second </testcasename>
<MediaType> Chat </MediaType>
<steps>
<CallSteps>
<Step>
<StepNo>1</StepNo>
<description> first step </description>
<StepNo>2</StepNo>
<description> second step </description>
<StepNo>3</StepNo>
<description> Third step </description>
</Step>
</CallSteps>
</steps>
</testcase>
<testcase>
<testcasename> Third </testcasename>
<MediaType> Voice </MediaType>
<steps>
<CallSteps>
<Step>
<StepNo>1</StepNo>
<description> first step </description>
</Step>
</CallSteps>
</steps>
</testcase>
</testcases>
我想显示在下表中
步数可以尽可能多(~100)
到目前为止,我已经来到这里-
select @xml
declare @idoc int
EXEC sp_xml_preparedocument @idoc OUTPUT, @xml;
select * from openxml(@idoc, '/testcases/testcase',2)
with (
testcasename nvarchar(10),
MediaType nvarchar(10),
steps xml
)
下面的输出
请帮忙
【问题讨论】:
-
不要使用那种真的老方法。 XQUERY 至少从 SQL Server 2008 年(甚至可能是 2005 年)就已经存在。
-
XQuery 从 SQL Server 2005 开始可用
标签: sql-server xml tsql