SQL命令未正确结束-不知道为什么

时间:2018-11-02 19:40:32

标签: sql oracle

我正在尝试处理此SQL查询,并不断陷入

  

SQL命令未正确结束

声明。

确切的错误是:

 AND NOT PROJECT.dnum = EMPLOYEE.dno)) > 10
                                     *
 ERROR at line 4:
 ORA-00933: SQL command not properly ended

以下是架构:

    CREATE TABLE EMPLOYEE (
        fname VARCHAR(20), 
        minit CHAR(1), 
        lname VARCHAR(20), 
        ssn INT NOT NULL, 
        bdate VARCHAR(20), 
        address VARCHAR(40), 
        sex CHAR(1) check (sex IN ('M', 'F')), 
        salary INT CHECK (salary > 20000), 
        superssn INT,
        dno INT,
        PRIMARY KEY (ssn)
    );

    CREATE TABLE DEPARTMENT (
        dname VARCHAR(20),
        dnumber INT NOT NULL,
        mgr_ssn INT,
        mgr_start_date VARCHAR(20),
        PRIMARY KEY (dnumber)
    );

    CREATE TABLE DEPTLOCATIONS (
        dnumber INT NOT NULL, 
        dlocation VARCHAR(20) NOT NULL check (dlocation IN ('BELLAIRE', 'SUGARLAND', 'HOUSTON', 'STAFFORD')),
        PRIMARY KEY (dnumber, dlocation)
    );

    CREATE TABLE PROJECT (
        pname VARCHAR(20) NOT NULL,
        pnumber INT NOT NULL,
        plocation VARCHAR(20) CHECK (plocation IN ('BELLAIRE', 'SUGARLAND', 'HOUSTON', 'STAFFORD')),
        dnum INT NOT NULL,
        PRIMARY KEY (pnumber)
    );

    CREATE TABLE WORKS_ON (
        essn INT NOT NULL,
        pno INT NOT NULL,
        hours FLOAT check (hours >= 5 AND hours <= 40),
        PRIMARY KEY (essn, pno)
    );

    CREATE TABLE DEPENDENT (
        essn INT NOT NULL,
        dependent_name VARCHAR(40) NOT NULL,
        sex CHAR(1) check (sex IN ('M', 'F')),
        bdate VARCHAR(20),
        relationship VARCHAR(20) CHECK (relationship IN ('SPOUSE', 'SON', 'DAUGHTER')),
        PRIMARY KEY (essn, dependent_name)
    );

我正在寻找薪水为 高于同一个部门中所有有两个员工的平均工资 或更多受抚养人;然后,对于每个找到的员工,我需要检查该员工的工作是否超过 不受家庭部门控制的项目,每周总计10个小时, 如果有的话,请打印员工的全名,以及薪水,受抚养人的数量, 由内政部控制的项目的总小时数,以及 其他(即非住宅)部门控制的项目的小时数

这是我的SQL查询:

    SELECT concat(
    concat
    (EMPLOYEE.fname, ' '|| EMPLOYEE.minit), 
    ' '||EMPLOYEE.lname) as NAME, 
    EMPLOYEE.ssn,
    (select count(*) from dependent where Employee.ssn=dependent.essn) AS Num_Dependents, 

    (SELECT SUM(hours) FROM EMPLOYEE 
    JOIN WORKS_ON ON (EMPLOYEE.ssn = WORKS_ON.essn) 
    JOIN PROJECT ON (PROJECT.pnumber=WORKS_ON.pno) 
    WHERE PROJECT.dnum = EMPLOYEE.dno) 
    AS HOME_HOURS,

    (SELECT SUM(hours) FROM EMPLOYEE 
    JOIN WORKS_ON ON (EMPLOYEE.ssn = WORKS_ON.essn) 
    JOIN PROJECT ON (PROJECT.pnumber=WORKS_ON.pno) 
    WHERE NOT PROJECT.dnum = EMPLOYEE.dno) AS NOT_HOME_HOURS,

    (SELECT EMPLOYEE.salary FROM EMPLOYEE WHERE
    EMPLOYEE.salary > (SELECT AVG(salary) FROM EMPLOYEE WHERE EMPLOYEE.dno = dno))

    AND 

    (SELECT SUM(hours) FROM EMPLOYEE 
    JOIN WORKS_ON ON (EMPLOYEE.ssn = WORKS_ON.essn) 
    JOIN PROJECT ON (PROJECT.pnumber=WORKS_ON.pno) 
    AND NOT PROJECT.dnum = EMPLOYEE.dno)) > 10  
    AND (SELECT COUNT(*) FROM DEPENDENT WHERE essn = ssn) >= 2;

我知道它的格式可能不尽如人意,但是我已经呆了好几个小时了,似乎没有什么可以摆脱的

  

SQL命令未正确结束

运行查询时引发的

错误。任何帮助或提示将不胜感激!谢谢大家。

2 个答案:

答案 0 :(得分:2)

您的sql看起来很奇怪。归结为一个简单的表示形式,它的结构看起来像(希望我正确理解了括号的布局):

SELECT
  (Query),
  (Query),
  (Query) AND (Query)
) dangling unrelated fragment

Oracle需要一个FROM,它不能处理包含这样的AND的选择列表。

  

我知道它的格式可能不尽人意

绝对是100%的问题。.如果没有很好地布置您的SQL,您将完全迷失在意大利面条中,而您最终得到了一个没有全套匹配括号的SQL,因此遭受语法错误

如果我将它放入文本编辑器并平衡括号,这就是您的sql的样子,这样看起来像(query here) as alias,的每一行都占一行:

enter image description here

您最终可以看到一个额外的支架在里面爬进并最终破坏了东西?

(请注意“不要截屏代码”的纯粹主义者-故意突出显示问题,好像它已被发布为文本一样,可能会包裹并失去影响)

答案 1 :(得分:0)

在此处的代码中:

从员工所在位置选择员工     EMPLOYEE.salary>(从EMPLOYEE的EMPLOYEE.dno = dno中选择AVG(salary)

AND 

(SELECT SUM(hours) FROM EMPLOYEE 
JOIN WORKS_ON ON (EMPLOYEE.ssn = WORKS_ON.essn) 
JOIN PROJECT ON (PROJECT.pnumber=WORKS_ON.pno) 
AND NOT PROJECT.dnum = EMPLOYEE.dno)) > 10  
AND (SELECT COUNT(*) FROM DEPENDENT WHERE essn = ssn) >= 2;

用尖括号括起来(以粗体显示)可以完成此Select语句,而无需考虑将小时数加总的select语句。因此,使用WHERE子句时的整体结构会受到影响。