IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_sys_calc_employee_manager]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_sys_calc_employee_manager]
GO
create procedure [dbo].[mps_sys_calc_employee_manager](@cmpid int) 
as
begin

SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
delete mps_employee_manager where cum_cmpid=@cmpid 
declare @level int = 1
insert into mps_employee_manager(cum_cmpid,cum_cuid,cum_dpid,cum_mcuid,cum_level,cum_isdepart,cum_gradediff) 
select @cmpid,lgu_cuid,dp_id,dp_mcuid1,@level,dp_isdepart,@level
from mps_depart with(nolock) , mps_login_role with(nolock)
where dp_cmpid=@cmpid and dp_id=lgu_dpid and dp_mcuid1>0 and not exists 
  (select 1 from mps_employee_manager with(nolock) 
  where cum_cuid=lgu_cuid and cum_dpid=dp_id and dp_mcuid1=cum_mcuid and cum_level=@level)
  
set @level = 2 
insert into mps_employee_manager(cum_cmpid,cum_cuid,cum_dpid,cum_mcuid,cum_level,cum_isdepart,cum_gradediff)  
select @cmpid,lgu_cuid,dp_id,dp_mcuid2,@level,dp_isdepart,@level
from mps_depart with(nolock) , mps_login_role with(nolock)
where dp_cmpid=@cmpid and dp_id=lgu_dpid and dp_mcuid2>0 and not exists 
  (select 1 from mps_employee_manager with(nolock) 
  where cum_cuid=lgu_cuid and cum_dpid=dp_id and dp_mcuid2=cum_mcuid and cum_level=@level)  

insert into mps_employee_manager(cum_cmpid,cum_cuid,cum_dpid,cum_mcuid,cum_level,cum_isdepart,cum_gradediff) 
select @cmpid,dp_mcuid1,dp_id,dp_mcuid2,@level,dp_isdepart,@level-1
from mps_depart with(nolock)  
where dp_cmpid=@cmpid and dp_mcuid1>0 and dp_mcuid2>0 and not exists 
  (select 1 from mps_employee_manager with(nolock) 
  where cum_cuid=dp_mcuid1 and cum_dpid=dp_id and dp_mcuid2=cum_mcuid and cum_level=@level-1)  

set @level = 3
insert into mps_employee_manager(cum_cmpid,cum_cuid,cum_dpid,cum_mcuid,cum_level,cum_isdepart,cum_gradediff)  
select @cmpid,lgu_cuid,dp_id,dp_mcuid3,@level,dp_isdepart,@level
from mps_depart with(nolock) , mps_login_role with(nolock)
where dp_cmpid=@cmpid and dp_id=lgu_dpid and dp_mcuid3>0 and not exists 
  (select 1 from mps_employee_manager with(nolock) 
  where cum_cuid=lgu_cuid and cum_dpid=dp_id and dp_mcuid3=cum_mcuid and cum_level=@level)  

insert into mps_employee_manager(cum_cmpid,cum_cuid,cum_dpid,cum_mcuid,cum_level,cum_isdepart,cum_gradediff) 
select @cmpid,dp_mcuid2,dp_id,dp_mcuid3,@level,dp_isdepart,@level-2
from mps_depart with(nolock)  
where dp_cmpid=@cmpid and dp_mcuid2>0 and dp_mcuid3>0 and not exists 
  (select 1 from mps_employee_manager with(nolock) 
  where cum_cuid=dp_mcuid2 and cum_dpid=dp_id and dp_mcuid3=cum_mcuid and cum_level=@level-2)  

insert into mps_employee_manager(cum_cmpid,cum_cuid,cum_dpid,cum_mcuid,cum_level,cum_isdepart,cum_gradediff) 
select @cmpid,dp_mcuid1,dp_id,dp_mcuid3,@level,dp_isdepart,@level-1
from mps_depart with(nolock)  
where dp_cmpid=@cmpid and dp_mcuid1>0 and dp_mcuid3>0 and not exists 
  (select 1 from mps_employee_manager with(nolock) 
  where cum_cuid=dp_mcuid1 and cum_dpid=dp_id and dp_mcuid3=cum_mcuid and cum_level=@level-1)    
  
