IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_g_departsusers]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_g_departsusers]
GO
CREATE procedure [dbo].[mps_g_departsusers](
@cmpid int ,
@dpids varchar(2000) ,
@key nvarchar(100) 
)
as
begin
set nocount on
declare @t table(dpid int) 
declare @i int,@curInd int,@strlen int,@tmps varchar(100) ,@srcs varchar(2000)
set @srcs = @dpids 
select @curInd=1,@strlen=LEN(@srcs) 
while (@curInd<=@strlen) 
begin 
select @i=charindex(',',@srcs,@curInd) 
if (@i>0) 
begin 
select @tmps=SUBSTRING(@srcs,@curInd,@i-@curInd) 
set @curInd=@i+1 
end else if (@curInd<=@strlen) 
begin 
select @tmps=SUBSTRING(@srcs,@curInd,@strlen-@curInd+1) 
set @curInd=@strlen+1 
end 
if (@tmps>'') insert into @t(dpid) values(convert(bigint,@tmps)) 
end
if (@key='') 
begin
select * from mps_employees , @t 
where cu_cmpid=@cmpid and cu_dpid=dpid and cu_use=0   
order by cu_dpid , cu_name 
end else 
begin
select * from mps_employees , @t 
where cu_cmpid=@cmpid and cu_dpid=dpid 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+'%'  
)
order by cu_dpid , cu_name 
end
set nocount off
end