petarkobakov

12. Assign Employee Task (Service Database,)

Feb 15th, 2023
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. -- 12. Assign Employee
  2.  
  3. CREATE PROCEDURE usp_AssignEmployeeToReport(@EmployeeId INT, @ReportId INT)
  4. AS
  5. BEGIN
  6. DECLARE @employeeDepartmentId INT = (SELECT DepartmentId FROM Employees WHERE Id LIKE @EmployeeId)
  7.  
  8. DECLARE @categoryDepartmentId INT = (SELECT d.Id AS CategoryDepartmentId FROM Reports AS r
  9. JOIN Categories AS c
  10. ON r.CategoryId = c.Id
  11. JOIN Departments AS d
  12. ON c.DepartmentId = d.Id
  13. WHERE r.Id LIKE @ReportId)
  14.  
  15. IF(@employeeDepartmentId<>@categoryDepartmentId)
  16. THROW 500001, 'Employee doesn''t belong to the appropriate department!', 1
  17.  
  18. UPDATE Reports
  19. SET EmployeeId = @EmployeeId
  20. WHERE Id LIKE @ReportId
  21. END
  22.  
  23. -- Example
  24.  
  25. EXEC usp_AssignEmployeeToReport 30, 1
  26.  
  27. EXEC usp_AssignEmployeeToReport 17, 2
Advertisement
Add Comment
Please, Sign In to add comment