--case when 嵌套
declare @departmentID int
;with DP (dpid,dpname,dppreid)
as
(
select dpid,DPPreID from CRC_Department_DP dp
where ('1'= (case when @departmentID<0 then '1'
else case when DPID=@departmentID then '1' else '0' end end))
union all
select a.dpid,a.dpname,a.DPPreID from CRC_Department_DP a inner join
DP b on a.DPPreID = b.DPID
)