IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[order_202_query]') AND type = 'P')
DROP PROCEDURE [dbo].[order_202_query]
GO
CREATE  procedure order_202_query(
@cmpid int,@branchid int,@userdpid int,@userotid int ,@useruid int ,@bizid int ,@finished int ,@status nvarchar(50),@pagesize int , @pagecount int , @pageobjids varchar(8000),@ord_jyfs_min varchar(100), @ord_jyfs_max varchar(100),@ord_no_min varchar(50), @ord_no_max varchar(50),@ord_khxm_min varchar(100), @ord_khxm_max varchar(100),@ord_xb_min varchar(100), @ord_xb_max varchar(100),@ord_tjbm_min varchar(100), @ord_tjbm_max varchar(100),@ord_sfzh_min varchar(100), @ord_sfzh_max varchar(100),@ord_khly_min varchar(100), @ord_khly_max varchar(100),@ord_kdpq_min varchar(100), @ord_kdpq_max varchar(100),@ord_kdcs_min varchar(100), @ord_kdcs_max varchar(100),@ord_status_min nvarchar(30), @ord_status_max nvarchar(30),@ord_nl_min int,@ord_nl_max int,@ord_dh_min varchar(100), @ord_dh_max varchar(100),@ord_creator_min nvarchar(50), @ord_creator_max nvarchar(50),@ord_date_min datetime,@ord_date_max datetime,@ord_createdepart_min nvarchar(50), @ord_createdepart_max nvarchar(50),@ord_tjr_min varchar(100), @ord_tjr_max varchar(100)) 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 order_202 a, @pobjs
where ord_id=ordid
order by nid 
select a.* from order_202_users a , @pobjs 
where ordp_ordid=ordid and ordp_cuid=@useruid
return 
end 
if (@status is null or @status = '_none_') set @status = '' 


if (@ord_jyfs_min is null or @ord_jyfs_min = '_none_') set @ord_jyfs_min = '' 
if (@ord_jyfs_max is null or @ord_jyfs_max = '_none_') set @ord_jyfs_max = '' 

if (@ord_no_min is null or @ord_no_min = '_none_') set @ord_no_min = '' 
if (@ord_no_max is null or @ord_no_max = '_none_') set @ord_no_max = '' 

if (@ord_khxm_min is null or @ord_khxm_min = '_none_') set @ord_khxm_min = '' 
if (@ord_khxm_max is null or @ord_khxm_max = '_none_') set @ord_khxm_max = '' 

if (@ord_xb_min is null or @ord_xb_min = '_none_') set @ord_xb_min = '' 
if (@ord_xb_max is null or @ord_xb_max = '_none_') set @ord_xb_max = '' 

if (@ord_tjbm_min is null or @ord_tjbm_min = '_none_') set @ord_tjbm_min = '' 
if (@ord_tjbm_max is null or @ord_tjbm_max = '_none_') set @ord_tjbm_max = '' 

if (@ord_sfzh_min is null or @ord_sfzh_min = '_none_') set @ord_sfzh_min = '' 
if (@ord_sfzh_max is null or @ord_sfzh_max = '_none_') set @ord_sfzh_max = '' 

if (@ord_khly_min is null or @ord_khly_min = '_none_') set @ord_khly_min = '' 
if (@ord_khly_max is null or @ord_khly_max = '_none_') set @ord_khly_max = '' 

if (@ord_kdpq_min is null or @ord_kdpq_min = '_none_') set @ord_kdpq_min = '' 
if (@ord_kdpq_max is null or @ord_kdpq_max = '_none_') set @ord_kdpq_max = '' 

if (@ord_kdcs_min is null or @ord_kdcs_min = '_none_') set @ord_kdcs_min = '' 
if (@ord_kdcs_max is null or @ord_kdcs_max = '_none_') set @ord_kdcs_max = '' 

if (@ord_status_min is null or @ord_status_min = '_none_') set @ord_status_min = '' 
if (@ord_status_max is null or @ord_status_max = '_none_') set @ord_status_max = '' 

if (@ord_nl_min is null or @ord_nl_min = -999999999) set @ord_nl_min = -999999999 
if (@ord_nl_max is null or @ord_nl_max = -999999999) set @ord_nl_max = 999999999

if (@ord_dh_min is null or @ord_dh_min = '_none_') set @ord_dh_min = '' 
if (@ord_dh_max is null or @ord_dh_max = '_none_') set @ord_dh_max = '' 

if (@ord_creator_min is null or @ord_creator_min = '_none_') set @ord_creator_min = '' 
if (@ord_creator_max is null or @ord_creator_max = '_none_') set @ord_creator_max = '' 


                if (@ord_date_min is null or @ord_date_min <= @nonedate) set @ord_date_min = '1900-01-01' 
                
if (@ord_date_max is null or @ord_date_max <= @nonedate) set @ord_date_max =  '2299-12-30 23:59:59'
else set @ord_date_max = @ord_date_max+ '23:59:59'
                

if (@ord_createdepart_min is null or @ord_createdepart_min = '_none_') set @ord_createdepart_min = '' 
if (@ord_createdepart_max is null or @ord_createdepart_max = '_none_') set @ord_createdepart_max = '' 

if (@ord_tjr_min is null or @ord_tjr_min = '_none_') set @ord_tjr_min = '' 
if (@ord_tjr_max is null or @ord_tjr_max = '_none_') set @ord_tjr_max = '' 

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

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

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

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

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

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

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

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

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

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

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

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

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

if @ord_tjr_min>'' 
begin 
if (charindex('%',@ord_tjr_min) = 0 and @ord_tjr_min!='_empty_') 
 begin
