IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[order_87_query]') AND type = 'P')
DROP PROCEDURE [dbo].[order_87_query]
GO
CREATE  procedure order_87_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_clr_min varchar(100), @ord_clr_max varchar(100),@ord_createdepart_min nvarchar(50), @ord_createdepart_max nvarchar(50),@ord_creator_min nvarchar(50), @ord_creator_max nvarchar(50),@ord_tsdx_min varchar(100), @ord_tsdx_max varchar(100),@ord_tssw_min varchar(100), @ord_tssw_max varchar(100),@ord_tsly_min varchar(100), @ord_tsly_max varchar(100),@ord_tslx_min varchar(100), @ord_tslx_max varchar(100),@ord_tswt_min varchar(100), @ord_tswt_max varchar(100),@ord_cljg_min varchar(100), @ord_cljg_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_87 a, @pobjs
where ord_id=ordid
order by nid 
select a.* from order_87_users a , @pobjs 
where ordp_ordid=ordid and ordp_cuid=@useruid
return 
end 
if (@status is null or @status = '_none_') set @status = '' 


if (@ord_clr_min is null or @ord_clr_min = '_none_') set @ord_clr_min = '' 
if (@ord_clr_max is null or @ord_clr_max = '_none_') set @ord_clr_max = '' 

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_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_tsdx_min is null or @ord_tsdx_min = '_none_') set @ord_tsdx_min = '' 
if (@ord_tsdx_max is null or @ord_tsdx_max = '_none_') set @ord_tsdx_max = '' 

if (@ord_tssw_min is null or @ord_tssw_min = '_none_') set @ord_tssw_min = '' 
if (@ord_tssw_max is null or @ord_tssw_max = '_none_') set @ord_tssw_max = '' 

if (@ord_tsly_min is null or @ord_tsly_min = '_none_') set @ord_tsly_min = '' 
if (@ord_tsly_max is null or @ord_tsly_max = '_none_') set @ord_tsly_max = '' 

if (@ord_tslx_min is null or @ord_tslx_min = '_none_') set @ord_tslx_min = '' 
if (@ord_tslx_max is null or @ord_tslx_max = '_none_') set @ord_tslx_max = '' 

if (@ord_tswt_min is null or @ord_tswt_min = '_none_') set @ord_tswt_min = '' 
if (@ord_tswt_max is null or @ord_tswt_max = '_none_') set @ord_tswt_max = '' 

if (@ord_cljg_min is null or @ord_cljg_min = '_none_') set @ord_cljg_min = '' 
if (@ord_cljg_max is null or @ord_cljg_max = '_none_') set @ord_cljg_max = '' 

if @ord_clr_min>'' 
begin 
if (charindex('%',@ord_clr_min) = 0 and @ord_clr_min!='_empty_') 
 begin
set @ord_clr_min = '%'+@ord_clr_min+'%';
 end else if (@ord_clr_min='_empty_') set @ord_clr_min='' 
end else
begin 
set @ord_clr_min = '%' 
end
if @ord_clr_max>'' 
begin 
if (charindex('%', @ord_clr_max) = 0 and @ord_clr_max!='_empty_') 
begin
set@ord_clr_max = '%'+@ord_clr_max+'%';
 end else if (@ord_clr_max='_empty_') set @ord_clr_max='' 
end else
begin 
set @ord_clr_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_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_tsdx_min>'' 
begin 
if (charindex('%',@ord_tsdx_min) = 0 and @ord_tsdx_min!='_empty_') 
 begin
set @ord_tsdx_min = '%'+@ord_tsdx_min+'%';
 end else if (@ord_tsdx_min='_empty_') set @ord_tsdx_min='' 
end else
begin 
set @ord_tsdx_min = '%' 
end
if @ord_tsdx_max>'' 
begin 
if (charindex('%', @ord_tsdx_max) = 0 and @ord_tsdx_max!='_empty_') 
begin
set@ord_tsdx_max = '%'+@ord_tsdx_max+'%';
 end else if (@ord_tsdx_max='_empty_') set @ord_tsdx_max='' 
end else
begin 
set @ord_tsdx_max = '%' 
end

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

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

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

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

