\"The maximum recursion 100 has been exhausted before statement completion\" error showing in SQL Query
WITH DepartmentCTE AS
( SELECT ID,
Department
Not sure if this is what you intended, but realize the DepartmentCTE CTE “calls” itself because the second part of its union is “From DepartmentCTE”. This is very useful behavior if intended and really bad if not intended. In your case I don’t see anything limiting the recursion. The CTE would juts call itself indefinitely . If you use the recursion, normally there would be some kind of limiting statement like an “If Level...” or “If Exists…). The recursion level of 100 is quite generous for DB environment and would agree with @jpw that turning it off would be bad. IN your case really would be an infinite loop until process crashed or something like it.
Was the looping your intent? If not, remove the DepartmentCTE in some way. If so then find how to limit in based one when you are “done”. If not sure maybe give some more info about goal to see if we can figure out.