set @ord_tjr_min = '%'+@ord_tjr_min+'%';
 end else if (@ord_tjr_min='_empty_') set @ord_tjr_min='' 
end else
begin 
set @ord_tjr_min = '%' 
end
if @ord_tjr_max>'' 
begin 
if (charindex('%', @ord_tjr_max) = 0 and @ord_tjr_max!='_empty_') 
begin
set@ord_tjr_max = '%'+@ord_tjr_max+'%';
 end else if (@ord_tjr_max='_empty_') set @ord_tjr_max='' 
end else
begin 
set @ord_tjr_max = '%' 
end
 
    
if (@finished = -11)
begin
if (@pagesize=10000)  -- 下载
begin
select * from order_202 a,order_202_users 
where a.ord_cmpid=@cmpid 
and ordp_ordid=a.ord_id 
and ordp_type='bizview' 
and ordp_cuid=@useruid 
and (a.ord_status=@status or @status='') 
and a.ord_finished > 0 
and ord_jyfs like @ord_jyfs_min 
and ord_jyfs like @ord_jyfs_max 
and ord_no like @ord_no_min 
and ord_no like @ord_no_max 
and ord_khxm like @ord_khxm_min 
and ord_khxm like @ord_khxm_max 
and ord_xb like @ord_xb_min 
and ord_xb like @ord_xb_max 
and ord_tjbm like @ord_tjbm_min 
and ord_tjbm like @ord_tjbm_max 
and ord_sfzh like @ord_sfzh_min 
and ord_sfzh like @ord_sfzh_max 
and ord_khly like @ord_khly_min 
and ord_khly like @ord_khly_max 
and ord_kdpq like @ord_kdpq_min 
and ord_kdpq like @ord_kdpq_max 
and ord_kdcs like @ord_kdcs_min 
and ord_kdcs like @ord_kdcs_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_nl>=@ord_nl_min 
and ord_nl<=@ord_nl_max 
and ord_dh like @ord_dh_min 
and ord_dh like @ord_dh_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
and ord_date>=@ord_date_min 
and ord_date<=@ord_date_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_tjr like @ord_tjr_min 
and ord_tjr like @ord_tjr_max 
and ((ord_bfrid = @useruid    or ord_gjjg in('待跟进','已放弃'))) 
order by ord_date desc,ord_jyfs,ord_no,ord_khxm,ord_xb,ord_tjbm,ord_sfzh,ord_khly,ord_kdpq,ord_kdcs,ord_status,ord_nl,ord_dh,ord_creator,ord_createdepart,ord_tjr
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.ord_id  from order_202 a,order_202_users 
where a.ord_id=ordp_ordid 
and ordp_type='bizview' 
and ordp_cuid=@useruid 
and a.ord_finished > 0
and (a.ord_status=@status or @status='') 
and ord_jyfs like @ord_jyfs_min 
and ord_jyfs like @ord_jyfs_max 
and ord_no like @ord_no_min 
and ord_no like @ord_no_max 
and ord_khxm like @ord_khxm_min 
and ord_khxm like @ord_khxm_max 
and ord_xb like @ord_xb_min 
and ord_xb like @ord_xb_max 
and ord_tjbm like @ord_tjbm_min 
and ord_tjbm like @ord_tjbm_max 
and ord_sfzh like @ord_sfzh_min 
and ord_sfzh like @ord_sfzh_max 
and ord_khly like @ord_khly_min 
and ord_khly like @ord_khly_max 
and ord_kdpq like @ord_kdpq_min 
and ord_kdpq like @ord_kdpq_max 
and ord_kdcs like @ord_kdcs_min 
and ord_kdcs like @ord_kdcs_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_nl>=@ord_nl_min 
and ord_nl<=@ord_nl_max 
and ord_dh like @ord_dh_min 
and ord_dh like @ord_dh_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
and ord_date>=@ord_date_min 
and ord_date<=@ord_date_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_tjr like @ord_tjr_min 
and ord_tjr like @ord_tjr_max 
and ((ord_bfrid = @useruid    or ord_gjjg in('待跟进','已放弃'))) 
order by ord_date desc,ord_jyfs,ord_no,ord_khxm,ord_xb,ord_tjbm,ord_sfzh,ord_khly,ord_kdpq,ord_kdcs,ord_status,ord_nl,ord_dh,ord_creator,ord_createdepart,ord_tjr
select count(1) resultcount  from datalinker where useruid=@useruid 
end else
begin
select count(1) cnt from order_202 a,order_202_users 
where a.ord_id=ordp_ordid 
and a.ord_finished > 0
and ordp_type='bizview' 
and ordp_cuid=@useruid 
and (a.ord_status=@status or @status='') 
and ord_jyfs like @ord_jyfs_min 
and ord_jyfs like @ord_jyfs_max 
and ord_no like @ord_no_min 
and ord_no like @ord_no_max 
and ord_khxm like @ord_khxm_min 
and ord_khxm like @ord_khxm_max 
and ord_xb like @ord_xb_min 
and ord_xb like @ord_xb_max 
and ord_tjbm like @ord_tjbm_min 
and ord_tjbm like @ord_tjbm_max 
and ord_sfzh like @ord_sfzh_min 
and ord_sfzh like @ord_sfzh_max 
and ord_khly like @ord_khly_min 
and ord_khly like @ord_khly_max 
and ord_kdpq like @ord_kdpq_min 
and ord_kdpq like @ord_kdpq_max 
and ord_kdcs like @ord_kdcs_min 
and ord_kdcs like @ord_kdcs_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_nl>=@ord_nl_min 
and ord_nl<=@ord_nl_max 
and ord_dh like @ord_dh_min 
and ord_dh like @ord_dh_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
and ord_date>=@ord_date_min 
and ord_date<=@ord_date_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_tjr like @ord_tjr_min 
and ord_tjr like @ord_tjr_max 
and ((ord_bfrid = @useruid    or ord_gjjg in('待跟进','已放弃'))) 