set @level = 4
insert into mps_employee_manager(cum_cmpid,cum_cuid,cum_dpid,cum_mcuid,cum_level,cum_isdepart,cum_gradediff) 
select @cmpid,lgu_cuid,dp_id,dp_mcuid4,@level,dp_isdepart,@level
from mps_depart with(nolock) , mps_login_role with(nolock)
where dp_cmpid=@cmpid and dp_id=lgu_dpid and dp_mcuid4>0 and not exists 
  (select 1 from mps_employee_manager with(nolock) 
  where cum_cuid=lgu_cuid and cum_dpid=dp_id and dp_mcuid4=cum_mcuid and cum_level=@level)  

insert into mps_employee_manager(cum_cmpid,cum_cuid,cum_dpid,cum_mcuid,cum_level,cum_isdepart,cum_gradediff) 
select @cmpid,dp_mcuid3,dp_id,dp_mcuid4,@level,dp_isdepart,@level-3
from mps_depart with(nolock)  
where dp_cmpid=@cmpid and dp_mcuid3>0 and dp_mcuid4>0 and not exists 
  (select 1 from mps_employee_manager with(nolock) 
  where cum_cuid=dp_mcuid3 and cum_dpid=dp_id and dp_mcuid4=cum_mcuid and cum_level=@level-3)  

insert into mps_employee_manager(cum_cmpid,cum_cuid,cum_dpid,cum_mcuid,cum_level,cum_isdepart,cum_gradediff) 
select @cmpid,dp_mcuid2,dp_id,dp_mcuid4,@level,dp_isdepart,@level-2
from mps_depart with(nolock)  
where dp_cmpid=@cmpid and dp_mcuid2>0 and dp_mcuid4>0 and not exists 
  (select 1 from mps_employee_manager with(nolock) 
  where cum_cuid=dp_mcuid2 and cum_dpid=dp_id and dp_mcuid4=cum_mcuid and cum_level=@level-2)      

insert into mps_employee_manager(cum_cmpid,cum_cuid,cum_dpid,cum_mcuid,cum_level,cum_isdepart,cum_gradediff) 
select @cmpid,dp_mcuid1,dp_id,dp_mcuid4,@level,dp_isdepart,@level-1
from mps_depart with(nolock)  
where dp_cmpid=@cmpid and dp_mcuid1>0 and dp_mcuid4>0 and not exists 
  (select 1 from mps_employee_manager with(nolock) 
  where cum_cuid=dp_mcuid1 and cum_dpid=dp_id and dp_mcuid4=cum_mcuid and cum_level=@level-1)      

set @level = 5
insert into mps_employee_manager(cum_cmpid,cum_cuid,cum_dpid,cum_mcuid,cum_level,cum_isdepart,cum_gradediff) 
select @cmpid,lgu_cuid,dp_id,dp_mcuid5,@level,dp_isdepart,@level
from mps_depart with(nolock) , mps_login_role with(nolock)
where dp_cmpid=@cmpid and dp_id=lgu_dpid and dp_mcuid5>0 and not exists 
  (select 1 from mps_employee_manager with(nolock) 
  where cum_cuid=lgu_cuid and cum_dpid=dp_id and dp_mcuid5=cum_mcuid and cum_level=@level)  

insert into mps_employee_manager(cum_cmpid,cum_cuid,cum_dpid,cum_mcuid,cum_level,cum_isdepart,cum_gradediff) 
select @cmpid,dp_mcuid4,dp_id,dp_mcuid5,@level,dp_isdepart,@level-4
from mps_depart with(nolock)  
where dp_cmpid=@cmpid and dp_mcuid4>0 and dp_mcuid5>0 and not exists 
  (select 1 from mps_employee_manager with(nolock) 
  where cum_cuid=dp_mcuid4 and cum_dpid=dp_id and dp_mcuid5=cum_mcuid and cum_level=@level-4)  

insert into mps_employee_manager(cum_cmpid,cum_cuid,cum_dpid,cum_mcuid,cum_level,cum_isdepart,cum_gradediff) 
select @cmpid,dp_mcuid3,dp_id,dp_mcuid5,@level,dp_isdepart,@level-3
from mps_depart with(nolock)  
where dp_cmpid=@cmpid and dp_mcuid3>0 and dp_mcuid5>0 and not exists 
  (select 1 from mps_employee_manager with(nolock) 
  where cum_cuid=dp_mcuid3 and cum_dpid=dp_id and dp_mcuid5=cum_mcuid and cum_level=@level-3)      

