IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_g_depart_branch]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_g_depart_branch]
GO
create proc [dbo].[mps_g_depart_branch]
@cmpid int,
@mybcid int,
@keys nvarchar(50)
as
begin
set nocount on
if @keys = '_none_' set @keys = ''
if (charindex('%',@keys)<=0 and @keys>'') set @keys='%'+@keys+'%'

select 
* , 
    (select count(dt_id) from mps_depart_title where dt_dpid=dp_id) dp_titlecount,
(select b.dp_depart from mps_depart  b where b.dp_id=a.dp_fdpid) dp_fdepart 
from mps_depart a   
where dp_cmpid=@cmpid
and dp_bcid=@mybcid 
and dp_isdepart= 0 
and dp_code != ''
and (@keys='' or (dp_code like @keys or dp_depart like @keys or dp_province like @keys or dp_municipal like @keys or dp_phone like @keys))
order by dp_order,dp_id, dp_level

set nocount off
end