-- 返回所有ID
insert into @pobjs(ordid) select top(@amount) ord_id from order_202 a,order_202_users 
where a.ord_id=ordp_ordid 
and a.ord_finished > 0
and ordp_type='bizview' 
and ordp_cuid=@useruid 
and (a.ord_status=@status or @status='') 
and ord_jyfs like @ord_jyfs_min 
and ord_jyfs like @ord_jyfs_max 
and ord_no like @ord_no_min 
and ord_no like @ord_no_max 
and ord_khxm like @ord_khxm_min 
and ord_khxm like @ord_khxm_max 
and ord_xb like @ord_xb_min 
and ord_xb like @ord_xb_max 
and ord_tjbm like @ord_tjbm_min 
and ord_tjbm like @ord_tjbm_max 
and ord_sfzh like @ord_sfzh_min 
and ord_sfzh like @ord_sfzh_max 
and ord_khly like @ord_khly_min 
and ord_khly like @ord_khly_max 
and ord_kdpq like @ord_kdpq_min 
and ord_kdpq like @ord_kdpq_max 
and ord_kdcs like @ord_kdcs_min 
and ord_kdcs like @ord_kdcs_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_nl>=@ord_nl_min 
and ord_nl<=@ord_nl_max 
and ord_dh like @ord_dh_min 
and ord_dh like @ord_dh_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
and ord_date>=@ord_date_min 
and ord_date<=@ord_date_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_tjr like @ord_tjr_min 
and ord_tjr like @ord_tjr_max 
and ((ord_bfrid = @useruid    or ord_gjjg in('待跟进','已放弃'))) 
order by ord_date desc,ord_jyfs,ord_no,ord_khxm,ord_xb,ord_tjbm,ord_sfzh,ord_khly,ord_kdpq,ord_kdcs,ord_status,ord_nl,ord_dh,ord_creator,ord_createdepart,ord_tjr
select ordid from @pobjs 
order by nid 
-- 返回第一页 
select top(@pagesize) * from order_202 a,@pobjs 
where a.ord_id=ordid and ((ord_bfrid = @useruid    or ord_gjjg in('待跟进','已放弃'))) 

and (a.ord_status=@status or @status='')
and a.ord_finished > 0
order by nid 
-- 返回权限 
select a.* from order_202_users a , @pobjs 
where ordp_ordid=ordid 
and nid<=@pagesize 
and ordp_cuid=@useruid 
end 
end else
if (@finished = -10)
begin
if (@pagesize=10000)  -- 下载
begin
select * from order_202 a,order_202_users 
where a.ord_cmpid=@cmpid 
and ordp_type='bizview' 
and ordp_ordid=a.ord_id 
and ordp_cuid=@useruid 
and (a.ord_status=@status or @status='') 
and (a.ord_finished>0 and a.ord_createcuid=@useruid) 
and ord_jyfs like @ord_jyfs_min 
and ord_jyfs like @ord_jyfs_max 
and ord_no like @ord_no_min 
and ord_no like @ord_no_max 
and ord_khxm like @ord_khxm_min 
and ord_khxm like @ord_khxm_max 
and ord_xb like @ord_xb_min 
and ord_xb like @ord_xb_max 
and ord_tjbm like @ord_tjbm_min 
and ord_tjbm like @ord_tjbm_max 
and ord_sfzh like @ord_sfzh_min 
and ord_sfzh like @ord_sfzh_max 
and ord_khly like @ord_khly_min 
and ord_khly like @ord_khly_max 
and ord_kdpq like @ord_kdpq_min 
and ord_kdpq like @ord_kdpq_max 
and ord_kdcs like @ord_kdcs_min 
and ord_kdcs like @ord_kdcs_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_nl>=@ord_nl_min 
and ord_nl<=@ord_nl_max 
and ord_dh like @ord_dh_min 
and ord_dh like @ord_dh_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
and ord_date>=@ord_date_min 
and ord_date<=@ord_date_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_tjr like @ord_tjr_min 
and ord_tjr like @ord_tjr_max 
and ((ord_bfrid = @useruid    or ord_gjjg in('待跟进','已放弃'))) 
order by ord_date desc,ord_jyfs,ord_no,ord_khxm,ord_xb,ord_tjbm,ord_sfzh,ord_khly,ord_kdpq,ord_kdcs,ord_status,ord_nl,ord_dh,ord_creator,ord_createdepart,ord_tjr
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.ord_id  from order_202 a,order_202_users 
where a.ord_id=ordp_ordid 
and ordp_type='bizview' 
and ordp_cuid=@useruid 
and (a.ord_finished>0 and a.ord_createcuid=@useruid) 
and (a.ord_status=@status or @status='') 
and ord_jyfs like @ord_jyfs_min 
and ord_jyfs like @ord_jyfs_max 
and ord_no like @ord_no_min 
and ord_no like @ord_no_max 
and ord_khxm like @ord_khxm_min 
and ord_khxm like @ord_khxm_max 
and ord_xb like @ord_xb_min 
and ord_xb like @ord_xb_max 
and ord_tjbm like @ord_tjbm_min 
and ord_tjbm like @ord_tjbm_max 
and ord_sfzh like @ord_sfzh_min 
and ord_sfzh like @ord_sfzh_max 
and ord_khly like @ord_khly_min 
and ord_khly like @ord_khly_max 
and ord_kdpq like @ord_kdpq_min 
and ord_kdpq like @ord_kdpq_max 
and ord_kdcs like @ord_kdcs_min 
and ord_kdcs like @ord_kdcs_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_nl>=@ord_nl_min 
and ord_nl<=@ord_nl_max 
and ord_dh like @ord_dh_min 
and ord_dh like @ord_dh_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
and ord_date>=@ord_date_min 
and ord_date<=@ord_date_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_tjr like @ord_tjr_min 
and ord_tjr like @ord_tjr_max 
and ((ord_bfrid = @useruid    or ord_gjjg in('待跟进','已放弃'))) 
order by ord_date desc,ord_jyfs,ord_no,ord_khxm,ord_xb,ord_tjbm,ord_sfzh,ord_khly,ord_kdpq,ord_kdcs,ord_status,ord_nl,ord_dh,ord_creator,ord_createdepart,ord_tjr
select count(1) resultcount  from datalinker where useruid=@useruid 
end else
begin
select count(1) cnt from order_202 a,order_202_users 
where a.ord_id=ordp_ordid 
and (a.ord_finished>0 and a.ord_createcuid=@useruid) 
and ordp_type='bizview' 
and ordp_cuid=@useruid 
and (a.ord_status=@status or @status='') 
and ord_jyfs like @ord_jyfs_min 
and ord_jyfs like @ord_jyfs_max 
and ord_no like @ord_no_min 
and ord_no like @ord_no_max 
and ord_khxm like @ord_khxm_min 
and ord_khxm like @ord_khxm_max 
and ord_xb like @ord_xb_min 
and ord_xb like @ord_xb_max 
and ord_tjbm like @ord_tjbm_min 
and ord_tjbm like @ord_tjbm_max 
and ord_sfzh like @ord_sfzh_min 
and ord_sfzh like @ord_sfzh_max 
and ord_khly like @ord_khly_min 
and ord_khly like @ord_khly_max 
and ord_kdpq like @ord_kdpq_min 
and ord_kdpq like @ord_kdpq_max 
and ord_kdcs like @ord_kdcs_min 
and ord_kdcs like @ord_kdcs_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_nl>=@ord_nl_min 
and ord_nl<=@ord_nl_max 
and ord_dh like @ord_dh_min 
and ord_dh like @ord_dh_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
and ord_date>=@ord_date_min 
and ord_date<=@ord_date_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_tjr like @ord_tjr_min 
and ord_tjr like @ord_tjr_max 
and ((ord_bfrid = @useruid    or ord_gjjg in('待跟进','已放弃'))) 

