ALTER PROCEDURE [dbo].[EmployeeManagers]
@ID INT
AS
Begin
SET FMTONLY OFF;
WITH subordinate AS (
SELECT Id,
Name,
Surname,
BirthDate,
PositionId,
ManagerId,
CreatedDate,
DeletedDate,
IsDeleted,
ModifiedDate,
1 AS [Managing Level]
FROM Employees
WHERE Id = @ID
UNION ALL
SELECT e.Id,
e.Name,
e.Surname,
e.BirthDate,
e.PositionId,
e.ManagerId,
e.CreatedDate,
e.DeletedDate,
e.IsDeleted,
e.ModifiedDate,
[Managing Level] + 1
FROM Employees e
JOIN subordinate s
ON e.Id=s.ManagerId
)
SELECT
m.Id ,
m.Name,
m.Surname,
m.BirthDate,
m.PositionId,
m.ManagerId,
m.CreatedDate,
m.DeletedDate,
m.IsDeleted,
m.ModifiedDate,
s.[Managing Level]
FROM Employees m
JOIN subordinate s
ON s.ManagerId = m.Id
ORDER BY [Managing Level];
end
这是我的存储过程,它获取雇员经理。它获取所有雇员经理,并给予他们管理级别
public string Name { get; set; }
public string Surname { get; set; }
public DateTime BirthDate { get; set; }
public int? PositionId { get; set; }
public Position Position { get; set; }
public int? ManagerId { get; set; }
public ICollection<EmployeeDepartments> EmployeeDepartments { get; set; }
public DateTime CreatedDate { get; set; }
public DateTime? DeletedDate { get; set; }
public bool? IsDeleted { get; set; }
public DateTime? ModifiedDate { get; set; }
}
这是我的员工实体
当我调用存储过程时,它只返回雇员实体字段,但我还想获得管理级别
public IQueryable<Employee> GetEmployeeManagers(int employeeId)
{
var parameter = new SqlParameter("@ID", employeeId);
var managers = _context.Employees.FromSqlRaw("spManagersTable @ID", parameter);
return managers;
}
我无法获得管理级别,这是工作,但我还想获得管理级别。我还需要在我的员工实体中获得管理级别
1条答案
按热度按时间anauzrmj1#
第一种情况,WITH子句返回ID为
@ID
的经理和MangerId为@ID
的雇员的ID。然后,它选择仅与这些ID匹配的雇员,这些ID具有您可能需要检索的所有列。或者,您可以简单地使用OR运算,返回经理以及经理ID为
@ID
的雇员: