I think the SQL portion of my brain is turned off today because I can't for the life of me figure out how to do this and I think it should be easy. I'll simplify the problem as much as possible. This is for MS SQL Server 2000 by the way.
Say I have 2 tables, Employee and DepartmentRelation whose schema (and some example data) looks something like the code attached.
DepartmentRelation.Emp_ID AND Employee.Parent_ID are both foreign keys to Employee.ID
Now say I want to get a list of all Employees AND their parents(!) for a given Dept_ID. So given Dept_ID = 1, I need to follow the foreign key to Employee.ID = 3 and then get that employee and all of his parents. In this case, I'd be expecting a result set with every record in Employee.
The Employee INNER JOIN DepartmentRelations ON DepartmentRelation.Emp_ID = Employee.ID is the easy part but I'm not sure how to get the records on up the line.
Any SQL experts out there or non-experts that have any idea how to do this.
Say I have 2 tables, Employee and DepartmentRelation whose schema (and some example data) looks something like the code attached.
DepartmentRelation.Emp_ID AND Employee.Parent_ID are both foreign keys to Employee.ID
Now say I want to get a list of all Employees AND their parents(!) for a given Dept_ID. So given Dept_ID = 1, I need to follow the foreign key to Employee.ID = 3 and then get that employee and all of his parents. In this case, I'd be expecting a result set with every record in Employee.
The Employee INNER JOIN DepartmentRelations ON DepartmentRelation.Emp_ID = Employee.ID is the easy part but I'm not sure how to get the records on up the line.
Any SQL experts out there or non-experts that have any idea how to do this.