ilu
ilu

Reputation: 31

SQL, Renaming column names in select statement without known the original name

I'm new to sql and have a special demand: I have many sql queries (written by others), and I know how many columns these sqls will generate. For example, here is one of the records

select * from some_table

and I known it will generate a table contain 2 columns but I don't known the column names. I want to know if there is a way that performs like this

select [1] as 'Column1', [2] as 'Column2' from some_table

where "[1] as 'Column1'" means the first column in the query result rename to 'Column1', and "[2] as 'Column2'" means the second column rename to 'Column2'.

Update:

I solved the problem using 'UNION ALL', for example

select 'c1' as 'Column1', 'c2' as 'Column2' limit 0
union all
select * from some_table

and 'limit 0' is to remove the row ['c1', 'c2'] from the first query . It will generate table like this

| Column1  | Column2 |
----------------------
| xxxx     | yyyyy   |

Seems works well until now, I will continue verify the correctness.

The solution comes from another question, but I failed to recall the link of that question.

Upvotes: 1

Views: 1938

Answers (1)

p_efros
p_efros

Reputation: 301

Looks like there are already similar questions like this one. According to the response, this is not possible in SQL.

Upvotes: 1

Related Questions