insert into mps_employee_manager(cum_cmpid,cum_cuid,cum_dpid,cum_mcuid,cum_level,cum_isdepart,cum_gradediff)  
select @cmpid,dp_mcuid2,dp_id,dp_mcuid5,@level,dp_isdepart,@level-2
from mps_depart with(nolock)  
where dp_cmpid=@cmpid and dp_mcuid2>0 and dp_mcuid5>0 and not exists 
  (select 1 from mps_employee_manager with(nolock) 
  where cum_cuid=dp_mcuid2 and cum_dpid=dp_id and dp_mcuid5=cum_mcuid and cum_level=@level-2)      

insert into mps_employee_manager(cum_cmpid,cum_cuid,cum_dpid,cum_mcuid,cum_level,cum_isdepart,cum_gradediff) 
select @cmpid,dp_mcuid1,dp_id,dp_mcuid5,@level,dp_isdepart,@level-1
from mps_depart with(nolock)  
where dp_cmpid=@cmpid and dp_mcuid1>0 and dp_mcuid5>0 and not exists 
  (select 1 from mps_employee_manager with(nolock) 
  where cum_cuid=dp_mcuid1 and cum_dpid=dp_id and dp_mcuid5=cum_mcuid and cum_level=@level-1)      
  
  set @level = 6
insert into mps_employee_manager(cum_cmpid,cum_cuid,cum_dpid,cum_mcuid,cum_level,cum_isdepart,cum_gradediff) 
select @cmpid,lgu_cuid,dp_id,dp_mcuid6,@level,dp_isdepart,@level
from mps_depart with(nolock) , mps_login_role with(nolock)
where dp_cmpid=@cmpid and dp_id=lgu_dpid and dp_mcuid6>0 and not exists 
  (select 1 from mps_employee_manager with(nolock) 
  where cum_cuid=lgu_cuid and cum_dpid=dp_id and dp_mcuid6=cum_mcuid and cum_level=@level)  

insert into mps_employee_manager(cum_cmpid,cum_cuid,cum_dpid,cum_mcuid,cum_level,cum_isdepart,cum_gradediff) 
select @cmpid,dp_mcuid5,dp_id,dp_mcuid6,@level,dp_isdepart,@level-5
from mps_depart with(nolock)  
where dp_cmpid=@cmpid and dp_mcuid5>0 and dp_mcuid6>0 and not exists 
  (select 1 from mps_employee_manager with(nolock) 
  where cum_cuid=dp_mcuid5 and cum_dpid=dp_id and dp_mcuid6=cum_mcuid and cum_level=@level-5)  

insert into mps_employee_manager(cum_cmpid,cum_cuid,cum_dpid,cum_mcuid,cum_level,cum_isdepart,cum_gradediff) 
select @cmpid,dp_mcuid4,dp_id,dp_mcuid6,@level,dp_isdepart,@level-4
from mps_depart with(nolock)  
where dp_cmpid=@cmpid and dp_mcuid4>0 and dp_mcuid6>0 and not exists 
  (select 1 from mps_employee_manager with(nolock) 
  where cum_cuid=dp_mcuid4 and cum_dpid=dp_id and dp_mcuid6=cum_mcuid and cum_level=@level-4)      

insert into mps_employee_manager(cum_cmpid,cum_cuid,cum_dpid,cum_mcuid,cum_level,cum_isdepart,cum_gradediff)  
select @cmpid,dp_mcuid3,dp_id,dp_mcuid6,@level,dp_isdepart,@level-3
from mps_depart with(nolock)  
where dp_cmpid=@cmpid and dp_mcuid3>0 and dp_mcuid6>0 and not exists 
  (select 1 from mps_employee_manager with(nolock) 
  where cum_cuid=dp_mcuid3 and cum_dpid=dp_id and dp_mcuid6=cum_mcuid and cum_level=@level-3)      

insert into mps_employee_manager(cum_cmpid,cum_cuid,cum_dpid,cum_mcuid,cum_level,cum_isdepart,cum_gradediff) 
select @cmpid,dp_mcuid2,dp_id,dp_mcuid6,@level,dp_isdepart,@level-2
from mps_depart with(nolock)  
where dp_cmpid=@cmpid and dp_mcuid2>0 and dp_mcuid6>0 and not exists 
  (select 1 from mps_employee_manager with(nolock) 
  where cum_cuid=dp_mcuid2 and cum_dpid=dp_id and dp_mcuid6=cum_mcuid and cum_level=@level-2)      

