IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_g_1branch_shop_sql]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_g_1branch_shop_sql]
GO
CREATE procedure [dbo].[mps_g_1branch_shop_sql](
@cmpid int,
@mybcid int ,
@key nvarchar(100),
@dkey1 nvarchar(50),
@dkey2 nvarchar(50),
@dkey3 nvarchar(50) ,
@bizsid int,
@extparam1 nvarchar(50),
@extparam2 nvarchar(50),
@extparam3 nvarchar(50),
@extparam4 nvarchar(50),
@extparam5 nvarchar(50),
@extparam6 nvarchar(50),
@extparam7 nvarchar(50),
@extparam8 nvarchar(50),
@extparam9 nvarchar(50) 
)
as 
begin
-- 返回部门名称
set nocount on

declare @sql1 nvarchar(4000) 
declare @sql nvarchar(max) 
select @sql1=bizs_condition from  mps_biztype_source with(nolock)  where bizs_id=@bizsid 
set @sql1 = ISNULL(@sql1,'') 
if (@sql1>'') set @sql1 = ' and ('+@sql1+') '

set @sql = '
select dp_id ,dp_dpstdid, dp_depart,dp_fullname ,dp_code ,dp_display 
from mps_depart  with(nolock) 
where dp_cmpid=@cmpid and (dp_bcid=@mybcid or @mybcid=0) and dp_fullname>'''' and dp_depart>''''  and dp_isdepart>0 and 
(
(dp_depart like ''%''+@key+''%'' ) or 
(dp_province like ''%''+@key+''%'' ) or 
(dp_municipal like ''%''+@key+''%'' )or 
(dp_county like ''%''+@key+''%'' ) or 
(dp_location like ''%''+@key+''%'' ) or 
(dp_category1 like ''%''+@key+''%'' ) or 
(dp_category2 like ''%''+@key+''%'' ) or 
(dp_category3 like ''%''+@key+''%'' ) or 
(dp_note like ''%''+@key+''%'' ) or 
(dp_status like ''%''+@key+''%'' ) or 
(dp_manager1 like ''%''+@key+''%'' ) or 
(dp_manager2 like ''%''+@key+''%'' ) or 
(dp_manager3 like ''%''+@key+''%'' ) or 
(dp_manager3 like ''%''+@key+''%'' ) or 
(dp_manager4 like ''%''+@key+''%'' ) or 
(dp_manager6 like ''%''+@key+''%'' ) 
) 
and  (dp_province=@dkey1 or @dkey1 = '''')
and  (dp_municipal=@dkey2 or @dkey2 = '''')
and  (dp_county=@dkey3 or @dkey3 = '''') '+@sql1+
'
order by   dp_order, dp_level  '

exec sp_executesql @sql,
N'@cmpid int,
@mybcid int ,
@key nvarchar(100),
@dkey1 nvarchar(50),
@dkey2 nvarchar(50),
@dkey3 nvarchar(50),
@extparam1 nvarchar(50),
@extparam2 nvarchar(50),
@extparam3 nvarchar(50),
@extparam4 nvarchar(50),
@extparam5 nvarchar(50),
@extparam6 nvarchar(50),
@extparam7 nvarchar(50),
@extparam8 nvarchar(50),
@extparam9 nvarchar(50)',
@cmpid ,
@mybcid,
@key ,
@dkey1,
@dkey2,
@dkey3,
@extparam1 ,
@extparam2 ,
@extparam3 ,
@extparam4 ,
@extparam5 ,
@extparam6 ,
@extparam7 ,
@extparam8 ,
@extparam9

set nocount off
end