IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_g_titlesusers_sql]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_g_titlesusers_sql]
GO

CREATE procedure [dbo].[mps_g_titlesusers_sql](
@cmpid int ,
@myotid int ,
@key nvarchar(100) ,
@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 where bizs_id=@bizsid 
set @sql1 = ISNULL(@sql1,'') 
if (@sql1>'') set @sql1 = ' and ('+@sql1+') '


if (@key='') 
begin
set @sql = '
select * from mps_employee  
where cu_cmpid=@cmpid and cu_otid=@myotid and cu_use=0   '+@sql1 +' 
order by cu_dpid , cu_name '
end else 
begin
set @sql = '
select * from mps_employee  
where cu_cmpid=@cmpid and cu_otid=@myotid and cu_use=0   and 
(cu_fastkey like ''%''+@key+''%'' or 
 cu_code like ''%''+@key+''%'' or 
 cu_name like ''%''+@key+''%'' or 
 cu_mobile like ''%''+@key+''%'' or 
 cu_contact like ''%''+@key+''%'' or 
 cu_position like ''%''+@key+''%'' or 
 cu_identno like ''%''+@key+''%'' or 
 cu_email like ''%''+@key+''%''  
) '+@sql1 +' 
order by cu_dpid , cu_name '
end
exec sp_executesql @sql,
N'@cmpid int ,
@myotid int ,
@key nvarchar(100) ,
@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,
@myotid,
@key  ,
@extparam1 ,
@extparam2 ,
@extparam3 ,
@extparam4 ,
@extparam5 ,
@extparam6 ,
@extparam7 ,
@extparam8 ,
@extparam9
set nocount off
end