insert into mps_employee_manager(cum_cmpid,cum_cuid,cum_dpid,cum_mcuid,cum_level,cum_isdepart,cum_gradediff) 
select @cmpid,dp_mcuid1,dp_id,dp_mcuid6,@level,dp_isdepart,@level-1
from mps_depart with(nolock)  
where dp_cmpid=@cmpid and dp_mcuid1>0 and dp_mcuid6>0 and not exists 
  (select 1 from mps_employee_manager with(nolock) 
  where cum_cuid=dp_mcuid1 and cum_dpid=dp_id and dp_mcuid6=cum_mcuid and cum_level=@level-1)      
  
set @level = 7
insert into mps_employee_manager(cum_cmpid,cum_cuid,cum_dpid,cum_mcuid,cum_level,cum_isdepart,cum_gradediff) 
select @cmpid,lgu_cuid,dp_id,dp_mcuid7,@level,dp_isdepart,@level
from mps_depart with(nolock) , mps_login_role with(nolock)
where dp_cmpid=@cmpid and dp_id=lgu_dpid and dp_mcuid7>0 and not exists 
  (select 1 from mps_employee_manager with(nolock) 
  where cum_cuid=lgu_cuid and cum_dpid=dp_id and dp_mcuid7=cum_mcuid and cum_level=@level)  

insert into mps_employee_manager(cum_cmpid,cum_cuid,cum_dpid,cum_mcuid,cum_level,cum_isdepart,cum_gradediff) 
select @cmpid,dp_mcuid6,dp_id,dp_mcuid7,@level,dp_isdepart,@level-6
from mps_depart with(nolock)  
where dp_cmpid=@cmpid and dp_mcuid6>0 and dp_mcuid7>0 and not exists 
  (select 1 from mps_employee_manager with(nolock) 
  where cum_cuid=dp_mcuid6 and cum_dpid=dp_id and dp_mcuid7=cum_mcuid and cum_level=@level-6)  

insert into mps_employee_manager(cum_cmpid,cum_cuid,cum_dpid,cum_mcuid,cum_level,cum_isdepart,cum_gradediff) 
select @cmpid,dp_mcuid5,dp_id,dp_mcuid7,@level,dp_isdepart,@level-5
from mps_depart with(nolock)  
where dp_cmpid=@cmpid and dp_mcuid5>0 and dp_mcuid7>0 and not exists 
  (select 1 from mps_employee_manager with(nolock) 
  where cum_cuid=dp_mcuid5 and cum_dpid=dp_id and dp_mcuid7=cum_mcuid and cum_level=@level-5)      

insert into mps_employee_manager(cum_cmpid,cum_cuid,cum_dpid,cum_mcuid,cum_level,cum_isdepart,cum_gradediff)  
select @cmpid,dp_mcuid4,dp_id,dp_mcuid7,@level,dp_isdepart,@level-4
from mps_depart with(nolock)  
where dp_cmpid=@cmpid and dp_mcuid4>0 and dp_mcuid7>0 and not exists 
  (select 1 from mps_employee_manager with(nolock) 
  where cum_cuid=dp_mcuid4 and cum_dpid=dp_id and dp_mcuid7=cum_mcuid and cum_level=@level-4)      

insert into mps_employee_manager(cum_cmpid,cum_cuid,cum_dpid,cum_mcuid,cum_level,cum_isdepart,cum_gradediff) 
select @cmpid,dp_mcuid3,dp_id,dp_mcuid7,@level,dp_isdepart,@level-3
from mps_depart with(nolock)  
where dp_cmpid=@cmpid and dp_mcuid3>0 and dp_mcuid7>0 and not exists 
  (select 1 from mps_employee_manager with(nolock) 
  where cum_cuid=dp_mcuid3 and cum_dpid=dp_id and dp_mcuid7=cum_mcuid and cum_level=@level-3)      

insert into mps_employee_manager(cum_cmpid,cum_cuid,cum_dpid,cum_mcuid,cum_level,cum_isdepart,cum_gradediff) 
select @cmpid,dp_mcuid2,dp_id,dp_mcuid7,@level,dp_isdepart,@level-2
from mps_depart with(nolock)  
where dp_cmpid=@cmpid and dp_mcuid2>0 and dp_mcuid7>0 and not exists 
  (select 1 from mps_employee_manager with(nolock) 
  where cum_cuid=dp_mcuid2 and cum_dpid=dp_id and dp_mcuid7=cum_mcuid and cum_level=@level-2) 
  
