1

I just want to know if it is possible to assign dynamic names to a column. for instance:

SQL> select sysdate+1 from dual;
SYSDATE+1
---------
09-JUL-08

the column name is SYSDATE+1. can I do something like:

SQL> select 1 as (select sysdate from dual) from dual;
(this throws error)

The reason for this requirement is that I need to assign a column name according to a value that comes from a parameter. So, instead of returning something like:

SQL> select sysdate, sysdate+1 from dual;
SYSDATE   SYSDATE+1

--------- ---------

08-JUL-08 09-JUL-08

I need to find a way to return something like:

SQL> select sysdate as <<dynamic column name based on parameter that has been passed in>>, sysdate+1 AS <<dynamic column name based on parameter that has been passed in>> from dual;

Obviously I can use something like:

select sysdate as "08-JUL-08", sysdate+1 AS "09-JUL-08" from dual;

But in this case I'm hard coding it. I need this alias to be dynamic.

Any way to do it?

  • 5
    The only way is to use dynamic SQL. I cannot imagine any scenario where doing this would be a good solution, so I suggest going back to the requirements first and figuring out a better way, a way that doesn't have this (rather ridiculous) constraint. – user5151179 Oct 30 '15 at 06:21
  • In "select sysdate as <>", <> can't be a valid alias because <> is a predefined not equals operator. An alias has to be a proper identifier. Similarly, in "select 1 as (select .." the intent is not clear to me. A dynamic alias can be used with dynamic sql, but alias names have to follow the rules of identifiers. – ramana_k Oct 30 '15 at 06:23
  • 1
    It seems to me from this query of yours `select sysdate as "08-JUL-08", sysdate+1 AS "09-JUL-08"` that you may actually be looking for pivot and unpivot features. – ramana_k Oct 30 '15 at 06:32

0 Answers0