SQL * PLUS中的查询问题

时间:2013-10-01 05:32:27

标签: sql oracle sqlplus

我在sql * plus中运行此查询时收到ORA-00923(找不到FROM关键字)错误。

SELECT EMPLOYEE_ID, FIRST_NAME||' '||LAST_NAME AS FULLNAME
FROM EMPLOYEES
WHERE (JOB_ID, DEPARTMENT_ID) 
IN (SELECT JOB_ID, DEPARTMENT_ID FROM JOB_HISTORY)
AND DEPARTMENT_ID=80; 

我在sql开发人员中运行该查询并猜测是什么,它没有任何问题,为什么我在尝试使用sql * plus时收到此错误消息。

3 个答案:

答案 0 :(得分:1)

SELECT   EMPLOYEE_ID, FIRST_NAME || ' ' || LAST_NAME AS FULLNAME
  FROM   EMPLOYEES
 WHERE   JOB_ID IN (SELECT   JOB_ID
                      FROM   JOB_HISTORY
                     WHERE   DEPARTMENT_ID = 80);

OR

SELECT   EMPLOYEE_ID, FIRST_NAME || ' ' || LAST_NAME AS FULLNAME
  FROM   EMPLOYEES
 WHERE   JOB_ID IN (SELECT   JOB_ID FROM JOB_HISTORY) AND DEPARTMENT_ID = 80;

OR

SELECT   EMPLOYEE_ID, FIRST_NAME || ' ' || LAST_NAME AS FULLNAME
  FROM   EMPLOYEES E
 WHERE   EXISTS (SELECT   NULL
                   FROM   JOB_HISTORY J
                  WHERE   J.JOB_ID = E.JOB_ID)
         AND DEPARTMENT_ID = 80;

答案 1 :(得分:1)

您的查询完全有效,并在sqlplus中完全按预期运行:

14:04:01 (41)HR@sandbox> l
  1  SELECT EMPLOYEE_ID, FIRST_NAME||' '||LAST_NAME AS FULLNAME
  2  FROM EMPLOYEES
  3  WHERE (JOB_ID, DEPARTMENT_ID)
  4  IN (SELECT JOB_ID, DEPARTMENT_ID FROM JOB_HISTORY)
  5* AND DEPARTMENT_ID=80
14:04:05 (41)HR@sandbox> /

34 rows selected.

Elapsed: 00:00:00.01

只有在语法错误时才会遇到ORA-00923。像这样:

14:04:06 (41)HR@sandbox> ed
Wrote file S:\spool\sandbox\BUF_HR_41.sql

  1  SELECT EMPLOYEE_ID, FIRST_NAME||' '||LAST_NAME AS FULLNAME X
  2  FROM EMPLOYEES
  3  WHERE (JOB_ID, DEPARTMENT_ID)
  4  IN (SELECT JOB_ID, DEPARTMENT_ID FROM JOB_HISTORY)
  5* AND DEPARTMENT_ID=80
14:05:17 (41)HR@sandbox> /
SELECT EMPLOYEE_ID, FIRST_NAME||' '||LAST_NAME AS FULLNAME X
                                                           *
ERROR at line 1:
ORA-00923: FROM keyword not found where expected

您可能在将查询从sqldeveloper复制到sqlplus时创建了一个?您确定您的帖子包含完全符号,符号到您实际尝试执行的查询吗?我会更加关注查询文本和错误消息 - 它通常指向错误,例如我的示例中*下的X

答案 2 :(得分:0)

我不知道你想要实现什么,但这是一个可能的解决方案:

/* Formatted on 10/1/2013 1:50:20 PM (QP5 v5.126.903.23003) */
SELECT   EMPLOYEE_ID, FIRST_NAME || ' ' || LAST_NAME AS FULLNAME
  FROM      EMPLOYEES EMP
         JOIN
            JOB_HISTORY JH
         ON EMP.JOB_ID = JH.JOB_ID AND EMP.DEPARTMENT_ID = JH.DEPARTMENT_ID
 WHERE   EMP.DEPARTMENT_ID = 80;