IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_customer1_query_simple]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_customer1_query_simple]
GO
CREATE  procedure mps_customer1_query_simple(
@cmpid int,@branchid int,@userdpid int,@userotid int ,@useruid int ,@onlyme int ,@pagesize int , @pagecount int , @pageobjids varchar(8000),@ctid int , @date1 datetime ,@date2 datetime ,@key1 nvarchar(50),@key2 nvarchar(50),@key3 nvarchar(50)) 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_customer1 a,    @pobjs
    where cc_id=ordid
    order by nid  
    select a.* from mps_customer1_users a , @pobjs where ccp_ccid=ordid and ccp_cuid=@useruid
    return 
    end  
    
if (@key1='') set @key1='%' 
if (charindex('%',@key1)<=0 and @key1>'') set @key1='%'+@key1+'%'

if (@key2='') set @key2='%' 
if (charindex('%',@key2)<=0 and @key2>'') set @key2='%'+@key2+'%'

if (@key3='') set @key3='%' 
if (charindex('%',@key3)<=0 and @key3>'') set @key3='%'+@key3+'%'

if (@onlyme = 0) 
begin
if (@pagesize=10000)  -- 下载
begin
select a.* from mps_customer1 a,mps_customer1_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 >= @date1 and cc_updated <= @date2 ) and 
(
 (cc_code like @key1 and cc_code like @key2 and cc_code like @key3 ) or 
 (cc_name like @key1 and cc_name like @key2 and cc_name like @key3 ) or 
 (cc_mobile like @key1 and cc_mobile like @key2 and cc_mobile like @key3 ) or 
 (cc_company like @key1 and cc_company like @key2 and cc_company like @key3 ) or 
 (cc_contact like @key1 and cc_contact like @key2 and cc_contact like @key3 ) or 
 (cc_identno like @key1 and cc_identno like @key2 and cc_identno like @key3 ) )
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_customer1 a,mps_customer1_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 >= @date1 and cc_updated <= @date2 ) and 
(
 (cc_code like @key1 and cc_code like @key2 and cc_code like @key3 ) or 
 (cc_name like @key1 and cc_name like @key2 and cc_name like @key3 ) or 
 (cc_mobile like @key1 and cc_mobile like @key2 and cc_mobile like @key3 ) or 
 (cc_company like @key1 and cc_company like @key2 and cc_company like @key3 ) or 
 (cc_contact like @key1 and cc_contact like @key2 and cc_contact like @key3 ) or 
 (cc_identno like @key1 and cc_identno like @key2 and cc_identno like @key3 ) )
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_customer1 a,mps_customer1_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 >= @date1 and cc_updated <= @date2 ) and 
(
 (cc_code like @key1 and cc_code like @key2 and cc_code like @key3 ) or 
 (cc_name like @key1 and cc_name like @key2 and cc_name like @key3 ) or 
 (cc_mobile like @key1 and cc_mobile like @key2 and cc_mobile like @key3 ) or 
 (cc_company like @key1 and cc_company like @key2 and cc_company like @key3 ) or 
 (cc_contact like @key1 and cc_contact like @key2 and cc_contact like @key3 ) or 
 (cc_identno like @key1 and cc_identno like @key2 and cc_identno like @key3 ) )

