我正在编写一个看起来像这样的存储过程:
SELECT
Positions.PositionId
,Positions.Title
,Positions.Location
,Positions.Description
,Positions.MaximumSalary
,PositionsDepartments.Description Department
,PositionsSubDepartments.Description Subdepartment
,PositionsDepartments.DepartmentId DepartmentId
,PositionsSubDepartments.SubDepartmentId SubdepartmentId
,@TheRole TheRole
,@Essentials Essentials
,@Desirable Desirable
,Positions.Published
,Positions.LastUpdatedDate
,PositionsStatus.Description Status
FROM
Positions
WITH (NOLOCK)
INNER JOIN PositionsStatus ON Positions.StatusId = PositionsStatus.StatusId
INNER JOIN PositionsSubDepartments ON Positions.SubDepartmentId = PositionsSubDepartments.SubDepartmentId
INNER JOIN PositionsDepartments ON PositionsSubDepartments.DepartmentId = PositionsDepartments.DepartmentId
WHERE
Positions.PositionId = @PositionId
但是 Positions.SubDepartmentId 现在可能为空 - 这意味着我没有得到我期望的所有数据。我已经尝试过了,但得到了大量重复数据:
SELECT
Positions.PositionId
,Positions.Title
,Positions.Location
,Positions.Description
,Positions.MaximumSalary
,PositionsDepartments.Description Department
,PositionsSubDepartments.Description Subdepartment
,PositionsDepartments.DepartmentId DepartmentId
,PositionsSubDepartments.SubDepartmentId SubdepartmentId
,@TheRole TheRole
,@Essentials Essentials
,@Desirable Desirable
,Positions.Published, Positions.LastUpdatedDate
,PositionsStatus.Description Status
FROM
Positions
WITH (NOLOCK)
INNER JOIN PositionsStatus ON Positions.StatusId = PositionsStatus.StatusId
INNER JOIN PositionsSubDepartments ON Positions.SubDepartmentId = PositionsSubDepartments.SubDepartmentId OR ( Positions.SubDepartmentId IS NULL)
INNER JOIN PositionsDepartments ON PositionsSubDepartments.DepartmentId = PositionsDepartments.DepartmentId
WHERE
Positions.PositionId = @PositionId
我究竟做错了什么?