if @ord_cljg_min>'' 
begin 
if (charindex('%',@ord_cljg_min) = 0 and @ord_cljg_min!='_empty_') 
 begin
set @ord_cljg_min = '%'+@ord_cljg_min+'%';
 end else if (@ord_cljg_min='_empty_') set @ord_cljg_min='' 
end else
begin 
set @ord_cljg_min = '%' 
end
if @ord_cljg_max>'' 
begin 
if (charindex('%', @ord_cljg_max) = 0 and @ord_cljg_max!='_empty_') 
begin
set@ord_cljg_max = '%'+@ord_cljg_max+'%';
 end else if (@ord_cljg_max='_empty_') set @ord_cljg_max='' 
end else
begin 
set @ord_cljg_max = '%' 
end
 
    
if (@finished = -11)
begin
if (@pagesize=10000)  -- 下载
begin
select a.* from order_87 a,order_87_users 
where a.ord_cmpid=@cmpid 
and ordp_type='bizview' 
and ordp_cuid=@useruid 
and (a.ord_status=@status or @status='') 
and a.ord_finished > 0 
and ord_clr like @ord_clr_min 
and ord_clr like @ord_clr_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
and ord_tsdx like @ord_tsdx_min 
and ord_tsdx like @ord_tsdx_max 
and ord_tssw like @ord_tssw_min 
and ord_tssw like @ord_tssw_max 
and ord_tsly like @ord_tsly_min 
and ord_tsly like @ord_tsly_max 
and ord_tslx like @ord_tslx_min 
and ord_tslx like @ord_tslx_max 
and ord_tswt like @ord_tswt_min 
and ord_tswt like @ord_tswt_max 
and ord_cljg like @ord_cljg_min 
and ord_cljg like @ord_cljg_max 
order by ord_date desc,ord_clr,ord_createdepart,ord_creator,ord_tsdx,ord_tssw,ord_tsly,ord_tslx,ord_tswt,ord_cljg
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_87 a,order_87_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_clr like @ord_clr_min 
and ord_clr like @ord_clr_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
and ord_tsdx like @ord_tsdx_min 
and ord_tsdx like @ord_tsdx_max 
and ord_tssw like @ord_tssw_min 
and ord_tssw like @ord_tssw_max 
and ord_tsly like @ord_tsly_min 
and ord_tsly like @ord_tsly_max 
and ord_tslx like @ord_tslx_min 
and ord_tslx like @ord_tslx_max 
and ord_tswt like @ord_tswt_min 
and ord_tswt like @ord_tswt_max 
and ord_cljg like @ord_cljg_min 
and ord_cljg like @ord_cljg_max 
order by ord_date desc,ord_clr,ord_createdepart,ord_creator,ord_tsdx,ord_tssw,ord_tsly,ord_tslx,ord_tswt,ord_cljg
select count(1) resultcount  from datalinker where useruid=@useruid 
end else
begin
select count(1) cnt from order_87 a,order_87_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_clr like @ord_clr_min 
and ord_clr like @ord_clr_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
and ord_tsdx like @ord_tsdx_min 
and ord_tsdx like @ord_tsdx_max 
and ord_tssw like @ord_tssw_min 
and ord_tssw like @ord_tssw_max 
and ord_tsly like @ord_tsly_min 
and ord_tsly like @ord_tsly_max 
and ord_tslx like @ord_tslx_min 
and ord_tslx like @ord_tslx_max 
and ord_tswt like @ord_tswt_min 
and ord_tswt like @ord_tswt_max 
and ord_cljg like @ord_cljg_min 
and ord_cljg like @ord_cljg_max 

