Nothing against the CLR method but since it just builds dynamic SQL and you have to provide almost everything anyway, why not just build a function in T-SQL that builds the dynamic SQL?
Shifting gears a bit, I wish MS would get off their duff's and make a truly useful and powerful PIVOT function in T-SQL like that found in MS Access.
--Jeff Moden
Change is inevitable... Change for the better is not.