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
它運行良好,但我想從該查詢中獲取數據。到目前爲止,我有兩行數據。
感謝的人。我對sql很陌生 – KiRa