-- 返回所有ID
insert into @pobjs(ordid) select top(@amount) ord_id from order_87 a,order_87_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_clr like @ord_clr_min 
and ord_clr like @ord_clr_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
and ord_tsdx like @ord_tsdx_min 
and ord_tsdx like @ord_tsdx_max 
and ord_tssw like @ord_tssw_min 
and ord_tssw like @ord_tssw_max 
and ord_tsly like @ord_tsly_min 
and ord_tsly like @ord_tsly_max 
and ord_tslx like @ord_tslx_min 
and ord_tslx like @ord_tslx_max 
and ord_tswt like @ord_tswt_min 
and ord_tswt like @ord_tswt_max 
and ord_cljg like @ord_cljg_min 
and ord_cljg like @ord_cljg_max 
order by ord_date desc,ord_clr,ord_createdepart,ord_creator,ord_tsdx,ord_tssw,ord_tsly,ord_tslx,ord_tswt,ord_cljg
select ordid from @pobjs 
order by nid 
-- 返回第一页 
select top(@pagesize) a.* from order_87 a,@pobjs 
where a.ord_id=ordid 
and (a.ord_status=@status or @status='')
and a.ord_finished > 0
order by nid 
-- 返回权限 
select a.* from order_87_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 a.* from order_87 a,order_87_users 
where a.ord_cmpid=@cmpid 
and ordp_type='bizview' 
and ordp_cuid=@useruid 
and (a.ord_status=@status or @status='') 
and (a.ord_finished>0 and a.ord_createcuid=@useruid) 
and ord_clr like @ord_clr_min 
and ord_clr like @ord_clr_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
and ord_tsdx like @ord_tsdx_min 
and ord_tsdx like @ord_tsdx_max 
and ord_tssw like @ord_tssw_min 
and ord_tssw like @ord_tssw_max 
and ord_tsly like @ord_tsly_min 
and ord_tsly like @ord_tsly_max 
and ord_tslx like @ord_tslx_min 
and ord_tslx like @ord_tslx_max 
and ord_tswt like @ord_tswt_min 
and ord_tswt like @ord_tswt_max 
and ord_cljg like @ord_cljg_min 
and ord_cljg like @ord_cljg_max 
order by ord_date desc,ord_clr,ord_createdepart,ord_creator,ord_tsdx,ord_tssw,ord_tsly,ord_tslx,ord_tswt,ord_cljg
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_87 a,order_87_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_clr like @ord_clr_min 
and ord_clr like @ord_clr_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
and ord_tsdx like @ord_tsdx_min 
and ord_tsdx like @ord_tsdx_max 
and ord_tssw like @ord_tssw_min 
and ord_tssw like @ord_tssw_max 
and ord_tsly like @ord_tsly_min 
and ord_tsly like @ord_tsly_max 
and ord_tslx like @ord_tslx_min 
and ord_tslx like @ord_tslx_max 
and ord_tswt like @ord_tswt_min 
and ord_tswt like @ord_tswt_max 
and ord_cljg like @ord_cljg_min 
and ord_cljg like @ord_cljg_max 
order by ord_date desc,ord_clr,ord_createdepart,ord_creator,ord_tsdx,ord_tssw,ord_tsly,ord_tslx,ord_tswt,ord_cljg
select count(1) resultcount  from datalinker where useruid=@useruid 
end else
begin
select count(1) cnt from order_87 a,order_87_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_clr like @ord_clr_min 
and ord_clr like @ord_clr_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
and ord_tsdx like @ord_tsdx_min 
and ord_tsdx like @ord_tsdx_max 
and ord_tssw like @ord_tssw_min 
and ord_tssw like @ord_tssw_max 
and ord_tsly like @ord_tsly_min 
and ord_tsly like @ord_tsly_max 
and ord_tslx like @ord_tslx_min 
and ord_tslx like @ord_tslx_max 
and ord_tswt like @ord_tswt_min 
and ord_tswt like @ord_tswt_max 
and ord_cljg like @ord_cljg_min 
and ord_cljg like @ord_cljg_max 

