0

当我在 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 developer 中运行了该查询并猜测是什么,它没有任何问题,为什么我在 sql*plus 中尝试时收到此错误消息。

4

3 回答 3

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);

或者

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;

或者

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;
于 2013-10-01T05:51:51.660 回答
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我的示例中一样。

于 2013-10-01T06:09:02.613 回答
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;
于 2013-10-01T05:48:40.883 回答