I have a table with over 400 columns, and these are named with our vendor’s archaic naming system. I need to move this data into a new table which uses our company’s naming conventions, so I have to change the names of these 400 columns.
Fortunately, I also have a table that cross-references the current column names with what they should become, like so:
Acronym | Name
----------------
A | ColumnNameA
B | ColumnNameB
C | ColumnNameC
etc…
So my question is this:
If it were only a few rows, I could easily do
SELECT
A AS ColumnNameA,
B AS ColumnNameB
FROM
Table
But there are just too many columns to do this by hand. What’s the best way to dynamically change column names in a SELECT statement based off of a cross-ref table?
My effort so far:
I was thinking something along the lines of
SET @sqlCommand = 'SELECT ' + @columns + ' FROM Table'
EXEC (@sqlCommand)
but I have no idea how to set @columns to be a dynamically generated list of all the acronyms as the final column names. Is this even a viable approach?
1 Answer