Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle rename columns from select automatically?

I have 2 tables with the following fields.

Table1

  • AA
  • BB
  • CC
  • DD

Table2

  • AA
  • CC
  • EE

Query

Select t1.*, 
       t2.*
  from table1 t1,
  join table2 t2 on table1.DD = table2.EE

My data columns back with the following column names:

AA, BB, CC, DD, **AA_1**, **CC_1**, EE

I don't want the column names like that. I want them to have the table name prefixed in the names of common (or all columns). I could fix this with:

select t1.AA as t1_AA, t1.BB as t1_BB, t1.CC as t1_CC, t1.DD as t1_DD, 
 t2.AA as t2_AA, t2.CC as t2_CC, t2.EE as t2_EEE
   from table1 t1,
    inner join table2 t2
    on table1.DD = table2.EE

But that means every select everywhere becomes 500 lines longer. Is there a magic way to do this in oracle? Basically I want to write my code like

 select t1.* as t1_*, t2.* as t2_*
       from table1 t1,
        inner join table2 t2
        on table1.DD = table2.EE

But of course that is not valid SQL

like image 206
bwawok Avatar asked Dec 28 '22 06:12

bwawok


1 Answers

In Oracle SELECT syntax, there is currently no way to assign column aliases to multiple columns based on some expression. You have to assign an alias to each individual column.

like image 175
vls Avatar answered Jan 06 '23 06:01

vls