-- 返回所有ID
insert into @pobjs(ordid) select top(@amount) ord_id from order_202 a,order_202_users 
where a.ord_id=ordp_ordid 
and (a.ord_finished>0 and a.ord_createcuid=@useruid) 
and ordp_type='bizview' 
and ordp_cuid=@useruid 
and (a.ord_status=@status or @status='') 
and ord_jyfs like @ord_jyfs_min 
and ord_jyfs like @ord_jyfs_max 
and ord_no like @ord_no_min 
and ord_no like @ord_no_max 
and ord_khxm like @ord_khxm_min 
and ord_khxm like @ord_khxm_max 
and ord_xb like @ord_xb_min 
and ord_xb like @ord_xb_max 
and ord_tjbm like @ord_tjbm_min 
and ord_tjbm like @ord_tjbm_max 
and ord_sfzh like @ord_sfzh_min 
and ord_sfzh like @ord_sfzh_max 
and ord_khly like @ord_khly_min 
and ord_khly like @ord_khly_max 
and ord_kdpq like @ord_kdpq_min 
and ord_kdpq like @ord_kdpq_max 
and ord_kdcs like @ord_kdcs_min 
and ord_kdcs like @ord_kdcs_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_nl>=@ord_nl_min 
and ord_nl<=@ord_nl_max 
and ord_dh like @ord_dh_min 
and ord_dh like @ord_dh_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
and ord_date>=@ord_date_min 
and ord_date<=@ord_date_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_tjr like @ord_tjr_min 
and ord_tjr like @ord_tjr_max 
and ((ord_bfrid = @useruid    or ord_gjjg in('待跟进','已放弃'))) 
order by ord_date desc,ord_jyfs,ord_no,ord_khxm,ord_xb,ord_tjbm,ord_sfzh,ord_khly,ord_kdpq,ord_kdcs,ord_status,ord_nl,ord_dh,ord_creator,ord_createdepart,ord_tjr
select ordid from @pobjs 
order by nid 
-- 返回第一页 
select top(@pagesize) * from order_202 a,@pobjs 
where a.ord_id=ordid and ((ord_bfrid = @useruid    or ord_gjjg in('待跟进','已放弃'))) 