insert into mps_employee_manager(cum_cmpid,cum_cuid,cum_dpid,cum_mcuid,cum_level,cum_isdepart,cum_gradediff) 
select @cmpid,dp_mcuid1,dp_id,dp_mcuid7,@level,dp_isdepart,@level-1
from mps_depart with(nolock)  
where dp_cmpid=@cmpid and dp_mcuid1>0 and dp_mcuid7>0 and not exists 
  (select 1 from mps_employee_manager with(nolock) 
  where cum_cuid=dp_mcuid1 and cum_dpid=dp_id and dp_mcuid7=cum_mcuid and cum_level=@level-1) 


set @level = 8
insert into mps_employee_manager(cum_cmpid,cum_cuid,cum_dpid,cum_mcuid,cum_level,cum_isdepart,cum_gradediff) 
select @cmpid,lgu_cuid,dp_id,dp_mcuid8,@level,dp_isdepart,@level
from mps_depart with(nolock) , mps_login_role with(nolock)
where dp_cmpid=@cmpid and dp_id=lgu_dpid and dp_mcuid8>0 and not exists 
  (select 1 from mps_employee_manager with(nolock) 
  where cum_cuid=lgu_cuid and cum_dpid=dp_id and dp_mcuid8=cum_mcuid and cum_level=@level)  

insert into mps_employee_manager(cum_cmpid,cum_cuid,cum_dpid,cum_mcuid,cum_level,cum_isdepart,cum_gradediff) 
select @cmpid,dp_mcuid1,dp_id,dp_mcuid8,@level,dp_isdepart,@level-1
from mps_depart with(nolock)  
where dp_cmpid=@cmpid and dp_mcuid1>0 and dp_mcuid8>0 and not exists 
  (select 1 from mps_employee_manager with(nolock) 
  where cum_cuid=dp_mcuid1 and cum_dpid=dp_id and dp_mcuid8=cum_mcuid and cum_level=@level-1)  

insert into mps_employee_manager(cum_cmpid,cum_cuid,cum_dpid,cum_mcuid,cum_level,cum_isdepart,cum_gradediff) 
select @cmpid,dp_mcuid5,dp_id,dp_mcuid8,@level,dp_isdepart,@level-2
from mps_depart with(nolock)  
where dp_cmpid=@cmpid and dp_mcuid2>0 and dp_mcuid8>0 and not exists 
  (select 1 from mps_employee_manager with(nolock) 
  where cum_cuid=dp_mcuid2 and cum_dpid=dp_id and dp_mcuid8=cum_mcuid and cum_level=@level-2)      

insert into mps_employee_manager(cum_cmpid,cum_cuid,cum_dpid,cum_mcuid,cum_level,cum_isdepart,cum_gradediff)  
select @cmpid,dp_mcuid3,dp_id,dp_mcuid8,@level,dp_isdepart,@level-3
from mps_depart with(nolock)  
where dp_cmpid=@cmpid and dp_mcuid3>0 and dp_mcuid8>0 and not exists 
  (select 1 from mps_employee_manager with(nolock) 
  where cum_cuid=dp_mcuid3 and cum_dpid=dp_id and dp_mcuid8=cum_mcuid and cum_level=@level-3)      

insert into mps_employee_manager(cum_cmpid,cum_cuid,cum_dpid,cum_mcuid,cum_level,cum_isdepart,cum_gradediff) 
select @cmpid,dp_mcuid4,dp_id,dp_mcuid8,@level,dp_isdepart,@level-4
from mps_depart with(nolock)  
where dp_cmpid=@cmpid and dp_mcuid4>0 and dp_mcuid8>0 and not exists 
  (select 1 from mps_employee_manager with(nolock) 
  where cum_cuid=dp_mcuid4 and cum_dpid=dp_id and dp_mcuid8=cum_mcuid and cum_level=@level-4)      

insert into mps_employee_manager(cum_cmpid,cum_cuid,cum_dpid,cum_mcuid,cum_level,cum_isdepart,cum_gradediff) 
select @cmpid,dp_mcuid5,dp_id,dp_mcuid8,@level,dp_isdepart,@level-5
from mps_depart with(nolock)  
where dp_cmpid=@cmpid and dp_mcuid5>0 and dp_mcuid8>0 and not exists 
  (select 1 from mps_employee_manager with(nolock) 
  where cum_cuid=dp_mcuid5 and cum_dpid=dp_id and dp_mcuid8=cum_mcuid and cum_level=@level-5) 
  
