oracle-databasecolumn-alias

ORA-00923: FROM keyword not found where expected, parameters in AS clause


SELECT
     LMD0011M.CKEY1                             AS CDNAME 
    ,LMD0011M.CKEY1 || '\:' || LMD0011M.CDTA1    AS NMNAME 
    FROM 
     LMD0011M LMD0011M

The above query works fine but when I change the column names to parameters... I get an "ORA-00923: FROM keyword not found where expected" error message.

SELECT
     LMD0011M.CKEY1                             AS :CDNAME 
    ,LMD0011M.CKEY1 || '\:' || LMD0011M.CDTA1    AS :NMNAME 
    FROM 
     LMD0011M LMD0011M

:CDNAME = CDNAME
:NMNAME = NMNAME

Even if I run the query in Oracle SQL Developer, I'm getting the error. I'm using Java Hibernate. How to solve this problem? Help pls


Solution

  • It would probably help if you described why you want to dynamically change column aliases. Personally, I don't see any valid cause to do that, but hey - if you do, no objections.

    From my point of view, it is PL/SQL you need and dynamic SQL. Pass desired aliases to the procedure and return refcursor which reflects what you've just passed. Here's an example:

    SQL> create or replace procedure p_test
      2    (alias_1 in varchar2, alias_2 in varchar2, par_rc out sys_refcursor)
      3  is
      4    l_str varchar2(1000);
      5  begin
      6    l_str := 'select ename as "' || dbms_assert.simple_sql_name(alias_1) ||'"'||
      7             ', job as "'        || dbms_assert.simple_sql_name(alias_2) ||'"'||
      8             ' from emp where deptno = 20';
      9    open par_rc for l_str;
     10  end;
     11  /
    

    Testing:

    SQL> var rc refcursor
    SQL> exec p_test('liTTle', 'f00t', :rc);
    
    PL/SQL procedure successfully completed.
    
    SQL> print :rc
    
    liTTle     f00t
    ---------- ---------
    SMITH      CLERK
    JONES      MANAGER
    SCOTT      ANALYST
    ADAMS      CLERK
    FORD       ANALYST
    
    SQL>
    SQL> exec p_test('brian', 'antiquena', :rc);
    
    PL/SQL procedure successfully completed.
    
    SQL> print :rc
    
    BRIAN      ANTIQUENA
    ---------- ---------
    SMITH      CLERK
    JONES      MANAGER
    SCOTT      ANALYST
    ADAMS      CLERK
    FORD       ANALYST
    
    SQL>