Skip to Main Content
  • Questions
  • Is it possible to create a column alias from a table value?

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Patrick.

Asked: October 30, 2015 - 1:31 pm UTC

Last updated: October 30, 2015 - 5:31 pm UTC

Version: 10g

Viewed 10K+ times! This question is

You Asked

Tom, is it possible to create a column alias from a table value?

For example:

Table A below has COL_ID's that don't mean anything, but rather the COL_DESCRIPTIONS do.

COL_ID COL_DESCRIPTION
------ ---------------------------
BD1423 CUSTOMER_FIRST_NAME
BD9721 CUSTONER_LAST_NAME

Table B

BD1423 BD9721
------ ----------------------------
JOHN SMITH
BOB JONES

Table A is set up just to match the COL_ID with the column "name" in Table B in order to get the value of COL_DESCRIPTION to be
used as a column alias.

For Example:

COLUMN ALIAS
(is something like this possible?)
|
|
V
|--------------------------------------------------------|
select B.BD1423 (select A.COL_DESCRIPTION from A where A.COL_ID = "BD1423"), <-- I don't want to hard code to "BD1423" or "BD9721",
B.BD9721 (select A.COL_DESCRIPTION from A where A.COL_ID = "BD9721") do you know what I'm getting at?
from B

Results:

CUSTOMER_FIRST_NAME CUSTOMER_LAST_NAME
------------------- ------------------
JOHN SMITH
BOB JONES

Thanks for any help Tom!






and Chris said...

Yuck. Seriously, yuck.

What's wrong with proper column names?

You can't generate aliases from subqueries. They have to be strings.

The easiest way to do this is with dynamic SQL. Find the names from your lookup table. Then create a string of your SQL statement accordingly:

create table cols (
  col_id varchar2(10),
  col_desc varchar2(30)
);

create table t (
  BD1423 varchar2(10),
  BD9721 varchar2(10)
);

insert into cols values ('BD1423', 'FIRST_NAME');
insert into cols values ('BD9721', 'LAST_NAME');

insert into t values ('John', 'Smith');
insert into t values ('Bob', 'Jones');

commit;

set serveroutput on
var c refcursor;

declare
  s varchar2(100);
  type arr is table of varchar2(30) index by varchar2(30);
  col_names arr;
begin
  for cur in (select * from cols) loop
    col_names(cur.col_id) := cur.col_desc;
  end loop;
  
  s := 'select BD1423 as ' || col_names('BD1423') || ', BD9721 as ' || col_names('BD9721') || ' from t';
  
  dbms_output.put_line(s);  
  
  open :c for s;
end;
/

select BD1423 as FIRST_NAME, BD9721 as LAST_NAME from t

print :c;

C
-
FIRST_NAME LAST_NAME  
---------- ---------- 
John       Smith      
Bob        Jones    


But really, save yourself a whole heap of pain and rename the columns in the actual table!

Rating

  (1 rating)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Data Migration

Patrick, October 30, 2015 - 5:27 pm UTC

Chris, this is a data migration issue. Tables are being pulled over from MainFrame COBOL environment where segments/elements (tables/columns) and put into an Oracle database as same. The segment/element descriptions are part of the DDL script only and not stored as tables/column names in Oracle. So I have to manually look at the DDL script to know what I'm actually querying. Yes, yuck and yuck. Thanks for your quick response!
Connor McDonald
October 30, 2015 - 5:31 pm UTC

You have my sympathies! Good luck.

More to Explore

DBMS_OUTPUT

More on PL/SQL routine DBMS_OUTPUT here