insert into mps_employee_manager(cum_cmpid,cum_cuid,cum_dpid,cum_mcuid,cum_level,cum_isdepart,cum_gradediff) 
select @cmpid,dp_mcuid6,dp_id,dp_mcuid8,@level,dp_isdepart,@level-6
from mps_depart with(nolock)  
where dp_cmpid=@cmpid and dp_mcuid6>0 and dp_mcuid8>0 and not exists 
  (select 1 from mps_employee_manager with(nolock) 
  where cum_cuid=dp_mcuid6 and cum_dpid=dp_id and dp_mcuid8=cum_mcuid and cum_level=@level-6)   
  
insert into mps_employee_manager(cum_cmpid,cum_cuid,cum_dpid,cum_mcuid,cum_level,cum_isdepart,cum_gradediff) 
select @cmpid,dp_mcuid7,dp_id,dp_mcuid8,@level,dp_isdepart,@level-7
from mps_depart with(nolock)  
where dp_cmpid=@cmpid and dp_mcuid7>0 and dp_mcuid8>0 and not exists 
  (select 1 from mps_employee_manager with(nolock) 
  where cum_cuid=dp_mcuid7 and cum_dpid=dp_id and dp_mcuid8=cum_mcuid and cum_level=@level-7)   
  
set @level = 9
insert into mps_employee_manager(cum_cmpid,cum_cuid,cum_dpid,cum_mcuid,cum_level,cum_isdepart,cum_gradediff) 
select @cmpid,lgu_cuid,dp_id,dp_mcuid9,@level,dp_isdepart,@level
from mps_depart with(nolock) , mps_login_role with(nolock)
where dp_cmpid=@cmpid and dp_id=lgu_dpid and dp_mcuid9>0 and not exists 
  (select 1 from mps_employee_manager with(nolock) 
  where cum_cuid=lgu_cuid and cum_dpid=dp_id and dp_mcuid9=cum_mcuid and cum_level=@level)  

insert into mps_employee_manager(cum_cmpid,cum_cuid,cum_dpid,cum_mcuid,cum_level,cum_isdepart,cum_gradediff) 
select @cmpid,dp_mcuid1,dp_id,dp_mcuid9,@level,dp_isdepart,@level-1
from mps_depart with(nolock)  
where dp_cmpid=@cmpid and dp_mcuid1>0 and dp_mcuid9>0 and not exists 
  (select 1 from mps_employee_manager with(nolock) 
  where cum_cuid=dp_mcuid1 and cum_dpid=dp_id and dp_mcuid9=cum_mcuid and cum_level=@level-1)  

insert into mps_employee_manager(cum_cmpid,cum_cuid,cum_dpid,cum_mcuid,cum_level,cum_isdepart,cum_gradediff) 
select @cmpid,dp_mcuid5,dp_id,dp_mcuid9,@level,dp_isdepart,@level-2
from mps_depart with(nolock)  
where dp_cmpid=@cmpid and dp_mcuid2>0 and dp_mcuid9>0 and not exists 
  (select 1 from mps_employee_manager with(nolock) 
  where cum_cuid=dp_mcuid2 and cum_dpid=dp_id and dp_mcuid9=cum_mcuid and cum_level=@level-2)      

insert into mps_employee_manager(cum_cmpid,cum_cuid,cum_dpid,cum_mcuid,cum_level,cum_isdepart,cum_gradediff)  
select @cmpid,dp_mcuid3,dp_id,dp_mcuid9,@level,dp_isdepart,@level-3
from mps_depart with(nolock)  
where dp_cmpid=@cmpid and dp_mcuid3>0 and dp_mcuid9>0 and not exists 
  (select 1 from mps_employee_manager with(nolock) 
  where cum_cuid=dp_mcuid3 and cum_dpid=dp_id and dp_mcuid9=cum_mcuid and cum_level=@level-3)      

insert into mps_employee_manager(cum_cmpid,cum_cuid,cum_dpid,cum_mcuid,cum_level,cum_isdepart,cum_gradediff) 
select @cmpid,dp_mcuid4,dp_id,dp_mcuid9,@level,dp_isdepart,@level-4
from mps_depart with(nolock)  
where dp_cmpid=@cmpid and dp_mcuid4>0 and dp_mcuid9>0 and not exists 
  (select 1 from mps_employee_manager with(nolock) 
  where cum_cuid=dp_mcuid4 and cum_dpid=dp_id and dp_mcuid9=cum_mcuid and cum_level=@level-4)      