and (a.ord_status=@status or @status='')
and (a.ord_finished>0 and a.ord_createcuid=@useruid) 
order by nid 
-- 返回权限 
select a.* from order_202_users a , @pobjs 
where ordp_ordid=ordid 
and nid<=@pagesize 
and ordp_cuid=@useruid 
end 
end else
if (@finished = 2)
begin
if (@pagesize=10000)  -- 下载
begin
select * from order_202 a,order_202_users 
where a.ord_cmpid=@cmpid 
and ordp_ordid=a.ord_id 
and ordp_type='bizview' 
and ordp_cuid=@useruid 
and (a.ord_status=@status or @status='') 
and a.ord_finished>=2 
and ord_jyfs like @ord_jyfs_min 
and ord_jyfs like @ord_jyfs_max 
and ord_no like @ord_no_min 
and ord_no like @ord_no_max 
and ord_khxm like @ord_khxm_min 
and ord_khxm like @ord_khxm_max 
and ord_xb like @ord_xb_min 
and ord_xb like @ord_xb_max 
and ord_tjbm like @ord_tjbm_min 
and ord_tjbm like @ord_tjbm_max 
and ord_sfzh like @ord_sfzh_min 
and ord_sfzh like @ord_sfzh_max 
and ord_khly like @ord_khly_min 
and ord_khly like @ord_khly_max 
and ord_kdpq like @ord_kdpq_min 
and ord_kdpq like @ord_kdpq_max 
and ord_kdcs like @ord_kdcs_min 
and ord_kdcs like @ord_kdcs_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_nl>=@ord_nl_min 
and ord_nl<=@ord_nl_max 
and ord_dh like @ord_dh_min 
and ord_dh like @ord_dh_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
and ord_date>=@ord_date_min 
and ord_date<=@ord_date_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_tjr like @ord_tjr_min 
and ord_tjr like @ord_tjr_max 
and ((ord_bfrid = @useruid    or ord_gjjg in('待跟进','已放弃'))) 
order by ord_date desc,ord_jyfs,ord_no,ord_khxm,ord_xb,ord_tjbm,ord_sfzh,ord_khly,ord_kdpq,ord_kdcs,ord_status,ord_nl,ord_dh,ord_creator,ord_createdepart,ord_tjr
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.ord_id  from order_202 a,order_202_users 
where a.ord_id=ordp_ordid 
and ordp_type='bizview' 
and ordp_cuid=@useruid 
and a.ord_finished>=2 
and (a.ord_status=@status or @status='') 
and ord_jyfs like @ord_jyfs_min 
and ord_jyfs like @ord_jyfs_max 
and ord_no like @ord_no_min 
and ord_no like @ord_no_max 
and ord_khxm like @ord_khxm_min 
and ord_khxm like @ord_khxm_max 
and ord_xb like @ord_xb_min 
and ord_xb like @ord_xb_max 
and ord_tjbm like @ord_tjbm_min 
and ord_tjbm like @ord_tjbm_max 
and ord_sfzh like @ord_sfzh_min 
and ord_sfzh like @ord_sfzh_max 
and ord_khly like @ord_khly_min 
and ord_khly like @ord_khly_max 
and ord_kdpq like @ord_kdpq_min 
and ord_kdpq like @ord_kdpq_max 
and ord_kdcs like @ord_kdcs_min 
and ord_kdcs like @ord_kdcs_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_nl>=@ord_nl_min 
and ord_nl<=@ord_nl_max 
and ord_dh like @ord_dh_min 
and ord_dh like @ord_dh_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
and ord_date>=@ord_date_min 
and ord_date<=@ord_date_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_tjr like @ord_tjr_min 
and ord_tjr like @ord_tjr_max 
and ((ord_bfrid = @useruid    or ord_gjjg in('待跟进','已放弃'))) 
order by ord_date desc,ord_jyfs,ord_no,ord_khxm,ord_xb,ord_tjbm,ord_sfzh,ord_khly,ord_kdpq,ord_kdcs,ord_status,ord_nl,ord_dh,ord_creator,ord_createdepart,ord_tjr
select count(1) resultcount  from datalinker where useruid=@useruid 
end else
begin
select count(1) cnt from order_202 a,order_202_users 
where a.ord_id=ordp_ordid 
and a.ord_finished>=2 
and ordp_type='bizview' 
and ordp_cuid=@useruid 
and (a.ord_status=@status or @status='') 
and ord_jyfs like @ord_jyfs_min 
and ord_jyfs like @ord_jyfs_max 
and ord_no like @ord_no_min 
and ord_no like @ord_no_max 
and ord_khxm like @ord_khxm_min 
and ord_khxm like @ord_khxm_max 
and ord_xb like @ord_xb_min 
and ord_xb like @ord_xb_max 
and ord_tjbm like @ord_tjbm_min 
and ord_tjbm like @ord_tjbm_max 
and ord_sfzh like @ord_sfzh_min 
and ord_sfzh like @ord_sfzh_max 
and ord_khly like @ord_khly_min 
and ord_khly like @ord_khly_max 
and ord_kdpq like @ord_kdpq_min 
and ord_kdpq like @ord_kdpq_max 
and ord_kdcs like @ord_kdcs_min 
and ord_kdcs like @ord_kdcs_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_nl>=@ord_nl_min 
and ord_nl<=@ord_nl_max 
and ord_dh like @ord_dh_min 
and ord_dh like @ord_dh_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
and ord_date>=@ord_date_min 
and ord_date<=@ord_date_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_tjr like @ord_tjr_min 
and ord_tjr like @ord_tjr_max 
and ((ord_bfrid = @useruid    or ord_gjjg in('待跟进','已放弃'))) 

