I am trying to insert dynamically generated column headers into a pivot statement without using stored procs due to how output is used with BI case tool. The first pivot statement below works but is hard-coded, the second uses sql code to generate the same output verbatim as a single text field but results in this error:
Msg 156, Level 15, State 1, Line 53 Incorrect syntax near the keyword 'SELECT'.
Msg 102, Level 15, State 1, Line 56 Incorrect syntax near ')'.
have searched in vain for awhile, any help greatly appreciated.
--Hard Coded Column Headers:
PIVOT (MAX(Result) FOR ColHdr IN (
[Column A], [Column B], [Column C], [Column D]
)) AS P
--Dynamically Generated Column Headers
PIVOT (MAX(Result) FOR ColHdr IN (
SELECT TOP 1 substring( (SELECT ', [' + [FriendlyName] + ']' FROM [dbo].
[TestResultSet] d FOR XML PATH ('')), 2, 1000) AS [ColHdrList] FROM [dbo].
[TestResultSet]
)) AS P