子查询返回的值超过1。存储

时间:2016-12-27 02:27:27

标签: sql-server

我在下面有这个SQL查询

SELECT 
    CASE
       WHEN ISNULL(dbo.EducationalBackground.SchoolId,'') = '' 
            AND ISNULL(dbo.EducationalBackground.SchoolName,'') <> '' 
          THEN 
             (SELECT a.SchoolName
              FROM dbo.EducationalBackground AS A 
              INNER JOIN dbo.PersonEducationalBackground as B ON a.EducationalBackgroundId = b.EducationalBackgroundId
              WHERE b.PersonId = @PersonId AND a.EducationalLevelId = 1)

        WHEN ISNULL(dbo.EducationalBackground.SchoolId,'') = '' 
             AND ISNULL(dbo.EducationalBackground.SchoolName,'') = '' 
           THEN 'N/A'

        ELSE 
            (SELECT dbo.School.SchooldName 
             FROM dbo.School 
             INNER JOIN dbo.EducationalBackground ON dbo.School.SchoolId = dbo.EducationalBackground.SchoolId 
             INNER JOIN dbo.PersonEducationalBackground ON dbo.EducationalBackground.EducationalBackgroundId = dbo.PersonEducationalBackground.EducationalBackgroundId
             WHERE dbo.PersonEducationalBackground.PersonId = @PersonId)
    END AS SchoolName
FROM     
    dbo.EducationalBackground  
INNER JOIN
    PersonEducationalBackground ON dbo.EducationalBackground.EducationalBackgroundId = PersonEducationalBackground.EducationalBackgroundId 
INNER JOIN
    dbo.Person ON PersonEducationalBackground.PersonId = dbo.Person.PersonId 
INNER JOIN
    PIMS.Employee ON PIMS.Employee.PersonId = dbo.Person.PersonId 
INNER JOIN
    dbo.EducationLevel  ON dbo.EducationLevel.EducationLevelId = dbo.EducationalBackground.EducationalLevelId 
WHERE
    dbo.Person.PersonId = @PersonId
    AND dbo.EducationalBackground.EducationalLevelId = 1

标题上写的上面有一个错误。如果你使用SELECT TOP 1它运行正常,但我想从该查询中获取数据。到目前为止,我有两行数据。

1 个答案:

答案 0 :(得分:1)

我修改了你的SQL语句,它可能会从你的语句中返回不同的行,你能试试吗,检查你想得到的结果

    SELECT case when ISNULL(dbo.EducationalBackground.SchoolId,'') = '' and ISNULL(dbo.EducationalBackground.SchoolName,'') <> '' then
                          EducationalBackground.SchoolName
                WHEN ISNULL(dbo.EducationalBackground.SchoolId,'') = '' and ISNULL(dbo.EducationalBackground.SchoolName,'') = '' then 'N/A'
                else School.SchooldName 
                end as SchoolName

    FROM     dbo.EducationalBackground  
             INNER JOIN PersonEducationalBackground on dbo.EducationalBackground.EducationalBackgroundId = PersonEducationalBackground.EducationalBackgroundId 
             inner JOIN dbo.Person ON PersonEducationalBackground.PersonId = dbo.Person.PersonId
             inner JOIN PIMS.Employee ON PIMS.Employee.PersonId = dbo.Person.PersonId
             inner JOIN dbo.EducationLevel  ON dbo.EducationLevel.EducationLevelId = dbo.EducationalBackground.EducationalLevelId 
             LEFT JOIN PersonEducationalBackground AS b ON EducationalBackground.EducationalBackgroundId = b.EducationalBackgroundId AND EducationalBackground.EducationalLevelId=1
             LEFT JOIN dbo.School ON dbo.School.SchoolId = dbo.EducationalBackground.SchoolId AND PersonEducationalBackground.PersonId = @PersonId

    where dbo.Person.PersonId = @PersonId
        and dbo.EducationalBackground.EducationalLevelId = 1