-- 返回所有ID
insert into @pobjs(ordid) select top(@amount) ord_id from order_202 a,order_202_users 
where a.ord_id=ordp_ordid 
and a.ord_finished>=2 
and ordp_type='bizview' 
and ordp_cuid=@useruid 
and (a.ord_status=@status or @status='') 
and ord_jyfs like @ord_jyfs_min 
and ord_jyfs like @ord_jyfs_max 
and ord_no like @ord_no_min 
and ord_no like @ord_no_max 
and ord_khxm like @ord_khxm_min 
and ord_khxm like @ord_khxm_max 
and ord_xb like @ord_xb_min 
and ord_xb like @ord_xb_max 
and ord_tjbm like @ord_tjbm_min 
and ord_tjbm like @ord_tjbm_max 
and ord_sfzh like @ord_sfzh_min 
and ord_sfzh like @ord_sfzh_max 
and ord_khly like @ord_khly_min 
and ord_khly like @ord_khly_max 
and ord_kdpq like @ord_kdpq_min 
and ord_kdpq like @ord_kdpq_max 
and ord_kdcs like @ord_kdcs_min 
and ord_kdcs like @ord_kdcs_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_nl>=@ord_nl_min 
and ord_nl<=@ord_nl_max 
and ord_dh like @ord_dh_min 
and ord_dh like @ord_dh_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
and ord_date>=@ord_date_min 
and ord_date<=@ord_date_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_tjr like @ord_tjr_min 
and ord_tjr like @ord_tjr_max 
and ((ord_bfrid = @useruid    or ord_gjjg in('待跟进','已放弃'))) 
order by ord_date desc,ord_jyfs,ord_no,ord_khxm,ord_xb,ord_tjbm,ord_sfzh,ord_khly,ord_kdpq,ord_kdcs,ord_status,ord_nl,ord_dh,ord_creator,ord_createdepart,ord_tjr
select ordid from @pobjs 
order by nid 
-- 返回第一页 
select top(@pagesize) * from order_202 a,@pobjs 
where a.ord_id=ordid and ((ord_bfrid = @useruid    or ord_gjjg in('待跟进','已放弃'))) 

and (a.ord_status=@status or @status='')
and a.ord_finished>=2 
order by nid 
-- 返回权限 
select a.* from order_202_users a , @pobjs 
where ordp_ordid=ordid 
and nid<=@pagesize 
and ordp_cuid=@useruid 
end 
end else
if (@finished = 1)
begin
if (@pagesize=10000)  -- 下载
begin
select * from order_202 a,order_202_users 
where a.ord_cmpid=@cmpid 
and ordp_type='bizview' 
and ordp_cuid=@useruid 
and ordp_ordid=a.ord_id 
and (a.ord_status=@status or @status='') 
and (a.ord_finished>0 and a.ord_finished<=1)
and ord_jyfs like @ord_jyfs_min 
and ord_jyfs like @ord_jyfs_max 
and ord_no like @ord_no_min 
and ord_no like @ord_no_max 
and ord_khxm like @ord_khxm_min 
and ord_khxm like @ord_khxm_max 
and ord_xb like @ord_xb_min 
and ord_xb like @ord_xb_max 
and ord_tjbm like @ord_tjbm_min 
and ord_tjbm like @ord_tjbm_max 
and ord_sfzh like @ord_sfzh_min 
and ord_sfzh like @ord_sfzh_max 
and ord_khly like @ord_khly_min 
and ord_khly like @ord_khly_max 
and ord_kdpq like @ord_kdpq_min 
and ord_kdpq like @ord_kdpq_max 
and ord_kdcs like @ord_kdcs_min 
and ord_kdcs like @ord_kdcs_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_nl>=@ord_nl_min 
and ord_nl<=@ord_nl_max 
and ord_dh like @ord_dh_min 
and ord_dh like @ord_dh_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
and ord_date>=@ord_date_min 
and ord_date<=@ord_date_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_tjr like @ord_tjr_min 
and ord_tjr like @ord_tjr_max 
and ((ord_bfrid = @useruid    or ord_gjjg in('待跟进','已放弃'))) 
order by ord_date desc,ord_jyfs,ord_no,ord_khxm,ord_xb,ord_tjbm,ord_sfzh,ord_khly,ord_kdpq,ord_kdcs,ord_status,ord_nl,ord_dh,ord_creator,ord_createdepart,ord_tjr
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.ord_id  from order_202 a,order_202_users 
where a.ord_id=ordp_ordid 
and ordp_type='bizview' 
and ordp_cuid=@useruid 
and (a.ord_finished>0 and a.ord_finished<=1)
and (a.ord_status=@status or @status='') 
and ord_jyfs like @ord_jyfs_min 
and ord_jyfs like @ord_jyfs_max 
and ord_no like @ord_no_min 
and ord_no like @ord_no_max 
and ord_khxm like @ord_khxm_min 
and ord_khxm like @ord_khxm_max 
and ord_xb like @ord_xb_min 
and ord_xb like @ord_xb_max 
and ord_tjbm like @ord_tjbm_min 
and ord_tjbm like @ord_tjbm_max 
and ord_sfzh like @ord_sfzh_min 
and ord_sfzh like @ord_sfzh_max 
and ord_khly like @ord_khly_min 
and ord_khly like @ord_khly_max 
and ord_kdpq like @ord_kdpq_min 
and ord_kdpq like @ord_kdpq_max 
and ord_kdcs like @ord_kdcs_min 
and ord_kdcs like @ord_kdcs_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_nl>=@ord_nl_min 
and ord_nl<=@ord_nl_max 
and ord_dh like @ord_dh_min 
and ord_dh like @ord_dh_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
and ord_date>=@ord_date_min 
and ord_date<=@ord_date_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_tjr like @ord_tjr_min 
and ord_tjr like @ord_tjr_max 
and ((ord_bfrid = @useruid    or ord_gjjg in('待跟进','已放弃'))) 
order by ord_date desc,ord_jyfs,ord_no,ord_khxm,ord_xb,ord_tjbm,ord_sfzh,ord_khly,ord_kdpq,ord_kdcs,ord_status,ord_nl,ord_dh,ord_creator,ord_createdepart,ord_tjr
select count(1) resultcount  from datalinker where useruid=@useruid 
end else
begin
select count(1) cnt from order_202 a,order_202_users 
where a.ord_id=ordp_ordid 
and (a.ord_finished>0 and a.ord_finished<=1)
and ordp_type='bizview' 
and ordp_cuid=@useruid 
and (a.ord_status=@status or @status='') 
and ord_jyfs like @ord_jyfs_min 
and ord_jyfs like @ord_jyfs_max 
and ord_no like @ord_no_min 
and ord_no like @ord_no_max 
and ord_khxm like @ord_khxm_min 
and ord_khxm like @ord_khxm_max 
and ord_xb like @ord_xb_min 
and ord_xb like @ord_xb_max 
and ord_tjbm like @ord_tjbm_min 
and ord_tjbm like @ord_tjbm_max 
and ord_sfzh like @ord_sfzh_min 
and ord_sfzh like @ord_sfzh_max 
and ord_khly like @ord_khly_min 
and ord_khly like @ord_khly_max 
and ord_kdpq like @ord_kdpq_min 
and ord_kdpq like @ord_kdpq_max 
and ord_kdcs like @ord_kdcs_min 
and ord_kdcs like @ord_kdcs_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_nl>=@ord_nl_min 
and ord_nl<=@ord_nl_max 
and ord_dh like @ord_dh_min 
and ord_dh like @ord_dh_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
and ord_date>=@ord_date_min 
and ord_date<=@ord_date_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_tjr like @ord_tjr_min 
and ord_tjr like @ord_tjr_max 
and ((ord_bfrid = @useruid    or ord_gjjg in('待跟进','已放弃'))) 