insert into mps_employee_manager(cum_cmpid,cum_cuid,cum_dpid,cum_mcuid,cum_level,cum_isdepart,cum_gradediff) 
select @cmpid,dp_mcuid5,dp_id,dp_mcuid9,@level,dp_isdepart,@level-5
from mps_depart with(nolock)  
where dp_cmpid=@cmpid and dp_mcuid5>0 and dp_mcuid9>0 and not exists 
  (select 1 from mps_employee_manager with(nolock) 
  where cum_cuid=dp_mcuid5 and cum_dpid=dp_id and dp_mcuid9=cum_mcuid and cum_level=@level-5) 
  
insert into mps_employee_manager(cum_cmpid,cum_cuid,cum_dpid,cum_mcuid,cum_level,cum_isdepart,cum_gradediff) 
select @cmpid,dp_mcuid6,dp_id,dp_mcuid9,@level,dp_isdepart,@level-6
from mps_depart with(nolock)  
where dp_cmpid=@cmpid and dp_mcuid6>0 and dp_mcuid9>0 and not exists 
  (select 1 from mps_employee_manager with(nolock) 
  where cum_cuid=dp_mcuid6 and cum_dpid=dp_id and dp_mcuid9=cum_mcuid and cum_level=@level-6)   
  
insert into mps_employee_manager(cum_cmpid,cum_cuid,cum_dpid,cum_mcuid,cum_level,cum_isdepart,cum_gradediff) 
select @cmpid,dp_mcuid7,dp_id,dp_mcuid9,@level,dp_isdepart,@level-7
from mps_depart with(nolock)  
where dp_cmpid=@cmpid and dp_mcuid7>0 and dp_mcuid9>0 and not exists 
  (select 1 from mps_employee_manager with(nolock) 
  where cum_cuid=dp_mcuid7 and cum_dpid=dp_id and dp_mcuid9=cum_mcuid and cum_level=@level-7)   
  
insert into mps_employee_manager(cum_cmpid,cum_cuid,cum_dpid,cum_mcuid,cum_level,cum_isdepart,cum_gradediff) 
select @cmpid,dp_mcuid8,dp_id,dp_mcuid9,@level,dp_isdepart,@level-8
from mps_depart with(nolock)  
where dp_cmpid=@cmpid and dp_mcuid8>0 and dp_mcuid9>0 and not exists 
  (select 1 from mps_employee_manager with(nolock) 
  where cum_cuid=dp_mcuid8 and cum_dpid=dp_id and dp_mcuid9=cum_mcuid and cum_level=@level-8)   
  
  
set @level = 10
insert into mps_employee_manager(cum_cmpid,cum_cuid,cum_dpid,cum_mcuid,cum_level,cum_isdepart,cum_gradediff) 
select @cmpid,lgu_cuid,dp_id,dp_mcuid10,@level,dp_isdepart,@level
from mps_depart with(nolock) , mps_login_role with(nolock)
where dp_cmpid=@cmpid and dp_id=lgu_dpid and dp_mcuid10>0 and not exists 
  (select 1 from mps_employee_manager with(nolock) 
  where cum_cuid=lgu_cuid and cum_dpid=dp_id and dp_mcuid10=cum_mcuid and cum_level=@level)  

insert into mps_employee_manager(cum_cmpid,cum_cuid,cum_dpid,cum_mcuid,cum_level,cum_isdepart,cum_gradediff) 
select @cmpid,dp_mcuid1,dp_id,dp_mcuid10,@level,dp_isdepart,@level-1
from mps_depart with(nolock)  
where dp_cmpid=@cmpid and dp_mcuid1>0 and dp_mcuid10>0 and not exists 
  (select 1 from mps_employee_manager with(nolock) 
  where cum_cuid=dp_mcuid1 and cum_dpid=dp_id and dp_mcuid10=cum_mcuid and cum_level=@level-1)  

insert into mps_employee_manager(cum_cmpid,cum_cuid,cum_dpid,cum_mcuid,cum_level,cum_isdepart,cum_gradediff) 
select @cmpid,dp_mcuid5,dp_id,dp_mcuid10,@level,dp_isdepart,@level-2
from mps_depart with(nolock)  
where dp_cmpid=@cmpid and dp_mcuid2>0 and dp_mcuid10>0 and not exists 
  (select 1 from mps_employee_manager with(nolock) 
  where cum_cuid=dp_mcuid2 and cum_dpid=dp_id and dp_mcuid10=cum_mcuid and cum_level=@level-2)      

