通过 OPENQUERY 和 OPENROWSET 获取的结果集中不允许有重复的列名
如何在 MSSQL 2005 中使用 OPENXML 获取具有相同列名的第二列?
这是我计划获得的结果集。
列数据 1 列数据 2
A B
C D
E F
DECLARE @hDoc int, @xmldata varchar(max)
SELECT @xmldata =
'<?xml version="1.0" encoding="utf-8" ?>
<reportResponse>
<reportDataRow rowNum="1">
<columnData colNum="1">
<data>A</data>
</columnData>
<columnData colNum="2">
<data>B</data>
</columnData>
</reportDataRow>
<reportDataRow rowNum="2">
<columnData colNum="1">
<data>C</data>
</columnData>
<columnData colNum="2">
<data>D</data>
</columnData>
</reportDataRow>
<reportDataRow rowNum="3">
<columnData colNum="1">
<data>E</data>
</columnData>
<columnData colNum="2">
<data>F</data>
</columnData>
</reportDataRow>
</reportResponse>'
EXEC sp_xml_preparedocument @hDoc OUTPUT, @xmldata
SELECT *
FROM OPENXML(@hDoc, '/reportResponse/reportDataRow',2)
WITH (columnData varchar(50) , columnData2 varchar(50) )
SELECT *
FROM OPENXML(@hDoc, '/reportResponse/reportDataRow/columnData',2)
WITH (data varchar(50))
EXEC sp_xml_removedocument @hDoc
How can I get the second column with the same column name using OPENXML in MSSQL 2005?
Here is the result set that I plan to get.
columnData1 columnData2
A B
C D
E F
DECLARE @hDoc int, @xmldata varchar(max)
SELECT @xmldata =
'<?xml version="1.0" encoding="utf-8" ?>
<reportResponse>
<reportDataRow rowNum="1">
<columnData colNum="1">
<data>A</data>
</columnData>
<columnData colNum="2">
<data>B</data>
</columnData>
</reportDataRow>
<reportDataRow rowNum="2">
<columnData colNum="1">
<data>C</data>
</columnData>
<columnData colNum="2">
<data>D</data>
</columnData>
</reportDataRow>
<reportDataRow rowNum="3">
<columnData colNum="1">
<data>E</data>
</columnData>
<columnData colNum="2">
<data>F</data>
</columnData>
</reportDataRow>
</reportResponse>'
EXEC sp_xml_preparedocument @hDoc OUTPUT, @xmldata
SELECT *
FROM OPENXML(@hDoc, '/reportResponse/reportDataRow',2)
WITH (columnData varchar(50) , columnData2 varchar(50) )
SELECT *
FROM OPENXML(@hDoc, '/reportResponse/reportDataRow/columnData',2)
WITH (data varchar(50))
EXEC sp_xml_removedocument @hDoc
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为如果不使用 OPENXML 作为临时行集,这是不可能的:
I don't think this is possible without using
OPENXML
as an interim rowset:谢谢。我通过使用 PIVOT 找到了答案。在我的实际情况中,我有超过 10 列和数千行,因此自连接并不容易。顺便说一句,这是为了解析来自 PayPal reportEngineResponse 的 xml 数据集。下面是分享的答案。
Thanks. I found the answer by using PIVOT. In my real case, I have more than 10 columns and thousands of rows, so it won't be easy to self join. BTW, this is for parsing xml data set from PayPal reportEngineResponse. Here is the answer to share.