I have a table under SQL Server 2005 with two columns (col1, col2) and this table have about 10 records, so the table look like this:
COL1 COL2
=============
val1 val_1
val2 val_2
...
valN val_N
what's the best way to swap rows for cols to obtain some like this in a view:
val1 val2 ... valN
====================
val_1 val_2 ... val_N,
where the values of COL1 becomes the column names of the result, and the values of COL2 the values for each new column.
SELECT * FROM <table>
PIVOT (
max(COL2)
FOR COL1 IN([COL1], [COL2], ..., [COLN])
) AS PVT
But, what about if i have an unknown number of columns, i mean, i have "about" 10 records (that becomes the columns), how can i determine the IN(<cols>) sintax?