-- 返回所有ID
insert into @pobjs(ordid) select top(@amount) ord_id from order_87 a,order_87_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_clr like @ord_clr_min 
and ord_clr like @ord_clr_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
and ord_tsdx like @ord_tsdx_min 
and ord_tsdx like @ord_tsdx_max 
and ord_tssw like @ord_tssw_min 
and ord_tssw like @ord_tssw_max 
and ord_tsly like @ord_tsly_min 
and ord_tsly like @ord_tsly_max 
and ord_tslx like @ord_tslx_min 
and ord_tslx like @ord_tslx_max 
and ord_tswt like @ord_tswt_min 
and ord_tswt like @ord_tswt_max 
and ord_cljg like @ord_cljg_min 
and ord_cljg like @ord_cljg_max 
order by ord_date desc,ord_clr,ord_createdepart,ord_creator,ord_tsdx,ord_tssw,ord_tsly,ord_tslx,ord_tswt,ord_cljg
select ordid from @pobjs 
order by nid 
-- 返回第一页 
select top(@pagesize) a.* from order_87 a,@pobjs 
where a.ord_id=ordid 
and (a.ord_status=@status or @status='')
and (a.ord_finished>0 and a.ord_createcuid=@useruid) 
order by nid 
-- 返回权限 
select a.* from order_87_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 a.* from order_87 a,order_87_users 
where a.ord_cmpid=@cmpid 
and ordp_type='bizview' 
and ordp_cuid=@useruid 
and (a.ord_status=@status or @status='') 
and a.ord_finished>=2 
and ord_clr like @ord_clr_min 
and ord_clr like @ord_clr_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
and ord_tsdx like @ord_tsdx_min 
and ord_tsdx like @ord_tsdx_max 
and ord_tssw like @ord_tssw_min 
and ord_tssw like @ord_tssw_max 
and ord_tsly like @ord_tsly_min 
and ord_tsly like @ord_tsly_max 
and ord_tslx like @ord_tslx_min 
and ord_tslx like @ord_tslx_max 
and ord_tswt like @ord_tswt_min 
and ord_tswt like @ord_tswt_max 
and ord_cljg like @ord_cljg_min 
and ord_cljg like @ord_cljg_max 
order by ord_date desc,ord_clr,ord_createdepart,ord_creator,ord_tsdx,ord_tssw,ord_tsly,ord_tslx,ord_tswt,ord_cljg
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_87 a,order_87_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_clr like @ord_clr_min 
and ord_clr like @ord_clr_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
and ord_tsdx like @ord_tsdx_min 
and ord_tsdx like @ord_tsdx_max 
and ord_tssw like @ord_tssw_min 
and ord_tssw like @ord_tssw_max 
and ord_tsly like @ord_tsly_min 
and ord_tsly like @ord_tsly_max 
and ord_tslx like @ord_tslx_min 
and ord_tslx like @ord_tslx_max 
and ord_tswt like @ord_tswt_min 
and ord_tswt like @ord_tswt_max 
and ord_cljg like @ord_cljg_min 
and ord_cljg like @ord_cljg_max 
order by ord_date desc,ord_clr,ord_createdepart,ord_creator,ord_tsdx,ord_tssw,ord_tsly,ord_tslx,ord_tswt,ord_cljg
select count(1) resultcount  from datalinker where useruid=@useruid 
end else
begin
select count(1) cnt from order_87 a,order_87_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_clr like @ord_clr_min 
and ord_clr like @ord_clr_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
and ord_tsdx like @ord_tsdx_min 
and ord_tsdx like @ord_tsdx_max 
and ord_tssw like @ord_tssw_min 
and ord_tssw like @ord_tssw_max 
and ord_tsly like @ord_tsly_min 
and ord_tsly like @ord_tsly_max 
and ord_tslx like @ord_tslx_min 
and ord_tslx like @ord_tslx_max 
and ord_tswt like @ord_tswt_min 
and ord_tswt like @ord_tswt_max 
and ord_cljg like @ord_cljg_min 
and ord_cljg like @ord_cljg_max 