-- 返回所有ID
insert into @pobjs(ordid) select top(@amount) cc_id from mps_customer1 a ,mps_customer1_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 >= @date1 and cc_updated <= @date2 ) and 
(
 (cc_code like @key1 and cc_code like @key2 and cc_code like @key3 ) or 
 (cc_name like @key1 and cc_name like @key2 and cc_name like @key3 ) or 
 (cc_mobile like @key1 and cc_mobile like @key2 and cc_mobile like @key3 ) or 
 (cc_company like @key1 and cc_company like @key2 and cc_company like @key3 ) or 
 (cc_contact like @key1 and cc_contact like @key2 and cc_contact like @key3 ) or 
 (cc_identno like @key1 and cc_identno like @key2 and cc_identno like @key3 ) )
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_customer1 a,@pobjs 
where a.cc_id=ordid 
order by nid 
-- 返回权限 
select a.* from mps_customer1_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_customer1 a,mps_customer1_users 
where a.cc_id=ccp_ccid 
and ccp_type='cstview' 
and a.cc_ctid=@ctid 
and ccp_cuid=@useruid 
and 
(cc_updated >= @date1 and cc_updated <= @date2 ) and 
(
 (cc_code like @key1 and cc_code like @key2 and cc_code like @key3 ) or 
 (cc_name like @key1 and cc_name like @key2 and cc_name like @key3 ) or 
 (cc_mobile like @key1 and cc_mobile like @key2 and cc_mobile like @key3 ) or 
 (cc_company like @key1 and cc_company like @key2 and cc_company like @key3 ) or 
 (cc_contact like @key1 and cc_contact like @key2 and cc_contact like @key3 ) or 
 (cc_identno like @key1 and cc_identno like @key2 and cc_identno like @key3 ) )
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_customer1 a,mps_customer1_users 
where a.cc_id=ccp_ccid 
and ccp_type='cstview' 
and a.cc_ctid=@ctid 
and ccp_cuid=@useruid 
and 
(cc_updated >= @date1 and cc_updated <= @date2 ) and 
(
 (cc_code like @key1 and cc_code like @key2 and cc_code like @key3 ) or 
 (cc_name like @key1 and cc_name like @key2 and cc_name like @key3 ) or 
 (cc_mobile like @key1 and cc_mobile like @key2 and cc_mobile like @key3 ) or 
 (cc_company like @key1 and cc_company like @key2 and cc_company like @key3 ) or 
 (cc_contact like @key1 and cc_contact like @key2 and cc_contact like @key3 ) or 
 (cc_identno like @key1 and cc_identno like @key2 and cc_identno like @key3 ) )
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_customer1 a,mps_customer1_users 
where a.cc_id=ccp_ccid 
and ccp_type='cstview' 
and a.cc_ctid=@ctid 
and ccp_cuid=@useruid 
and 
(cc_updated >= @date1 and cc_updated <= @date2 ) and 
(
 (cc_code like @key1 and cc_code like @key2 and cc_code like @key3 ) or 
 (cc_name like @key1 and cc_name like @key2 and cc_name like @key3 ) or 
 (cc_mobile like @key1 and cc_mobile like @key2 and cc_mobile like @key3 ) or 
 (cc_company like @key1 and cc_company like @key2 and cc_company like @key3 ) or 
 (cc_contact like @key1 and cc_contact like @key2 and cc_contact like @key3 ) or 
 (cc_identno like @key1 and cc_identno like @key2 and cc_identno like @key3 ) )

-- 返回所有ID
insert into @pobjs(ordid) select top(@amount) cc_id from mps_customer1 a,mps_customer1_users 
where a.cc_id=ccp_ccid 
and ccp_type='cstview' 
and a.cc_ctid=@ctid 
and ccp_cuid=@useruid
and 
(cc_updated >= @date1 and cc_updated <= @date2 ) and 
(
 (cc_code like @key1 and cc_code like @key2 and cc_code like @key3 ) or 
 (cc_name like @key1 and cc_name like @key2 and cc_name like @key3 ) or 
 (cc_mobile like @key1 and cc_mobile like @key2 and cc_mobile like @key3 ) or 
 (cc_company like @key1 and cc_company like @key2 and cc_company like @key3 ) or 
 (cc_contact like @key1 and cc_contact like @key2 and cc_contact like @key3 ) or 
 (cc_identno like @key1 and cc_identno like @key2 and cc_identno like @key3 ) )
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_customer1 a,@pobjs 
where a.cc_id=ordid 
order by nid 
-- 返回权限 
select a.* from mps_customer1_users a , @pobjs 
where ccp_ccid=ordid 
and nid<=@pagesize 
and ccp_cuid=@useruid 
end 
end
set nocount off end