通过 OPENQUERY 和 OPENROWSET 获取的结果集中不允许有重复的列名

发布于 2024-08-17 07:49:41 字数 1260 浏览 9 评论 0原文

如何在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

酸甜透明夹心 2024-08-24 07:49:41

我认为如果不使用 OPENXML 作为临时行集,这是不可能的:

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

;WITH xmlCTE
AS
(
        SELECT *
        FROM OPENXML(@hDoc, '/reportResponse/reportDataRow/columnData',2)
        WITH (DATA VARCHAR(50) 
             ,rowNum INT       '../@rowNum'
             ,colNum INT       '@colNum')
)
SELECT c1.DATA AS columnData1
       ,c2.DATA AS columnData2
FROM xmlCTE AS c1
JOIN xmlCTE AS c2
ON   c1.rowNum = c2.rowNum
AND  c2.colNum = 2
WHERE c1.colNum = 1

EXEC sp_xml_removedocument @hDoc

I don't think this is possible without using OPENXML as an interim rowset:

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

;WITH xmlCTE
AS
(
        SELECT *
        FROM OPENXML(@hDoc, '/reportResponse/reportDataRow/columnData',2)
        WITH (DATA VARCHAR(50) 
             ,rowNum INT       '../@rowNum'
             ,colNum INT       '@colNum')
)
SELECT c1.DATA AS columnData1
       ,c2.DATA AS columnData2
FROM xmlCTE AS c1
JOIN xmlCTE AS c2
ON   c1.rowNum = c2.rowNum
AND  c2.colNum = 2
WHERE c1.colNum = 1

EXEC sp_xml_removedocument @hDoc
熊抱啵儿 2024-08-24 07:49:41

谢谢。我通过使用 PIVOT 找到了答案。在我的实际情况中,我有超过 10 列和数千行,因此自连接并不容易。顺便说一句,这是为了解析来自 PayPal reportEngineResponse 的 xml 数据集。下面是分享的答案。

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 [1] as row1, [2] as row2
FROM
(SELECT *
FROM OPENXML(@hDoc, '/reportResponse/reportDataRow/columnData',2)
WITH (data VARCHAR(50) 
 ,rowNum INT       '../@rowNum'
 ,colNum INT       '@colNum')) P
PIVOT
(
MAX(data)
FOR colNum IN ([1],[2]) 
)AS pvt

EXEC sp_xml_removedocument @hDoc

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.

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 [1] as row1, [2] as row2
FROM
(SELECT *
FROM OPENXML(@hDoc, '/reportResponse/reportDataRow/columnData',2)
WITH (data VARCHAR(50) 
 ,rowNum INT       '../@rowNum'
 ,colNum INT       '@colNum')) P
PIVOT
(
MAX(data)
FOR colNum IN ([1],[2]) 
)AS pvt

EXEC sp_xml_removedocument @hDoc
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文