IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_customer3_query]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_customer3_query]
GO
CREATE  procedure mps_customer3_query(
@cmpid int,@branchid int,@userdpid int,@userotid int ,@useruid int ,@onlyme int ,@pagesize int , @pagecount int , @pageobjids varchar(8000),@ctid int , @cc_updated_min datetime,@cc_updated_max datetime,@cc_code_min varchar(50), @cc_code_max varchar(50),@cc_name_min nvarchar(50), @cc_name_max nvarchar(50),@cc_mobile_min varchar(18), @cc_mobile_max varchar(18),@cc_company_min nvarchar(50), @cc_company_max nvarchar(50),@cc_contact_min varchar(50), @cc_contact_max varchar(50),@cc_identno_min varchar(20), @cc_identno_max varchar(20)) as begin     set nocount on 
    
    declare @nonedate datetime 
    set @nonedate = CONVERT(datetime,'1910-1-1') 
    declare @s1 varchar(8000) 
    declare @s2 varchar(100) 
    declare @i int 
    declare @amount int 
    select @amount = @pagecount*@pagesize 
    declare @pobjs table(nid int identity(1, 1), ordid bigint) 
    set @s1 = @pageobjids  
    while (@s1>'') 
    begin 
    set @i = charindex(',', @s1) 
    if (@i>0) 
    begin 
    set @s2 = substring(@s1,1,@i-1) 
    set @s1 = substring(@s1,@i+1,len(@s1)-@i) 
    end else  
    begin 
    set @s2 = @s1 
    set @s1 = '' 
    end  
    insert into @pobjs(ordid) values(convert(bigint,@s2)) 
    end 
    if (exists (select top 1 * from @pobjs)) 
    begin 
    select a.* from mps_customer3 a,    @pobjs
    where cc_id=ordid
    order by nid  
    select a.* from mps_customer3_users a , @pobjs where ccp_ccid=ordid and ccp_cuid=@useruid
    return 
    end 

                if (@cc_updated_min is null or @cc_updated_min = @nonedate) set @cc_updated_min = @nonedate 
                
if (@cc_updated_max is null or @cc_updated_max = @nonedate) set @cc_updated_max =  '2299-12-30 23:59:59'
else set @cc_updated_max = @cc_updated_max+ '23:59:59'
                

if (@cc_code_min is null or @cc_code_min = '_none_') set @cc_code_min = '' 
if (@cc_code_max is null or @cc_code_max = '_none_') set @cc_code_max = '' 

if (@cc_name_min is null or @cc_name_min = '_none_') set @cc_name_min = '' 
if (@cc_name_max is null or @cc_name_max = '_none_') set @cc_name_max = '' 

if (@cc_mobile_min is null or @cc_mobile_min = '_none_') set @cc_mobile_min = '' 
if (@cc_mobile_max is null or @cc_mobile_max = '_none_') set @cc_mobile_max = '' 

if (@cc_company_min is null or @cc_company_min = '_none_') set @cc_company_min = '' 
if (@cc_company_max is null or @cc_company_max = '_none_') set @cc_company_max = '' 

if (@cc_contact_min is null or @cc_contact_min = '_none_') set @cc_contact_min = '' 
if (@cc_contact_max is null or @cc_contact_max = '_none_') set @cc_contact_max = '' 

if (@cc_identno_min is null or @cc_identno_min = '_none_') set @cc_identno_min = '' 
if (@cc_identno_max is null or @cc_identno_max = '_none_') set @cc_identno_max = '' 

if @cc_code_min>'' 
begin 
if (charindex('%',@cc_code_min) = 0 and @cc_code_min!='_empty_') 
 begin
set @cc_code_min = '%'+@cc_code_min+'%';
 end else if (@cc_code_min='_empty_') set @cc_code_min='' 
end else
begin 
set @cc_code_min = '%' 
end
if @cc_code_max>'' 
begin 
if (charindex('%', @cc_code_max) = 0 and @cc_code_max!='_empty_') 
begin
set@cc_code_max = '%'+@cc_code_max+'%';
 end else if (@cc_code_max='_empty_') set @cc_code_max='' 
end else
begin 
set @cc_code_max = '%' 
end

if @cc_name_min>'' 
begin 
if (charindex('%',@cc_name_min) = 0 and @cc_name_min!='_empty_') 
 begin