-- 返回所有ID
insert into @pobjs(ordid) select top(@amount) ord_id from order_202 a,order_202_users 
where a.ord_id=ordp_ordid 
and (a.ord_finished>0 and a.ord_finished<=1)
and ordp_type='bizview' 
and ordp_cuid=@useruid 
and (a.ord_status=@status or @status='') 
and ord_jyfs like @ord_jyfs_min 
and ord_jyfs like @ord_jyfs_max 
and ord_no like @ord_no_min 
and ord_no like @ord_no_max 
and ord_khxm like @ord_khxm_min 
and ord_khxm like @ord_khxm_max 
and ord_xb like @ord_xb_min 
and ord_xb like @ord_xb_max 
and ord_tjbm like @ord_tjbm_min 
and ord_tjbm like @ord_tjbm_max 
and ord_sfzh like @ord_sfzh_min 
and ord_sfzh like @ord_sfzh_max 
and ord_khly like @ord_khly_min 
and ord_khly like @ord_khly_max 
and ord_kdpq like @ord_kdpq_min 
and ord_kdpq like @ord_kdpq_max 
and ord_kdcs like @ord_kdcs_min 
and ord_kdcs like @ord_kdcs_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_nl>=@ord_nl_min 
and ord_nl<=@ord_nl_max 
and ord_dh like @ord_dh_min 
and ord_dh like @ord_dh_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
and ord_date>=@ord_date_min 
and ord_date<=@ord_date_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_tjr like @ord_tjr_min 
and ord_tjr like @ord_tjr_max 
and ((ord_bfrid = @useruid    or ord_gjjg in('待跟进','已放弃'))) 
order by ord_date desc,ord_jyfs,ord_no,ord_khxm,ord_xb,ord_tjbm,ord_sfzh,ord_khly,ord_kdpq,ord_kdcs,ord_status,ord_nl,ord_dh,ord_creator,ord_createdepart,ord_tjr
select ordid from @pobjs 
order by nid 
-- 返回第一页 
select top(@pagesize) * from order_202 a,@pobjs 
where a.ord_id=ordid and ((ord_bfrid = @useruid    or ord_gjjg in('待跟进','已放弃'))) 

