无法执行查询

时间:2015-10-28 06:30:11

标签: sql sql-server

当我输入如下所示的代码时,我收到了这个错误:

  

Msg 116,Level 16,State 1,Line 1
  当未使用EXISTS引入子查询时,只能在选择列表中指定一个表达式。

代码:

SELECT 
    E_ID, E_NAME, PROJECT, MANAGER
FROM 
    EMPLOYEE_PROJECT 
INNER JOIN 
    EMP_MASTER ON (EMPID = E_ID AND LOCATION = 'MUMBAI' AND 
                   E_ID NOT IN (SELECT * FROM SALARY_ADVANCE))

4 个答案:

答案 0 :(得分:9)

问题在这里

 E_ID NOT IN (SELECT * FROM SALARY_ADVANCE))

您必须提及一列而不是所有

答案 1 :(得分:7)

这一行的问题:

E_ID NOT IN (SELECT * FROM SALARY_ADVANCE)

您应该在本SELECT中提及您检查的字段。否则,您获得的表集不是一组值。

E_ID NOT IN (SELECT FIELD_NAME FROM SALARY_ADVANCE)

答案 2 :(得分:3)

NOT IN有点棘手,因为如果从子查询返回NULL,它会返回零行。也许不是在许多情况下的预期......在子查询中添加IS NOT NULL以避免这种情况。

SELECT E_ID, 
       E_NAME, 
       PROJECT, 
       MANAGER
  FROM EMPLOYEE_PROJECT INNER JOIN EMP_MASTER ON 
         EMPID = E_ID AND 
         LOCATION = 'MUMBAI' AND 
         E_ID NOT IN (SELECT E_ID 
                        FROM SALARY_ADVANCE
                      WHERE E_ID IS NOT NULL)

您也可以重新写入NOT EXISTS,这是无效的!

SELECT E_ID, 
       E_NAME, 
       PROJECT, 
       MANAGER
  FROM EMPLOYEE_PROJECT INNER JOIN EMP_MASTER ON (
         EMPID = E_ID AND 
         LOCATION = 'MUMBAI' AND 
         NOT EXISTS (SELECT E_ID 
                     FROM SALARY_ADVANCE as SA
                     WHERE SA.E_ID = EMP*table*.E_ID))

答案 3 :(得分:2)

SELECT E_ID, 
       E_NAME, 
       PROJECT, 
       MANAGER
  FROM EMPLOYEE_PROJECT INNER JOIN EMP_MASTER ON 
         EMPID = E_ID AND 
         LOCATION = 'MUMBAI' AND 
         E_ID NOT IN (SELECT E_ID 
                        FROM SALARY_ADVANCE)