set @cc_name_min = '%'+@cc_name_min+'%';
 end else if (@cc_name_min='_empty_') set @cc_name_min='' 
end else
begin 
set @cc_name_min = '%' 
end
if @cc_name_max>'' 
begin 
if (charindex('%', @cc_name_max) = 0 and @cc_name_max!='_empty_') 
begin
set@cc_name_max = '%'+@cc_name_max+'%';
 end else if (@cc_name_max='_empty_') set @cc_name_max='' 
end else
begin 
set @cc_name_max = '%' 
end

if @cc_mobile_min>'' 
begin 
if (charindex('%',@cc_mobile_min) = 0 and @cc_mobile_min!='_empty_') 
 begin
set @cc_mobile_min = '%'+@cc_mobile_min+'%';
 end else if (@cc_mobile_min='_empty_') set @cc_mobile_min='' 
end else
begin 
set @cc_mobile_min = '%' 
end
if @cc_mobile_max>'' 
begin 
if (charindex('%', @cc_mobile_max) = 0 and @cc_mobile_max!='_empty_') 
begin
set@cc_mobile_max = '%'+@cc_mobile_max+'%';
 end else if (@cc_mobile_max='_empty_') set @cc_mobile_max='' 
end else
begin 
set @cc_mobile_max = '%' 
end

if @cc_company_min>'' 
begin 
if (charindex('%',@cc_company_min) = 0 and @cc_company_min!='_empty_') 
 begin
set @cc_company_min = '%'+@cc_company_min+'%';
 end else if (@cc_company_min='_empty_') set @cc_company_min='' 
end else
begin 
set @cc_company_min = '%' 
end
if @cc_company_max>'' 
begin 
if (charindex('%', @cc_company_max) = 0 and @cc_company_max!='_empty_') 
begin
set@cc_company_max = '%'+@cc_company_max+'%';
 end else if (@cc_company_max='_empty_') set @cc_company_max='' 
end else
begin 
set @cc_company_max = '%' 
end

if @cc_contact_min>'' 
begin 
if (charindex('%',@cc_contact_min) = 0 and @cc_contact_min!='_empty_') 
 begin
set @cc_contact_min = '%'+@cc_contact_min+'%';
 end else if (@cc_contact_min='_empty_') set @cc_contact_min='' 
end else
begin 
set @cc_contact_min = '%' 
end
if @cc_contact_max>'' 
begin 
if (charindex('%', @cc_contact_max) = 0 and @cc_contact_max!='_empty_') 
begin
set@cc_contact_max = '%'+@cc_contact_max+'%';
 end else if (@cc_contact_max='_empty_') set @cc_contact_max='' 
end else
begin 
set @cc_contact_max = '%' 
end

if @cc_identno_min>'' 
begin 
if (charindex('%',@cc_identno_min) = 0 and @cc_identno_min!='_empty_') 
 begin
set @cc_identno_min = '%'+@cc_identno_min+'%';
 end else if (@cc_identno_min='_empty_') set @cc_identno_min='' 
end else
begin 
set @cc_identno_min = '%' 
end
if @cc_identno_max>'' 
begin 
if (charindex('%', @cc_identno_max) = 0 and @cc_identno_max!='_empty_') 
begin
set@cc_identno_max = '%'+@cc_identno_max+'%';
 end else if (@cc_identno_max='_empty_') set @cc_identno_max='' 
