IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_ding_archmap]') AND type = 'V')
DROP VIEW [dbo].[mps_ding_archmap]
GO
CREATE view mps_ding_archmap
as
select  dp_cmpid,dp_id,ding_bcid dp_bcid,dp_depart
from
(
select (select bc_dingid from mps_groupbranch where bc_id=_raw.dp_bcid) ding_bcid,* from mps_depart _raw 
where dp_fdpid=0 and dp_depart is not null and dp_depart!='' and dp_dingid=0 
union
select (select dp_dingid from mps_depart where dp_id=_raw.dp_fdpid) ding_bcid,* from mps_depart _raw 
where dp_dingid=0  and dp_fdpid!=0

)  A
where ding_bcid!=0