Given a table "ABC" with columns Col1, Col2 and Col3 it is possible to automatically generate something like the following:
SELECT
Col1 AS 'ABC_Col1',
Col2 AS 'ABC_Col2',
Col3 AS 'ABC_Col3'
FROM ABC
I have a table without a fixed set of columns (users are able to append their own columns) where I still need the column prefix (because it is needed in a JOIN/CTE with other tables that also have columns with the names Col1, Col2 etc…)
Therefore I would like to be able to write something like this:
SELECT
T0.* AS 'ABC_T.*',
FROM ABC T0
Which is of course not valid SQL, but can it be done somehow so the "*" columns all get the same prefix?
Best Answer
This will give you a map of old column names and new column names:
From there it's just some dynamic sql. I'm still playing with it.
EDIT
OK, I ditched that.
A) this is terrible performance (because it's a cursor)
B) I know you're not meant to do work for people on here, but I got carried away.
C) I considered not even posting this because of how poor of an answer I feel it is, but it's a least an idea.