end else
begin 
set @cc_identno_max = '%' 
end
 
    
if (@onlyme = 0) 
begin
if (@pagesize=10000)  -- 下载
begin
select a.* from mps_customer3 a,mps_customer3_users 
where a.cc_id=ccp_ccid 
and ccp_type='cstview' 
and a.cc_ctid=@ctid 
and ccp_cuid=@useruid 
and (a.cc_cuid=@useruid or a.cc_createcuid=@useruid ) 
and cc_updated>=@cc_updated_min 
and cc_updated<=@cc_updated_max 
and cc_code like @cc_code_min 
and cc_code like @cc_code_max 
and cc_name like @cc_name_min 
and cc_name like @cc_name_max 
and cc_mobile like @cc_mobile_min 
and cc_mobile like @cc_mobile_max 
and cc_company like @cc_company_min 
and cc_company like @cc_company_max 
and cc_contact like @cc_contact_min 
and cc_contact like @cc_contact_max 
and cc_identno like @cc_identno_min 
and cc_identno like @cc_identno_max 
order by cc_updated desc,cc_code,cc_name,cc_mobile,cc_company,cc_contact,cc_identno
end else 
if (@pagesize>=100000000)  --  转存到中间表
begin
set @pagesize = @pagesize - 100000000 
if (@pagesize<=0) set @pagesize = 100000000 
delete datalinker where useruid = @useruid 
insert into datalinker(useruid,objid) 
select top(@pagesize) @useruid,a.cc_id  from mps_customer3 a,mps_customer3_users 
where a.cc_id=ccp_ccid 
and ccp_type='cstview' 
and a.cc_ctid=@ctid 
and ccp_cuid=@useruid 
and (a.cc_cuid=@useruid or a.cc_createcuid=@useruid ) 
and cc_updated>=@cc_updated_min 
and cc_updated<=@cc_updated_max 
and cc_code like @cc_code_min 
and cc_code like @cc_code_max 
and cc_name like @cc_name_min 
and cc_name like @cc_name_max 
and cc_mobile like @cc_mobile_min 
and cc_mobile like @cc_mobile_max 
and cc_company like @cc_company_min 
and cc_company like @cc_company_max 
and cc_contact like @cc_contact_min 
and cc_contact like @cc_contact_max 
and cc_identno like @cc_identno_min 
and cc_identno like @cc_identno_max 
order by cc_updated desc,cc_code,cc_name,cc_mobile,cc_company,cc_contact,cc_identno
select count(1) resultcount  from datalinker where useruid=@useruid 
end else 
begin
select count(1) cnt from mps_customer3 a,mps_customer3_users 
where a.cc_id=ccp_ccid 
and ccp_type='cstview' 
and a.cc_ctid=@ctid 
and ccp_cuid=@useruid 
and (a.cc_cuid= @useruid or a.cc_createcuid= @useruid) 
and cc_updated>=@cc_updated_min 
and cc_updated<=@cc_updated_max 
and cc_code like @cc_code_min 
and cc_code like @cc_code_max 
and cc_name like @cc_name_min 
and cc_name like @cc_name_max 
and cc_mobile like @cc_mobile_min 
and cc_mobile like @cc_mobile_max 
and cc_company like @cc_company_min 
and cc_company like @cc_company_max 
and cc_contact like @cc_contact_min 
and cc_contact like @cc_contact_max 
and cc_identno like @cc_identno_min 
and cc_identno like @cc_identno_max 

