这个SQL查询有什么问题?

时间:2011-12-05 07:00:00

标签: sql sql-server-2008

我正在开发一个Web应用程序,现在我需要使这个应用程序能够从其数据库生成一些报告。我有一个庞大的数据库,结构非常复杂。我提出了一个很大的SQL查询,它对我很好,直到我改变了一点数据库的结构。我根据这些变化对其进行了修改,但仍无效。我在SQLServer Management Studio中测试了它,但我失败了。有时它会告诉我错误是语法错误。有时它会告诉我''。

的错误

数据库结构简单:

员工表:姓名,用户名,工作提示,BadgeNo,EmpOrgType,DivisionID

部门表:部门代码,部门名称

课程表: CourseID,CourseName,GroupID

组表: GroupID,GroupName

employee_courses: employeeID,courseID

每个表中的第一个属性是每个表的主键,除了最后一个表employee_courses表。

查询是:

select *
from
    (SELECT TOP (100) PERCENT 
            dbo.Divisions.DivisionName, 
            dbo.employee.EmpOrgType, 
            dbo.employee.Name, 
            T1.Username, 
            courses_2.CourseName, 
            CASE WHEN dbo.employee_courses.courseId IS NULL THEN '' '' 
                 ELSE ''Yes'' END AS CourseId
     FROM dbo.employee_courses 
     RIGHT OUTER JOIN dbo.courses AS courses_2 
     INNER JOIN (SELECT employee_1.Username, 
                        courses_1.CourseID
                 FROM dbo.employee AS employee_1 
                 CROSS JOIN dbo.courses AS courses_1) 
                                  AS T1 ON courses_2.CourseID = T1.CourseID 
     INNER JOIN dbo.employee ON T1.Username = dbo.employee.Username 
                             ON dbo.employee_courses.employeeId = T1.Username AND
                                dbo.employee_courses.courseId = T1.CourseID
     ORDER BY T1.Username
    ) DataTable

此查询应检索员工的信息:Name,Username,JobTitle,BadgeNo,EmpOrgType和DivisionName。然后,它应该显示他从课程表中提取的课程,并提及所选课程的GroupName。

2 个答案:

答案 0 :(得分:1)

  

有时它告诉我错误是语法错误。有时它会告诉我''。

的错误

问题可能在这里

CASE WHEN dbo.employee_courses.courseId IS NULL THEN '' '' ELSE ''Yes'' END AS CourseId

答案 1 :(得分:1)

你真的想在结果字符串中看到单引号吗?如果是,请尝试:

SELECT *
FROM   (SELECT TOP (100) PERCENT dbo.divisions.divisionname,
                             dbo.employee.emporgtype,
                             dbo.employee.name,
                             t1.username,
                             courses_2.coursename,
                             CASE
                               WHEN dbo.employee_courses.courseid IS NULL
                             THEN ''' '''
                               ELSE '''Yes'''
                             END AS courseid
    FROM   dbo.employee_courses
           RIGHT OUTER JOIN dbo.courses AS courses_2
                            INNER JOIN (SELECT employee_1.username,
                                               courses_1.courseid
                                        FROM   dbo.employee AS employee_1
                                               CROSS JOIN dbo.courses AS
                                                          courses_1)
                                       AS t1
                              ON courses_2.courseid = t1.courseid
                            INNER JOIN dbo.employee
                              ON t1.username = dbo.employee.username
             ON dbo.employee_courses.employeeid = t1.username
                AND dbo.employee_courses.courseid = t1.courseid
    ORDER  BY t1.username) datatable