IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_depart_details]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_depart_details]
GO
create proc mps_depart_details
@cmpid int,
@bcid int,
@key varchar(50)
as 
begin
    set nocount on
declare @fdpid int
set @fdpid = CONVERT(int,@key)
if @fdpid = 0
begin
select dp_depart as caption,
(select count(cu_id) from mps_employee c where cu_dpid = dp_id and cu_bcid=@bcid 
and exists (select 1 from mps_login b where b.cu_id =c.cu_id)) amount,
dp_id as value,
dp_fdpid as fvalue 
from mps_depart a   where dp_cmpid=@cmpid and dp_bcid=@bcid  and dp_isdepart=0
order by dp_order, dp_level
end else
begin
select dp_depart,
(select count(cu_id) from mps_employee c where cu_dpid = dp_id and cu_bcid=@bcid and 
exists (select 1 from mps_login b where b.cu_id =c.cu_id)) amount,dp_id 
from  mps_depart a   
where dp_cmpid=@cmpid and dp_fdpid=@fdpid  and dp_isdepart=1 and dp_bcid=@bcid
end
set nocount off
end