-- 返回所有ID
insert into @pobjs(ordid) select top(@amount) ord_id from order_87 a,order_87_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_clr like @ord_clr_min 
and ord_clr like @ord_clr_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
and ord_tsdx like @ord_tsdx_min 
and ord_tsdx like @ord_tsdx_max 
and ord_tssw like @ord_tssw_min 
and ord_tssw like @ord_tssw_max 
and ord_tsly like @ord_tsly_min 
and ord_tsly like @ord_tsly_max 
and ord_tslx like @ord_tslx_min 
and ord_tslx like @ord_tslx_max 
and ord_tswt like @ord_tswt_min 
and ord_tswt like @ord_tswt_max 
and ord_cljg like @ord_cljg_min 
and ord_cljg like @ord_cljg_max 
order by ord_date desc,ord_clr,ord_createdepart,ord_creator,ord_tsdx,ord_tssw,ord_tsly,ord_tslx,ord_tswt,ord_cljg
select ordid from @pobjs 
order by nid 
-- 返回第一页 
select top(@pagesize) a.* from order_87 a,@pobjs 
where a.ord_id=ordid 
and (a.ord_status=@status or @status='')
and a.ord_finished>=2 
order by nid 
-- 返回权限 
select a.* from order_87_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 a.* from order_87 a,order_87_users 
where a.ord_cmpid=@cmpid 
and ordp_type='bizview' 
and ordp_cuid=@useruid 
and (a.ord_status=@status or @status='') 
and (a.ord_finished>0 and a.ord_finished<=1)
and ord_clr like @ord_clr_min 
and ord_clr like @ord_clr_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
and ord_tsdx like @ord_tsdx_min 
and ord_tsdx like @ord_tsdx_max 
and ord_tssw like @ord_tssw_min 
and ord_tssw like @ord_tssw_max 
and ord_tsly like @ord_tsly_min 
and ord_tsly like @ord_tsly_max 
and ord_tslx like @ord_tslx_min 
and ord_tslx like @ord_tslx_max 
and ord_tswt like @ord_tswt_min 
and ord_tswt like @ord_tswt_max 
and ord_cljg like @ord_cljg_min 
and ord_cljg like @ord_cljg_max 
order by ord_date desc,ord_clr,ord_createdepart,ord_creator,ord_tsdx,ord_tssw,ord_tsly,ord_tslx,ord_tswt,ord_cljg
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_87 a,order_87_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_clr like @ord_clr_min 
and ord_clr like @ord_clr_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
and ord_tsdx like @ord_tsdx_min 
and ord_tsdx like @ord_tsdx_max 
and ord_tssw like @ord_tssw_min 
and ord_tssw like @ord_tssw_max 
and ord_tsly like @ord_tsly_min 
and ord_tsly like @ord_tsly_max 
and ord_tslx like @ord_tslx_min 
and ord_tslx like @ord_tslx_max 
and ord_tswt like @ord_tswt_min 
and ord_tswt like @ord_tswt_max 
and ord_cljg like @ord_cljg_min 
and ord_cljg like @ord_cljg_max 
order by ord_date desc,ord_clr,ord_createdepart,ord_creator,ord_tsdx,ord_tssw,ord_tsly,ord_tslx,ord_tswt,ord_cljg
select count(1) resultcount  from datalinker where useruid=@useruid 
end else
begin
select count(1) cnt from order_87 a,order_87_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_clr like @ord_clr_min 
and ord_clr like @ord_clr_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
and ord_tsdx like @ord_tsdx_min 
and ord_tsdx like @ord_tsdx_max 
and ord_tssw like @ord_tssw_min 
and ord_tssw like @ord_tssw_max 
and ord_tsly like @ord_tsly_min 
and ord_tsly like @ord_tsly_max 
and ord_tslx like @ord_tslx_min 
and ord_tslx like @ord_tslx_max 
and ord_tswt like @ord_tswt_min 
and ord_tswt like @ord_tswt_max 
and ord_cljg like @ord_cljg_min 
and ord_cljg like @ord_cljg_max 

