If you need your output like this, you need to name your columns dynamically. This is not possible - unless you use dynamic SQL. Try this:
DECLARE @xml XML=
'<ReportData>
<ColumnName>SOLD_DATE</ColumnName>
<ColumnName>STORE_NUMBER</ColumnName>
<ColumnName>PHONE_NUMBER</ColumnName>
<ColumnName>FAX_NUMBER</ColumnName>
<Row>
<Col>03/31/2016</Col>
<Col>1234</Col>
<Col>(425) 673-7065</Col>
<Col>(425) 278-4974</Col>
</Row>
<Row>
<Col>05/05/2016</Col>
<Col>3456</Col>
<Col>(425) 555-7065</Col>
<Col>(425) 444-4974</Col>
</Row>
</ReportData>';
The only thing you can rely on, is the sort-order within the XML
WITH ColumnNames AS
(
SELECT ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS ColNr
,C.value('.','nvarchar(max)') AS Caption
FROM @xml.nodes('/ReportData/ColumnName') AS A(C)
)
,RowLines AS
(
SELECT ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS RowNr
,R.query('.') AS RowXML
FROM @xml.nodes('/ReportData/Row') AS A(R)
)
,RowValues AS
(
SELECT RowNr
,ROW_NUMBER() OVER(PARTITION BY RowNr ORDER BY (SELECT NULL)) AS ValNr
,C.value('.','nvarchar(max)') AS ColVal
FROM RowLines
CROSS APPLY RowXML.nodes('Row/Col') AS A(C)
)
SELECT *
INTO #tmpResult
FROM RowValues
INNER JOIN ColumnNames ON ColNr=ValNr
ORDER BY RowNr,ValNr;
The table #tmpResult
has now this content:
+-------+-------+----------------+-------+--------------+
| RowNr | ValNr | ColVal | ColNr | Caption |
+-------+-------+----------------+-------+--------------+
| 1 | 1 | 03/31/2016 | 1 | SOLD_DATE |
+-------+-------+----------------+-------+--------------+
| 1 | 2 | 1234 | 2 | STORE_NUMBER |
+-------+-------+----------------+-------+--------------+
| 1 | 3 | (425) 673-7065 | 3 | PHONE_NUMBER |
+-------+-------+----------------+-------+--------------+
| 1 | 4 | (425) 278-4974 | 4 | FAX_NUMBER |
+-------+-------+----------------+-------+--------------+
| 2 | 1 | 05/05/2016 | 1 | SOLD_DATE |
+-------+-------+----------------+-------+--------------+
| 2 | 2 | 3456 | 2 | STORE_NUMBER |
+-------+-------+----------------+-------+--------------+
| 2 | 3 | (425) 555-7065 | 3 | PHONE_NUMBER |
+-------+-------+----------------+-------+--------------+
| 2 | 4 | (425) 444-4974 | 4 | FAX_NUMBER |
+-------+-------+----------------+-------+--------------+
Now we need a dynamically created PIVOT
statement:
DECLARE @colNames NVARCHAR(MAX)=
(
STUFF(
(
SELECT DISTINCT ',' + Caption + ''
FROM #tmpResult
FOR XML PATH('')
),1,1,''
)
);
DECLARE @cmd NVARCHAR(MAX)=
'SELECT p.*
FROM
(
SELECT RowNr,ColVal,Caption FROM #tmpResult
) AS tbl
PIVOT
(
MAX(ColVal) FOR Caption IN(' + @colNames + ')
) AS p;';
EXEC(@cmd);
And this is the result:
+-------+----------------+----------------+------------+--------------+
| RowNr | FAX_NUMBER | PHONE_NUMBER | SOLD_DATE | STORE_NUMBER |
+-------+----------------+----------------+------------+--------------+
| 1 | (425) 278-4974 | (425) 673-7065 | 03/31/2016 | 1234 |
+-------+----------------+----------------+------------+--------------+
| 2 | (425) 444-4974 | (425) 555-7065 | 05/05/2016 | 3456 |
+-------+----------------+----------------+------------+--------------+
Clean up
DROP TABLE #tmpResult;