-- 返回所有ID
insert into @pobjs(ordid) select top(@amount) cc_id from mps_customer3 a ,mps_customer3_users 
where a.cc_id=ccp_ccid 
and ccp_type='cstview' 
and a.cc_ctid=@ctid 
and ccp_cuid=@useruid
and (a.cc_cuid= @useruid or a.cc_createcuid= @useruid) 
and cc_updated>=@cc_updated_min 
and cc_updated<=@cc_updated_max 
and cc_code like @cc_code_min 
and cc_code like @cc_code_max 
and cc_name like @cc_name_min 
and cc_name like @cc_name_max 
and cc_mobile like @cc_mobile_min 
and cc_mobile like @cc_mobile_max 
and cc_company like @cc_company_min 
and cc_company like @cc_company_max 
and cc_contact like @cc_contact_min 
and cc_contact like @cc_contact_max 
and cc_identno like @cc_identno_min 
and cc_identno like @cc_identno_max 
order by cc_updated desc,cc_code,cc_name,cc_mobile,cc_company,cc_contact,cc_identno
select ordid from @pobjs 
order by nid 
-- 返回第一页 
select top(@pagesize) a.* from mps_customer3 a,@pobjs 
where a.cc_id=ordid 
order by nid 
-- 返回权限 
select a.* from mps_customer3_users a , @pobjs 
where ccp_ccid=ordid 
and nid<=@pagesize 
and ccp_cuid=@useruid 
end 
end 
else
begin
if (@pagesize=10000)  -- 下载
begin
select a.* from mps_customer3 a,mps_customer3_users 
where a.cc_id=ccp_ccid 
and ccp_type='cstview' 
and a.cc_ctid=@ctid 
and ccp_cuid=@useruid 
and cc_updated>=@cc_updated_min 
and cc_updated<=@cc_updated_max 
and cc_code like @cc_code_min 
and cc_code like @cc_code_max 
and cc_name like @cc_name_min 
and cc_name like @cc_name_max 
and cc_mobile like @cc_mobile_min 
and cc_mobile like @cc_mobile_max 
and cc_company like @cc_company_min 
and cc_company like @cc_company_max 
and cc_contact like @cc_contact_min 
and cc_contact like @cc_contact_max 
and cc_identno like @cc_identno_min 
and cc_identno like @cc_identno_max 
order by cc_updated desc,cc_code,cc_name,cc_mobile,cc_company,cc_contact,cc_identno
end else 
if (@pagesize>=100000000)  --  转存到中间表
begin
set @pagesize = @pagesize - 100000000 
if (@pagesize<=0) set @pagesize = 100000000 
delete datalinker where useruid = @useruid 
insert into datalinker(useruid,objid) 
select top(@pagesize) @useruid,a.cc_id  from mps_customer3 a,mps_customer3_users 
where a.cc_id=ccp_ccid 
and ccp_type='cstview' 
and a.cc_ctid=@ctid 
and ccp_cuid=@useruid 
and cc_updated>=@cc_updated_min 
and cc_updated<=@cc_updated_max 
and cc_code like @cc_code_min 
and cc_code like @cc_code_max 
and cc_name like @cc_name_min 
and cc_name like @cc_name_max 
and cc_mobile like @cc_mobile_min 
and cc_mobile like @cc_mobile_max 
and cc_company like @cc_company_min 
and cc_company like @cc_company_max 
and cc_contact like @cc_contact_min 
and cc_contact like @cc_contact_max 
and cc_identno like @cc_identno_min 
and cc_identno like @cc_identno_max 
order by cc_updated desc,cc_code,cc_name,cc_mobile,cc_company,cc_contact,cc_identno
select count(1) resultcount  from datalinker where useruid=@useruid 
end else
begin
select count(1) cnt from mps_customer3 a,mps_customer3_users 
where a.cc_id=ccp_ccid 
and ccp_type='cstview' 
and a.cc_ctid=@ctid 
and ccp_cuid=@useruid 
and cc_updated>=@cc_updated_min 
and cc_updated<=@cc_updated_max 
and cc_code like @cc_code_min 
and cc_code like @cc_code_max 
and cc_name like @cc_name_min 
and cc_name like @cc_name_max 
and cc_mobile like @cc_mobile_min 
and cc_mobile like @cc_mobile_max 
and cc_company like @cc_company_min 
and cc_company like @cc_company_max 
and cc_contact like @cc_contact_min 
and cc_contact like @cc_contact_max 
and cc_identno like @cc_identno_min 
and cc_identno like @cc_identno_max 

-- 返回所有ID
insert into @pobjs(ordid) select top(@amount) cc_id from mps_customer3 a,mps_customer3_users 
where a.cc_id=ccp_ccid 
and ccp_type='cstview' 
and a.cc_ctid=@ctid 
and ccp_cuid=@useruid
and cc_updated>=@cc_updated_min 
and cc_updated<=@cc_updated_max 
and cc_code like @cc_code_min 
and cc_code like @cc_code_max 
and cc_name like @cc_name_min 
and cc_name like @cc_name_max 
and cc_mobile like @cc_mobile_min 
and cc_mobile like @cc_mobile_max 
and cc_company like @cc_company_min 
and cc_company like @cc_company_max 
and cc_contact like @cc_contact_min 
and cc_contact like @cc_contact_max 
and cc_identno like @cc_identno_min 
and cc_identno like @cc_identno_max 
order by cc_updated desc,cc_code,cc_name,cc_mobile,cc_company,cc_contact,cc_identno
select ordid from @pobjs 
order by nid 
-- 返回第一页 
select top(@pagesize) a.* from mps_customer3 a,@pobjs 
where a.cc_id=ordid 
order by nid 
-- 返回权限 
select a.* from mps_customer3_users a , @pobjs 
where ccp_ccid=ordid 
and nid<=@pagesize 
and ccp_cuid=@useruid 
end 
end
set nocount off end