-- 返回所有ID
insert into @pobjs(ordid) select top(@amount) ord_id from order_87 a,order_87_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_clr like @ord_clr_min 
and ord_clr like @ord_clr_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
and ord_tsdx like @ord_tsdx_min 
and ord_tsdx like @ord_tsdx_max 
and ord_tssw like @ord_tssw_min 
and ord_tssw like @ord_tssw_max 
and ord_tsly like @ord_tsly_min 
and ord_tsly like @ord_tsly_max 
and ord_tslx like @ord_tslx_min 
and ord_tslx like @ord_tslx_max 
and ord_tswt like @ord_tswt_min 
and ord_tswt like @ord_tswt_max 
and ord_cljg like @ord_cljg_min 
and ord_cljg like @ord_cljg_max 
order by ord_date desc,ord_clr,ord_createdepart,ord_creator,ord_tsdx,ord_tssw,ord_tsly,ord_tslx,ord_tswt,ord_cljg
select ordid from @pobjs 
order by nid 
-- 返回第一页 
select top(@pagesize) a.* from order_87 a,@pobjs 
where a.ord_id=ordid 
and (a.ord_status=@status or @status='')
and (a.ord_finished>0 and a.ord_finished<=1) 
order by nid 
-- 返回权限 
select a.* from order_87_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 a.* from order_87 a,order_87_users 
where a.ord_cmpid=@cmpid 
and ordp_type='bizview' 
and ordp_cuid=@useruid 
and (a.ord_status=@status or @status='') 
and ord_finished=-1 
and ord_clr like @ord_clr_min 
and ord_clr like @ord_clr_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
and ord_tsdx like @ord_tsdx_min 
and ord_tsdx like @ord_tsdx_max 
and ord_tssw like @ord_tssw_min 
and ord_tssw like @ord_tssw_max 
and ord_tsly like @ord_tsly_min 
and ord_tsly like @ord_tsly_max 
and ord_tslx like @ord_tslx_min 
and ord_tslx like @ord_tslx_max 
and ord_tswt like @ord_tswt_min 
and ord_tswt like @ord_tswt_max 
and ord_cljg like @ord_cljg_min 
and ord_cljg like @ord_cljg_max 
order by ord_date desc,ord_clr,ord_createdepart,ord_creator,ord_tsdx,ord_tssw,ord_tsly,ord_tslx,ord_tswt,ord_cljg
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_87 a,order_87_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_clr like @ord_clr_min 
and ord_clr like @ord_clr_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
and ord_tsdx like @ord_tsdx_min 
and ord_tsdx like @ord_tsdx_max 
and ord_tssw like @ord_tssw_min 
and ord_tssw like @ord_tssw_max 
and ord_tsly like @ord_tsly_min 
and ord_tsly like @ord_tsly_max 
and ord_tslx like @ord_tslx_min 
and ord_tslx like @ord_tslx_max 
and ord_tswt like @ord_tswt_min 
and ord_tswt like @ord_tswt_max 
and ord_cljg like @ord_cljg_min 
and ord_cljg like @ord_cljg_max 
order by ord_date desc,ord_clr,ord_createdepart,ord_creator,ord_tsdx,ord_tssw,ord_tsly,ord_tslx,ord_tswt,ord_cljg
select count(1) resultcount  from datalinker where useruid=@useruid 
end else
begin
select count(1) cnt from order_87 a,order_87_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_clr like @ord_clr_min 
and ord_clr like @ord_clr_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
and ord_tsdx like @ord_tsdx_min 
and ord_tsdx like @ord_tsdx_max 
and ord_tssw like @ord_tssw_min 
and ord_tssw like @ord_tssw_max 
and ord_tsly like @ord_tsly_min 
and ord_tsly like @ord_tsly_max 
and ord_tslx like @ord_tslx_min 
and ord_tslx like @ord_tslx_max 
and ord_tswt like @ord_tswt_min 
and ord_tswt like @ord_tswt_max 
and ord_cljg like @ord_cljg_min 
and ord_cljg like @ord_cljg_max 

-- 返回所有ID
insert into @pobjs(ordid) select top(@amount) ord_id from order_87 a,order_87_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_clr like @ord_clr_min 
and ord_clr like @ord_clr_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
and ord_tsdx like @ord_tsdx_min 
and ord_tsdx like @ord_tsdx_max 
and ord_tssw like @ord_tssw_min 
and ord_tssw like @ord_tssw_max 
and ord_tsly like @ord_tsly_min 
and ord_tsly like @ord_tsly_max 
and ord_tslx like @ord_tslx_min 
and ord_tslx like @ord_tslx_max 
and ord_tswt like @ord_tswt_min 
and ord_tswt like @ord_tswt_max 
and ord_cljg like @ord_cljg_min 
and ord_cljg like @ord_cljg_max 
order by ord_date desc,ord_clr,ord_createdepart,ord_creator,ord_tsdx,ord_tssw,ord_tsly,ord_tslx,ord_tswt,ord_cljg
select ordid from @pobjs 
order by nid 
-- 返回第一页 
select top(@pagesize) a.* from order_87 a,@pobjs 
where a.ord_id=ordid 
and (a.ord_status=@status or @status='')
and ord_finished=-1 
order by nid 
-- 返回权限 
select a.* from order_87_users a , @pobjs 
where ordp_ordid=ordid 
and nid<=@pagesize 
and ordp_cuid=@useruid 
end 
end 
set nocount off end