insert into mps_employee_manager(cum_cmpid,cum_cuid,cum_dpid,cum_mcuid,cum_level,cum_isdepart,cum_gradediff)  
select @cmpid,dp_mcuid3,dp_id,dp_mcuid10,@level,dp_isdepart,@level-3
from mps_depart with(nolock)  
where dp_cmpid=@cmpid and dp_mcuid3>0 and dp_mcuid10>0 and not exists 
  (select 1 from mps_employee_manager with(nolock) 
  where cum_cuid=dp_mcuid3 and cum_dpid=dp_id and dp_mcuid10=cum_mcuid and cum_level=@level-3)      

insert into mps_employee_manager(cum_cmpid,cum_cuid,cum_dpid,cum_mcuid,cum_level,cum_isdepart,cum_gradediff) 
select @cmpid,dp_mcuid4,dp_id,dp_mcuid10,@level,dp_isdepart,@level-4
from mps_depart with(nolock)  
where dp_cmpid=@cmpid and dp_mcuid4>0 and dp_mcuid10>0 and not exists 
  (select 1 from mps_employee_manager with(nolock) 
  where cum_cuid=dp_mcuid4 and cum_dpid=dp_id and dp_mcuid10=cum_mcuid and cum_level=@level-4)      

insert into mps_employee_manager(cum_cmpid,cum_cuid,cum_dpid,cum_mcuid,cum_level,cum_isdepart,cum_gradediff) 
select @cmpid,dp_mcuid5,dp_id,dp_mcuid10,@level,dp_isdepart,@level-5
from mps_depart with(nolock)  
where dp_cmpid=@cmpid and dp_mcuid5>0 and dp_mcuid10>0 and not exists 
  (select 1 from mps_employee_manager with(nolock) 
  where cum_cuid=dp_mcuid5 and cum_dpid=dp_id and dp_mcuid10=cum_mcuid and cum_level=@level-5) 
  
insert into mps_employee_manager(cum_cmpid,cum_cuid,cum_dpid,cum_mcuid,cum_level,cum_isdepart,cum_gradediff) 
select @cmpid,dp_mcuid6,dp_id,dp_mcuid10,@level,dp_isdepart,@level-6
from mps_depart with(nolock)  
where dp_cmpid=@cmpid and dp_mcuid6>0 and dp_mcuid10>0 and not exists 
  (select 1 from mps_employee_manager with(nolock) 
  where cum_cuid=dp_mcuid6 and cum_dpid=dp_id and dp_mcuid10=cum_mcuid and cum_level=@level-6)   
  
insert into mps_employee_manager(cum_cmpid,cum_cuid,cum_dpid,cum_mcuid,cum_level,cum_isdepart,cum_gradediff) 
select @cmpid,dp_mcuid7,dp_id,dp_mcuid10,@level,dp_isdepart,@level-7
from mps_depart with(nolock)  
where dp_cmpid=@cmpid and dp_mcuid10>0 and dp_mcuid9>0 and not exists 
  (select 1 from mps_employee_manager with(nolock) 
  where cum_cuid=dp_mcuid7 and cum_dpid=dp_id and dp_mcuid10=cum_mcuid and cum_level=@level-7)   
  
insert into mps_employee_manager(cum_cmpid,cum_cuid,cum_dpid,cum_mcuid,cum_level,cum_isdepart,cum_gradediff) 
select @cmpid,dp_mcuid8,dp_id,dp_mcuid10,@level,dp_isdepart,@level-8
from mps_depart with(nolock)  
where dp_cmpid=@cmpid and dp_mcuid8>0 and dp_mcuid10>0 and not exists 
  (select 1 from mps_employee_manager with(nolock) 
  where cum_cuid=dp_mcuid8 and cum_dpid=dp_id and dp_mcuid10=cum_mcuid and cum_level=@level-8) 
  
insert into mps_employee_manager(cum_cmpid,cum_cuid,cum_dpid,cum_mcuid,cum_level,cum_isdepart,cum_gradediff) 
select @cmpid,dp_mcuid9,dp_id,dp_mcuid10,@level,dp_isdepart,@level-9
from mps_depart with(nolock)  
where dp_cmpid=@cmpid and dp_mcuid9>0 and dp_mcuid10>0 and not exists 
  (select 1 from mps_employee_manager with(nolock) 
  where cum_cuid=dp_mcuid9 and cum_dpid=dp_id and dp_mcuid10=cum_mcuid and cum_level=@level-9) 
 
  
end