2010-01-08 23 views
0

如何使用MSSQL 2005中的OPENXML獲得具有相同列名的第二列?通過OPENQUERY和OPENROWSET獲取的結果集中不允許使用重複的列名

這是我計劃得到的結果集。

columnData1 columnData2

A B

C^d

電子網

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 

回答

0

謝謝。我通過使用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 
0

我不認爲這是可能的,而無需使用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 
相關問題