WHERE上的SQL Server查询错误

时间:2017-12-01 02:12:06

标签: c# sql sql-server

SQL Server 2005通过C#(VS 20015)。知道为什么WHERE不起作用?

SQL语句是:

SELECT 
    AA003Employee.idEmployee, 
    AA001Person.LastName, AA001Person.FirstName, 
    AA017JobTitle.JobTitle, AA017JobTitle.Department, 
    AA003Employee.Disabled 
FROM 
    AA003Employee 
JOIN 
    AA001Person ON AA003Employee.idPerson = AA001Person.idPerson 
JOIN 
    AA017JobTitle ON AA003Employee.idJobTitle = AA017JobTitle.idJobTitle 
ORDER BY 
    AA001Person.LastName ASC, AA001Person.FirstName ASC, 
    AA017JobTitle.JobTitle ASC 
WHERE 
    AA003Employee.Disabled = 'N';

尝试:

  1. 省略 - >时工作正常WHERE AA003Employee.Disabled = 'N'

  2. 当我尝试用 - >替换它时会抛出错误“...... WHERE Disabled = 'N'";

  3. 以下是错误的屏幕截图:

    Screenshot

3 个答案:

答案 0 :(得分:6)

您的问题是WHERE子句的放置。 SQL关心语句中子句的顺序。但是你也应该学会使用表别名:

SELECT e.idEmployee, p.LastName, p.FirstName, jt.JobTitle, jt.Department, e.Disabled
FROM AA003Employee e JOIN
     AA001Person p
     ON e.idPerson = p.idPerson JOIN
     AA017JobTitle jt
     ON e.idJobTitle = jt.idJobTitle
WHERE e.Disabled = 'N'
ORDER BY p.LastName ASC, p.FirstName ASC, jt.JobTitle ASC;

请参阅?查询更容易编写和阅读。

答案 1 :(得分:1)

用,

替换查询
SELECT AA003Employee.idEmployee, AA001Person.LastName, AA001Person.FirstName, AA017JobTitle.JobTitle, AA017JobTitle.Department, AA003Employee.Disabled 
FROM AA003Employee 
JOIN AA001Person ON AA003Employee.idPerson=AA001Person.idPerson 
JOIN AA017JobTitle ON AA003Employee.idJobTitle=AA017JobTitle.idJobTitle 
WHERE AA003Employee.Disabled = 'N' 
ORDER BY AA001Person.LastName ASC, AA001Person.FirstName ASC, AA017JobTitle.JobTitle ASC 

答案 2 :(得分:0)

用以下SQL语句替换查询

SELECT 
    AA003Employee.idEmployee, 
    AA001Person.LastName, 
    AA001Person.FirstName, 
    AA017JobTitle.JobTitle, 
    AA017JobTitle.Department, 
    AA003Employee.Disabled 

FROM AA003Employee 

JOIN AA001Person ON AA003Employee.idPerson=AA001Person.idPerson 
JOIN AA017JobTitle ON AA003Employee.idJobTitle=AA017JobTitle.idJobTitle 

WHERE AA003Employee.Disabled = 'N';

ORDER BY AA001Person.LastName ASC, AA001Person.FirstName ASC, AA017JobTitle.JobTitle ASC