I would like to rename all column names of a table given a string with new names separated by comma. Here be the string:
declare @str varchar(max)='A,B,C'
The number of columns in a table may vary, and appropriately the number of names in a string. So in a 5-column table the string would be 'A,B,C,D,E'
.
I have a table with column names as:
col1 | col2 | col3
and in expected results I would like to have:
A | B | C
Update I have tried to follow that path:
SELECT
ORDINAL_POSITION
,COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE
TABLE_NAME = 'my_temp'
order by ORDINAL_POSITION asc
but I do not know how to split @sql string so that it can be applied to results in the following way:
ORDINAL_POSITION COLUMN_NAME sql_string
1 Col1 A
2 Col2 B
3 Col3 C
Then I could easily create a string like:
dynamic_sql='EXEC sp_rename my_table.' + COLUMN_NAME +',' + sql_string +', ''COLUMN'''