ORA-00923: 在预期的位置找不到 FROM 关键字,AS 子句中的参数

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

上面的查询工作正常,但是当我将列名更改为参数时...我收到“ORA-00923:在预期的位置找不到 FROM 关键字”错误消息。

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

:CDNAME = CDNAME
:NMNAME = NMNAME

即使我 运行 在 Oracle SQL Developer 中查询,我也会收到错误。我正在使用 Java 休眠。如何解决这个问题呢?请帮忙

如果您描述了为什么您想要动态更改列别名,这可能会有所帮助。就个人而言,我看不出有任何正当理由这样做,但是嘿 - 如果你这样做,没有异议。

在我看来,它是 PL/SQL 你需要的并且是动态的 SQL。将所需的别名传递给过程和 return refcursor,它 反映 您刚刚传递的内容。这是一个例子:

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  /

测试:

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>