Predefined column names in SQL Server pivot table
Posted
by
Marcos Buarque
on Stack Overflow
See other posts from Stack Overflow
or by Marcos Buarque
Published on 2011-01-16T04:46:01Z
Indexed on
2011/01/16
4:53 UTC
Read the original article
Hit count: 310
Hi, the other day I opened a topic here in StackOverflow (stackoverflow.com/questions/4663698/how-can-i-display-a-consolidated-version-of-my-sql-server-table). At that time I needed help on how to show data on a pivot table. From the help I got here in the forum, my research led me to this page about dynamic SQL: www.sommarskog.se/dynamic_sql.html. And then it led me to this awesome SQL script by Itzik Ben-Gan that will create a stored procedure that outputs a pivot table exactly the way I want: sommarskog.se/pivot_sp.sp.
Well, almost. I need one change in this stored procedure. Instead of having dynamic column names pulled from the @on_cols variable in the SPROC, I need the output table to hold generic column names in simple ASC order. Could be, for example, col1, col2, col3, col4 ... The dynamic column names are a problem for me. So I need them named by their index in the order they appear. I have tried all sorts of things changing this great SQL script, but it won't work.
I did not paste the code from the author because it is too long, but the link above will get us there.
Any help appreciated. Thank you very much
© Stack Overflow or respective owner