MSSQL 2005 Dynamic PIVOT

Here’s a method of generating a dynamic pivot of a dataset. It first generates the columns as a string (“[ColumnHeader0],[ColumnHeader1],[ColumnHeader2], …” format), builds a dynamic SQL string and executes it using the PIVOT expression. From the code below, the @cols variable is generated by using the FOR XML PATH expression. …