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

CREATE procedure [dbo].[customer_0_query](
@cmpid int ,
@branchid int,
@userdpid int,
@userotid int ,
@useruid int ,
@pagesize int , 
@pagecount int , 
@pageobjids varchar(8000),
@cc_date1 datetime ,
@cc_date2 datetime  
) as 
begin
set nocount on
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_customer a , @pobjs where cc_id=ordid order by nid  
select a.* from mps_customer_users a , @pobjs where ccp_ccid=ordid   and ccp_cuid=@useruid
return 
end 

-- 返回总量
select count(1) cnt from mps_customer 

-- 返回所有ID
insert into @pobjs(ordid) select top(@amount) cc_id from mps_customer order by cc_updated desc  
select ordid from @pobjs order by nid 

-- 返回第一页
select top(@pagesize) * from mps_customer order by cc_updated desc 
-- 返回权限
 select a.* from mps_customer_users a , @pobjs where ccp_ccid=ordid and nid<=@pagesize and ccp_cuid=@useruid
set nocount off 
end