2016-12-27 183 views
2

我有以下子查詢返回多個值。存儲

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 

這個SQL查詢我上面的標題說的錯誤。如果U使用SELECT TOP 1它運行良好,但我想從該查詢中獲取數據。到目前爲止,我有兩行數據。

回答

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 
+1

感謝的人。我對sql很陌生 – KiRa