This is a far-fetched example, but consider a scenario where employees have jobs, and where employees have managers who also have jobs:
create table WorkingLocation (
ID int not null primary key identity(1,1),
Name varchar(50)
)
create table Employee (
ID int not null primary key identity(1,1),
Name varchar(50),
WorkingLocationID int null,
ManagerID int null,
constraint FK_Employee_WorkingLocation foreign key (WorkingLocationID) references WorkingLocation (ID),
constraint FK_Employee_Manager foreign key (ManagerID) references Employee (ID)
)
Now consider a business rule that wants to collaborate WorkingLocation, but will agree to its manager WorkingLocationif he does not have one. At this point you have two options:
1: You have a request that receives both and allows business rules to decide what to use:
select
e.*,
emp_location.*,
mgr_location.*
from Employee e
left join WorkingLocation emp_location on e.WorkingLocationID = emp_location.ID
left join Employee mgr on e.ManagerID = mgr.ID
left join WorkingLocation mgr_location on mgr.WorkingLocationID = mgr_location.ID
where e.ID = @id
2: Make separate calls to the database to get manager information if the employee does not WorkingLocation.
What do you prefer and why?
source
share