and (a.ord_status=@status or @status='')
and (a.ord_finished>0 and a.ord_finished<=1) 
order by nid 
-- 返回权限 
select a.* from order_202_users a , @pobjs 
where ordp_ordid=ordid 
and nid<=@pagesize 
and ordp_cuid=@useruid 
end 
end else
if (@finished = -1)
begin
if (@pagesize=10000)  -- 下载
begin
select * from order_202 a,order_202_users 
where a.ord_cmpid=@cmpid 
and ordp_type='bizview' 
and ordp_ordid=a.ord_id 
and ordp_cuid=@useruid 
and (a.ord_status=@status or @status='') 
and ord_finished=-1 
and ord_jyfs like @ord_jyfs_min 
and ord_jyfs like @ord_jyfs_max 
and ord_no like @ord_no_min 
and ord_no like @ord_no_max 
and ord_khxm like @ord_khxm_min 
and ord_khxm like @ord_khxm_max 
and ord_xb like @ord_xb_min 
and ord_xb like @ord_xb_max 
and ord_tjbm like @ord_tjbm_min 
and ord_tjbm like @ord_tjbm_max 
and ord_sfzh like @ord_sfzh_min 
and ord_sfzh like @ord_sfzh_max 
and ord_khly like @ord_khly_min 
and ord_khly like @ord_khly_max 
and ord_kdpq like @ord_kdpq_min 
and ord_kdpq like @ord_kdpq_max 
and ord_kdcs like @ord_kdcs_min 
and ord_kdcs like @ord_kdcs_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_nl>=@ord_nl_min 
and ord_nl<=@ord_nl_max 
and ord_dh like @ord_dh_min 
and ord_dh like @ord_dh_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
and ord_date>=@ord_date_min 
and ord_date<=@ord_date_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_tjr like @ord_tjr_min 
and ord_tjr like @ord_tjr_max 
and ((ord_bfrid = @useruid    or ord_gjjg in('待跟进','已放弃'))) 
order by ord_date desc,ord_jyfs,ord_no,ord_khxm,ord_xb,ord_tjbm,ord_sfzh,ord_khly,ord_kdpq,ord_kdcs,ord_status,ord_nl,ord_dh,ord_creator,ord_createdepart,ord_tjr
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.ord_id  from order_202 a,order_202_users 
where a.ord_id=ordp_ordid 
and ordp_type='bizview' 
and ordp_cuid=@useruid 
and ord_finished=-1 
and (a.ord_status=@status or @status='') 
and ord_jyfs like @ord_jyfs_min 
and ord_jyfs like @ord_jyfs_max 
and ord_no like @ord_no_min 
and ord_no like @ord_no_max 
and ord_khxm like @ord_khxm_min 
and ord_khxm like @ord_khxm_max 
and ord_xb like @ord_xb_min 
and ord_xb like @ord_xb_max 
and ord_tjbm like @ord_tjbm_min 
and ord_tjbm like @ord_tjbm_max 
and ord_sfzh like @ord_sfzh_min 
and ord_sfzh like @ord_sfzh_max 
and ord_khly like @ord_khly_min 
and ord_khly like @ord_khly_max 
and ord_kdpq like @ord_kdpq_min 
and ord_kdpq like @ord_kdpq_max 
and ord_kdcs like @ord_kdcs_min 
and ord_kdcs like @ord_kdcs_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_nl>=@ord_nl_min 
and ord_nl<=@ord_nl_max 
and ord_dh like @ord_dh_min 
and ord_dh like @ord_dh_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
and ord_date>=@ord_date_min 
and ord_date<=@ord_date_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_tjr like @ord_tjr_min 
and ord_tjr like @ord_tjr_max 
and ((ord_bfrid = @useruid    or ord_gjjg in('待跟进','已放弃'))) 
order by ord_date desc,ord_jyfs,ord_no,ord_khxm,ord_xb,ord_tjbm,ord_sfzh,ord_khly,ord_kdpq,ord_kdcs,ord_status,ord_nl,ord_dh,ord_creator,ord_createdepart,ord_tjr
select count(1) resultcount  from datalinker where useruid=@useruid 
end else
begin
select count(1) cnt from order_202 a,order_202_users 
where a.ord_id=ordp_ordid 
and ord_finished=-1 
and ordp_type='bizview' 
and ordp_cuid=@useruid 
and (a.ord_status=@status or @status='') 
and ord_jyfs like @ord_jyfs_min 
and ord_jyfs like @ord_jyfs_max 
and ord_no like @ord_no_min 
and ord_no like @ord_no_max 
and ord_khxm like @ord_khxm_min 
and ord_khxm like @ord_khxm_max 
and ord_xb like @ord_xb_min 
and ord_xb like @ord_xb_max 
and ord_tjbm like @ord_tjbm_min 
and ord_tjbm like @ord_tjbm_max 
and ord_sfzh like @ord_sfzh_min 
and ord_sfzh like @ord_sfzh_max 
and ord_khly like @ord_khly_min 
and ord_khly like @ord_khly_max 
and ord_kdpq like @ord_kdpq_min 
and ord_kdpq like @ord_kdpq_max 
and ord_kdcs like @ord_kdcs_min 
and ord_kdcs like @ord_kdcs_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_nl>=@ord_nl_min 
and ord_nl<=@ord_nl_max 
and ord_dh like @ord_dh_min 
and ord_dh like @ord_dh_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
and ord_date>=@ord_date_min 
and ord_date<=@ord_date_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_tjr like @ord_tjr_min 
and ord_tjr like @ord_tjr_max 
and ((ord_bfrid = @useruid    or ord_gjjg in('待跟进','已放弃'))) 

-- 返回所有ID
insert into @pobjs(ordid) select top(@amount) ord_id from order_202 a,order_202_users 
where a.ord_id=ordp_ordid 
and ord_finished=-1 
and ordp_type='bizview' 
and ordp_cuid=@useruid 
and (a.ord_status=@status or @status='') 
and ord_jyfs like @ord_jyfs_min 
and ord_jyfs like @ord_jyfs_max 
and ord_no like @ord_no_min 
and ord_no like @ord_no_max 
and ord_khxm like @ord_khxm_min 
and ord_khxm like @ord_khxm_max 
and ord_xb like @ord_xb_min 
and ord_xb like @ord_xb_max 
and ord_tjbm like @ord_tjbm_min 
and ord_tjbm like @ord_tjbm_max 
and ord_sfzh like @ord_sfzh_min 
and ord_sfzh like @ord_sfzh_max 
and ord_khly like @ord_khly_min 
and ord_khly like @ord_khly_max 
and ord_kdpq like @ord_kdpq_min 
and ord_kdpq like @ord_kdpq_max 
and ord_kdcs like @ord_kdcs_min 
and ord_kdcs like @ord_kdcs_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_nl>=@ord_nl_min 
and ord_nl<=@ord_nl_max 
and ord_dh like @ord_dh_min 
and ord_dh like @ord_dh_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
and ord_date>=@ord_date_min 
and ord_date<=@ord_date_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_tjr like @ord_tjr_min 
and ord_tjr like @ord_tjr_max 
and ((ord_bfrid = @useruid    or ord_gjjg in('待跟进','已放弃'))) 
order by ord_date desc,ord_jyfs,ord_no,ord_khxm,ord_xb,ord_tjbm,ord_sfzh,ord_khly,ord_kdpq,ord_kdcs,ord_status,ord_nl,ord_dh,ord_creator,ord_createdepart,ord_tjr
select ordid from @pobjs 
order by nid 
-- 返回第一页 
select top(@pagesize) * from order_202 a,@pobjs 
where a.ord_id=ordid and ((ord_bfrid = @useruid    or ord_gjjg in('待跟进','已放弃'))) 

and (a.ord_status=@status or @status='')
and ord_finished=-1 
order by nid 
-- 返回权限 
select a.* from order_202_users a , @pobjs 
where ordp_ordid=ordid 
and nid<=@pagesize 
and ordp_cuid=@useruid 
end 
end 
set nocount off end