IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[order_45_query]') AND type = 'P')
DROP PROCEDURE [dbo].[order_45_query]
GO
CREATE  procedure order_45_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_gh_min varchar(100), @ord_gh_max varchar(100),@ord_createdepart_min nvarchar(50), @ord_createdepart_max nvarchar(50),@ord_lzzt_min varchar(100), @ord_lzzt_max varchar(100),@ord_zb_min varchar(100), @ord_zb_max varchar(100),@ord_zj_min varchar(100), @ord_zj_max varchar(100),@ord_yjlzrq_min datetime,@ord_yjlzrq_max datetime,@ord_zw_min varchar(100), @ord_zw_max varchar(100),@ord_tx_min varchar(100), @ord_tx_max varchar(100),@ord_rzrq_min datetime,@ord_rzrq_max datetime,@ord_lxdh_min varchar(100), @ord_lxdh_max varchar(100),@ord_creator_min nvarchar(50), @ord_creator_max 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 order_45 a, @pobjs
where ord_id=ordid
order by nid 
select a.* from order_45_users a , @pobjs 
where ordp_ordid=ordid and ordp_cuid=@useruid
return 
end 
if (@status is null or @status = '_none_') set @status = '' 


if (@ord_gh_min is null or @ord_gh_min = '_none_') set @ord_gh_min = '' 
if (@ord_gh_max is null or @ord_gh_max = '_none_') set @ord_gh_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_lzzt_min is null or @ord_lzzt_min = '_none_') set @ord_lzzt_min = '' 
if (@ord_lzzt_max is null or @ord_lzzt_max = '_none_') set @ord_lzzt_max = '' 

if (@ord_zb_min is null or @ord_zb_min = '_none_') set @ord_zb_min = '' 
if (@ord_zb_max is null or @ord_zb_max = '_none_') set @ord_zb_max = '' 

if (@ord_zj_min is null or @ord_zj_min = '_none_') set @ord_zj_min = '' 
if (@ord_zj_max is null or @ord_zj_max = '_none_') set @ord_zj_max = '' 


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

if (@ord_zw_min is null or @ord_zw_min = '_none_') set @ord_zw_min = '' 
if (@ord_zw_max is null or @ord_zw_max = '_none_') set @ord_zw_max = '' 

if (@ord_tx_min is null or @ord_tx_min = '_none_') set @ord_tx_min = '' 
if (@ord_tx_max is null or @ord_tx_max = '_none_') set @ord_tx_max = '' 


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

if (@ord_lxdh_min is null or @ord_lxdh_min = '_none_') set @ord_lxdh_min = '' 
if (@ord_lxdh_max is null or @ord_lxdh_max = '_none_') set @ord_lxdh_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_gh_min>'' 
begin 
if (charindex('%',@ord_gh_min) = 0 and @ord_gh_min!='_empty_') 
 begin
set @ord_gh_min = '%'+@ord_gh_min+'%';
 end else if (@ord_gh_min='_empty_') set @ord_gh_min='' 
end else
begin 
set @ord_gh_min = '%' 
end
if @ord_gh_max>'' 
begin 
if (charindex('%', @ord_gh_max) = 0 and @ord_gh_max!='_empty_') 
begin
set@ord_gh_max = '%'+@ord_gh_max+'%';
 end else if (@ord_gh_max='_empty_') set @ord_gh_max='' 
end else
begin 
set @ord_gh_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_lzzt_min>'' 
begin 
if (charindex('%',@ord_lzzt_min) = 0 and @ord_lzzt_min!='_empty_') 
 begin
set @ord_lzzt_min = '%'+@ord_lzzt_min+'%';
 end else if (@ord_lzzt_min='_empty_') set @ord_lzzt_min='' 
end else
begin 
set @ord_lzzt_min = '%' 
end
if @ord_lzzt_max>'' 
begin 
if (charindex('%', @ord_lzzt_max) = 0 and @ord_lzzt_max!='_empty_') 
begin
set@ord_lzzt_max = '%'+@ord_lzzt_max+'%';
 end else if (@ord_lzzt_max='_empty_') set @ord_lzzt_max='' 
end else
begin 
set @ord_lzzt_max = '%' 
end

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

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

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

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

if @ord_lxdh_min>'' 
begin 
if (charindex('%',@ord_lxdh_min) = 0 and @ord_lxdh_min!='_empty_') 
 begin
set @ord_lxdh_min = '%'+@ord_lxdh_min+'%';
 end else if (@ord_lxdh_min='_empty_') set @ord_lxdh_min='' 
end else
begin 
set @ord_lxdh_min = '%' 
end
if @ord_lxdh_max>'' 
begin 
if (charindex('%', @ord_lxdh_max) = 0 and @ord_lxdh_max!='_empty_') 
begin
set@ord_lxdh_max = '%'+@ord_lxdh_max+'%';
 end else if (@ord_lxdh_max='_empty_') set @ord_lxdh_max='' 
end else
begin 
set @ord_lxdh_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 (@finished = -11)
begin
if (@pagesize=10000)  -- 下载
begin
select a.* from order_45 a,order_45_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_gh like @ord_gh_min 
and ord_gh like @ord_gh_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_lzzt like @ord_lzzt_min 
and ord_lzzt like @ord_lzzt_max 
and ord_zb like @ord_zb_min 
and ord_zb like @ord_zb_max 
and ord_zj like @ord_zj_min 
and ord_zj like @ord_zj_max 
and ord_yjlzrq>=@ord_yjlzrq_min 
and ord_yjlzrq<=@ord_yjlzrq_max 
and ord_zw like @ord_zw_min 
and ord_zw like @ord_zw_max 
and ord_tx like @ord_tx_min 
and ord_tx like @ord_tx_max 
and ord_rzrq>=@ord_rzrq_min 
and ord_rzrq<=@ord_rzrq_max 
and ord_lxdh like @ord_lxdh_min 
and ord_lxdh like @ord_lxdh_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
order by ord_date desc,ord_gh,ord_createdepart,ord_lzzt,ord_zb,ord_zj,ord_yjlzrq desc,ord_zw,ord_tx,ord_rzrq desc,ord_lxdh,ord_creator
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_45 a,order_45_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_gh like @ord_gh_min 
and ord_gh like @ord_gh_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_lzzt like @ord_lzzt_min 
and ord_lzzt like @ord_lzzt_max 
and ord_zb like @ord_zb_min 
and ord_zb like @ord_zb_max 
and ord_zj like @ord_zj_min 
and ord_zj like @ord_zj_max 
and ord_yjlzrq>=@ord_yjlzrq_min 
and ord_yjlzrq<=@ord_yjlzrq_max 
and ord_zw like @ord_zw_min 
and ord_zw like @ord_zw_max 
and ord_tx like @ord_tx_min 
and ord_tx like @ord_tx_max 
and ord_rzrq>=@ord_rzrq_min 
and ord_rzrq<=@ord_rzrq_max 
and ord_lxdh like @ord_lxdh_min 
and ord_lxdh like @ord_lxdh_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
order by ord_date desc,ord_gh,ord_createdepart,ord_lzzt,ord_zb,ord_zj,ord_yjlzrq desc,ord_zw,ord_tx,ord_rzrq desc,ord_lxdh,ord_creator
select count(1) resultcount  from datalinker where useruid=@useruid 
end else
begin
select count(1) cnt from order_45 a,order_45_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_gh like @ord_gh_min 
and ord_gh like @ord_gh_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_lzzt like @ord_lzzt_min 
and ord_lzzt like @ord_lzzt_max 
and ord_zb like @ord_zb_min 
and ord_zb like @ord_zb_max 
and ord_zj like @ord_zj_min 
and ord_zj like @ord_zj_max 
and ord_yjlzrq>=@ord_yjlzrq_min 
and ord_yjlzrq<=@ord_yjlzrq_max 
and ord_zw like @ord_zw_min 
and ord_zw like @ord_zw_max 
and ord_tx like @ord_tx_min 
and ord_tx like @ord_tx_max 
and ord_rzrq>=@ord_rzrq_min 
and ord_rzrq<=@ord_rzrq_max 
and ord_lxdh like @ord_lxdh_min 
and ord_lxdh like @ord_lxdh_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 

-- 返回所有ID
insert into @pobjs(ordid) select top(@amount) ord_id from order_45 a,order_45_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_gh like @ord_gh_min 
and ord_gh like @ord_gh_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_lzzt like @ord_lzzt_min 
and ord_lzzt like @ord_lzzt_max 
and ord_zb like @ord_zb_min 
and ord_zb like @ord_zb_max 
and ord_zj like @ord_zj_min 
and ord_zj like @ord_zj_max 
and ord_yjlzrq>=@ord_yjlzrq_min 
and ord_yjlzrq<=@ord_yjlzrq_max 
and ord_zw like @ord_zw_min 
and ord_zw like @ord_zw_max 
and ord_tx like @ord_tx_min 
and ord_tx like @ord_tx_max 
and ord_rzrq>=@ord_rzrq_min 
and ord_rzrq<=@ord_rzrq_max 
and ord_lxdh like @ord_lxdh_min 
and ord_lxdh like @ord_lxdh_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
order by ord_date desc,ord_gh,ord_createdepart,ord_lzzt,ord_zb,ord_zj,ord_yjlzrq desc,ord_zw,ord_tx,ord_rzrq desc,ord_lxdh,ord_creator
select ordid from @pobjs 
order by nid 
-- 返回第一页 
select top(@pagesize) a.* from order_45 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_45_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_45 a,order_45_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_gh like @ord_gh_min 
and ord_gh like @ord_gh_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_lzzt like @ord_lzzt_min 
and ord_lzzt like @ord_lzzt_max 
and ord_zb like @ord_zb_min 
and ord_zb like @ord_zb_max 
and ord_zj like @ord_zj_min 
and ord_zj like @ord_zj_max 
and ord_yjlzrq>=@ord_yjlzrq_min 
and ord_yjlzrq<=@ord_yjlzrq_max 
and ord_zw like @ord_zw_min 
and ord_zw like @ord_zw_max 
and ord_tx like @ord_tx_min 
and ord_tx like @ord_tx_max 
and ord_rzrq>=@ord_rzrq_min 
and ord_rzrq<=@ord_rzrq_max 
and ord_lxdh like @ord_lxdh_min 
and ord_lxdh like @ord_lxdh_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
order by ord_date desc,ord_gh,ord_createdepart,ord_lzzt,ord_zb,ord_zj,ord_yjlzrq desc,ord_zw,ord_tx,ord_rzrq desc,ord_lxdh,ord_creator
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_45 a,order_45_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_gh like @ord_gh_min 
and ord_gh like @ord_gh_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_lzzt like @ord_lzzt_min 
and ord_lzzt like @ord_lzzt_max 
and ord_zb like @ord_zb_min 
and ord_zb like @ord_zb_max 
and ord_zj like @ord_zj_min 
and ord_zj like @ord_zj_max 
and ord_yjlzrq>=@ord_yjlzrq_min 
and ord_yjlzrq<=@ord_yjlzrq_max 
and ord_zw like @ord_zw_min 
and ord_zw like @ord_zw_max 
and ord_tx like @ord_tx_min 
and ord_tx like @ord_tx_max 
and ord_rzrq>=@ord_rzrq_min 
and ord_rzrq<=@ord_rzrq_max 
and ord_lxdh like @ord_lxdh_min 
and ord_lxdh like @ord_lxdh_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
order by ord_date desc,ord_gh,ord_createdepart,ord_lzzt,ord_zb,ord_zj,ord_yjlzrq desc,ord_zw,ord_tx,ord_rzrq desc,ord_lxdh,ord_creator
select count(1) resultcount  from datalinker where useruid=@useruid 
end else
begin
select count(1) cnt from order_45 a,order_45_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_gh like @ord_gh_min 
and ord_gh like @ord_gh_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_lzzt like @ord_lzzt_min 
and ord_lzzt like @ord_lzzt_max 
and ord_zb like @ord_zb_min 
and ord_zb like @ord_zb_max 
and ord_zj like @ord_zj_min 
and ord_zj like @ord_zj_max 
and ord_yjlzrq>=@ord_yjlzrq_min 
and ord_yjlzrq<=@ord_yjlzrq_max 
and ord_zw like @ord_zw_min 
and ord_zw like @ord_zw_max 
and ord_tx like @ord_tx_min 
and ord_tx like @ord_tx_max 
and ord_rzrq>=@ord_rzrq_min 
and ord_rzrq<=@ord_rzrq_max 
and ord_lxdh like @ord_lxdh_min 
and ord_lxdh like @ord_lxdh_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 

-- 返回所有ID
insert into @pobjs(ordid) select top(@amount) ord_id from order_45 a,order_45_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_gh like @ord_gh_min 
and ord_gh like @ord_gh_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_lzzt like @ord_lzzt_min 
and ord_lzzt like @ord_lzzt_max 
and ord_zb like @ord_zb_min 
and ord_zb like @ord_zb_max 
and ord_zj like @ord_zj_min 
and ord_zj like @ord_zj_max 
and ord_yjlzrq>=@ord_yjlzrq_min 
and ord_yjlzrq<=@ord_yjlzrq_max 
and ord_zw like @ord_zw_min 
and ord_zw like @ord_zw_max 
and ord_tx like @ord_tx_min 
and ord_tx like @ord_tx_max 
and ord_rzrq>=@ord_rzrq_min 
and ord_rzrq<=@ord_rzrq_max 
and ord_lxdh like @ord_lxdh_min 
and ord_lxdh like @ord_lxdh_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
order by ord_date desc,ord_gh,ord_createdepart,ord_lzzt,ord_zb,ord_zj,ord_yjlzrq desc,ord_zw,ord_tx,ord_rzrq desc,ord_lxdh,ord_creator
select ordid from @pobjs 
order by nid 
-- 返回第一页 
select top(@pagesize) a.* from order_45 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_45_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_45 a,order_45_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_gh like @ord_gh_min 
and ord_gh like @ord_gh_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_lzzt like @ord_lzzt_min 
and ord_lzzt like @ord_lzzt_max 
and ord_zb like @ord_zb_min 
and ord_zb like @ord_zb_max 
and ord_zj like @ord_zj_min 
and ord_zj like @ord_zj_max 
and ord_yjlzrq>=@ord_yjlzrq_min 
and ord_yjlzrq<=@ord_yjlzrq_max 
and ord_zw like @ord_zw_min 
and ord_zw like @ord_zw_max 
and ord_tx like @ord_tx_min 
and ord_tx like @ord_tx_max 
and ord_rzrq>=@ord_rzrq_min 
and ord_rzrq<=@ord_rzrq_max 
and ord_lxdh like @ord_lxdh_min 
and ord_lxdh like @ord_lxdh_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
order by ord_date desc,ord_gh,ord_createdepart,ord_lzzt,ord_zb,ord_zj,ord_yjlzrq desc,ord_zw,ord_tx,ord_rzrq desc,ord_lxdh,ord_creator
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_45 a,order_45_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_gh like @ord_gh_min 
and ord_gh like @ord_gh_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_lzzt like @ord_lzzt_min 
and ord_lzzt like @ord_lzzt_max 
and ord_zb like @ord_zb_min 
and ord_zb like @ord_zb_max 
and ord_zj like @ord_zj_min 
and ord_zj like @ord_zj_max 
and ord_yjlzrq>=@ord_yjlzrq_min 
and ord_yjlzrq<=@ord_yjlzrq_max 
and ord_zw like @ord_zw_min 
and ord_zw like @ord_zw_max 
and ord_tx like @ord_tx_min 
and ord_tx like @ord_tx_max 
and ord_rzrq>=@ord_rzrq_min 
and ord_rzrq<=@ord_rzrq_max 
and ord_lxdh like @ord_lxdh_min 
and ord_lxdh like @ord_lxdh_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
order by ord_date desc,ord_gh,ord_createdepart,ord_lzzt,ord_zb,ord_zj,ord_yjlzrq desc,ord_zw,ord_tx,ord_rzrq desc,ord_lxdh,ord_creator
select count(1) resultcount  from datalinker where useruid=@useruid 
end else
begin
select count(1) cnt from order_45 a,order_45_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_gh like @ord_gh_min 
and ord_gh like @ord_gh_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_lzzt like @ord_lzzt_min 
and ord_lzzt like @ord_lzzt_max 
and ord_zb like @ord_zb_min 
and ord_zb like @ord_zb_max 
and ord_zj like @ord_zj_min 
and ord_zj like @ord_zj_max 
and ord_yjlzrq>=@ord_yjlzrq_min 
and ord_yjlzrq<=@ord_yjlzrq_max 
and ord_zw like @ord_zw_min 
and ord_zw like @ord_zw_max 
and ord_tx like @ord_tx_min 
and ord_tx like @ord_tx_max 
and ord_rzrq>=@ord_rzrq_min 
and ord_rzrq<=@ord_rzrq_max 
and ord_lxdh like @ord_lxdh_min 
and ord_lxdh like @ord_lxdh_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 

-- 返回所有ID
insert into @pobjs(ordid) select top(@amount) ord_id from order_45 a,order_45_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_gh like @ord_gh_min 
and ord_gh like @ord_gh_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_lzzt like @ord_lzzt_min 
and ord_lzzt like @ord_lzzt_max 
and ord_zb like @ord_zb_min 
and ord_zb like @ord_zb_max 
and ord_zj like @ord_zj_min 
and ord_zj like @ord_zj_max 
and ord_yjlzrq>=@ord_yjlzrq_min 
and ord_yjlzrq<=@ord_yjlzrq_max 
and ord_zw like @ord_zw_min 
and ord_zw like @ord_zw_max 
and ord_tx like @ord_tx_min 
and ord_tx like @ord_tx_max 
and ord_rzrq>=@ord_rzrq_min 
and ord_rzrq<=@ord_rzrq_max 
and ord_lxdh like @ord_lxdh_min 
and ord_lxdh like @ord_lxdh_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
order by ord_date desc,ord_gh,ord_createdepart,ord_lzzt,ord_zb,ord_zj,ord_yjlzrq desc,ord_zw,ord_tx,ord_rzrq desc,ord_lxdh,ord_creator
select ordid from @pobjs 
order by nid 
-- 返回第一页 
select top(@pagesize) a.* from order_45 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_45_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_45 a,order_45_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_gh like @ord_gh_min 
and ord_gh like @ord_gh_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_lzzt like @ord_lzzt_min 
and ord_lzzt like @ord_lzzt_max 
and ord_zb like @ord_zb_min 
and ord_zb like @ord_zb_max 
and ord_zj like @ord_zj_min 
and ord_zj like @ord_zj_max 
and ord_yjlzrq>=@ord_yjlzrq_min 
and ord_yjlzrq<=@ord_yjlzrq_max 
and ord_zw like @ord_zw_min 
and ord_zw like @ord_zw_max 
and ord_tx like @ord_tx_min 
and ord_tx like @ord_tx_max 
and ord_rzrq>=@ord_rzrq_min 
and ord_rzrq<=@ord_rzrq_max 
and ord_lxdh like @ord_lxdh_min 
and ord_lxdh like @ord_lxdh_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
order by ord_date desc,ord_gh,ord_createdepart,ord_lzzt,ord_zb,ord_zj,ord_yjlzrq desc,ord_zw,ord_tx,ord_rzrq desc,ord_lxdh,ord_creator
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_45 a,order_45_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_gh like @ord_gh_min 
and ord_gh like @ord_gh_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_lzzt like @ord_lzzt_min 
and ord_lzzt like @ord_lzzt_max 
and ord_zb like @ord_zb_min 
and ord_zb like @ord_zb_max 
and ord_zj like @ord_zj_min 
and ord_zj like @ord_zj_max 
and ord_yjlzrq>=@ord_yjlzrq_min 
and ord_yjlzrq<=@ord_yjlzrq_max 
and ord_zw like @ord_zw_min 
and ord_zw like @ord_zw_max 
and ord_tx like @ord_tx_min 
and ord_tx like @ord_tx_max 
and ord_rzrq>=@ord_rzrq_min 
and ord_rzrq<=@ord_rzrq_max 
and ord_lxdh like @ord_lxdh_min 
and ord_lxdh like @ord_lxdh_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
order by ord_date desc,ord_gh,ord_createdepart,ord_lzzt,ord_zb,ord_zj,ord_yjlzrq desc,ord_zw,ord_tx,ord_rzrq desc,ord_lxdh,ord_creator
select count(1) resultcount  from datalinker where useruid=@useruid 
end else
begin
select count(1) cnt from order_45 a,order_45_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_gh like @ord_gh_min 
and ord_gh like @ord_gh_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_lzzt like @ord_lzzt_min 
and ord_lzzt like @ord_lzzt_max 
and ord_zb like @ord_zb_min 
and ord_zb like @ord_zb_max 
and ord_zj like @ord_zj_min 
and ord_zj like @ord_zj_max 
and ord_yjlzrq>=@ord_yjlzrq_min 
and ord_yjlzrq<=@ord_yjlzrq_max 
and ord_zw like @ord_zw_min 
and ord_zw like @ord_zw_max 
and ord_tx like @ord_tx_min 
and ord_tx like @ord_tx_max 
and ord_rzrq>=@ord_rzrq_min 
and ord_rzrq<=@ord_rzrq_max 
and ord_lxdh like @ord_lxdh_min 
and ord_lxdh like @ord_lxdh_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 

-- 返回所有ID
insert into @pobjs(ordid) select top(@amount) ord_id from order_45 a,order_45_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_gh like @ord_gh_min 
and ord_gh like @ord_gh_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_lzzt like @ord_lzzt_min 
and ord_lzzt like @ord_lzzt_max 
and ord_zb like @ord_zb_min 
and ord_zb like @ord_zb_max 
and ord_zj like @ord_zj_min 
and ord_zj like @ord_zj_max 
and ord_yjlzrq>=@ord_yjlzrq_min 
and ord_yjlzrq<=@ord_yjlzrq_max 
and ord_zw like @ord_zw_min 
and ord_zw like @ord_zw_max 
and ord_tx like @ord_tx_min 
and ord_tx like @ord_tx_max 
and ord_rzrq>=@ord_rzrq_min 
and ord_rzrq<=@ord_rzrq_max 
and ord_lxdh like @ord_lxdh_min 
and ord_lxdh like @ord_lxdh_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
order by ord_date desc,ord_gh,ord_createdepart,ord_lzzt,ord_zb,ord_zj,ord_yjlzrq desc,ord_zw,ord_tx,ord_rzrq desc,ord_lxdh,ord_creator
select ordid from @pobjs 
order by nid 
-- 返回第一页 
select top(@pagesize) a.* from order_45 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_45_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_45 a,order_45_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_gh like @ord_gh_min 
and ord_gh like @ord_gh_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_lzzt like @ord_lzzt_min 
and ord_lzzt like @ord_lzzt_max 
and ord_zb like @ord_zb_min 
and ord_zb like @ord_zb_max 
and ord_zj like @ord_zj_min 
and ord_zj like @ord_zj_max 
and ord_yjlzrq>=@ord_yjlzrq_min 
and ord_yjlzrq<=@ord_yjlzrq_max 
and ord_zw like @ord_zw_min 
and ord_zw like @ord_zw_max 
and ord_tx like @ord_tx_min 
and ord_tx like @ord_tx_max 
and ord_rzrq>=@ord_rzrq_min 
and ord_rzrq<=@ord_rzrq_max 
and ord_lxdh like @ord_lxdh_min 
and ord_lxdh like @ord_lxdh_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
order by ord_date desc,ord_gh,ord_createdepart,ord_lzzt,ord_zb,ord_zj,ord_yjlzrq desc,ord_zw,ord_tx,ord_rzrq desc,ord_lxdh,ord_creator
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_45 a,order_45_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_gh like @ord_gh_min 
and ord_gh like @ord_gh_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_lzzt like @ord_lzzt_min 
and ord_lzzt like @ord_lzzt_max 
and ord_zb like @ord_zb_min 
and ord_zb like @ord_zb_max 
and ord_zj like @ord_zj_min 
and ord_zj like @ord_zj_max 
and ord_yjlzrq>=@ord_yjlzrq_min 
and ord_yjlzrq<=@ord_yjlzrq_max 
and ord_zw like @ord_zw_min 
and ord_zw like @ord_zw_max 
and ord_tx like @ord_tx_min 
and ord_tx like @ord_tx_max 
and ord_rzrq>=@ord_rzrq_min 
and ord_rzrq<=@ord_rzrq_max 
and ord_lxdh like @ord_lxdh_min 
and ord_lxdh like @ord_lxdh_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
order by ord_date desc,ord_gh,ord_createdepart,ord_lzzt,ord_zb,ord_zj,ord_yjlzrq desc,ord_zw,ord_tx,ord_rzrq desc,ord_lxdh,ord_creator
select count(1) resultcount  from datalinker where useruid=@useruid 
end else
begin
select count(1) cnt from order_45 a,order_45_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_gh like @ord_gh_min 
and ord_gh like @ord_gh_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_lzzt like @ord_lzzt_min 
and ord_lzzt like @ord_lzzt_max 
and ord_zb like @ord_zb_min 
and ord_zb like @ord_zb_max 
and ord_zj like @ord_zj_min 
and ord_zj like @ord_zj_max 
and ord_yjlzrq>=@ord_yjlzrq_min 
and ord_yjlzrq<=@ord_yjlzrq_max 
and ord_zw like @ord_zw_min 
and ord_zw like @ord_zw_max 
and ord_tx like @ord_tx_min 
and ord_tx like @ord_tx_max 
and ord_rzrq>=@ord_rzrq_min 
and ord_rzrq<=@ord_rzrq_max 
and ord_lxdh like @ord_lxdh_min 
and ord_lxdh like @ord_lxdh_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 

-- 返回所有ID
insert into @pobjs(ordid) select top(@amount) ord_id from order_45 a,order_45_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_gh like @ord_gh_min 
and ord_gh like @ord_gh_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_lzzt like @ord_lzzt_min 
and ord_lzzt like @ord_lzzt_max 
and ord_zb like @ord_zb_min 
and ord_zb like @ord_zb_max 
and ord_zj like @ord_zj_min 
and ord_zj like @ord_zj_max 
and ord_yjlzrq>=@ord_yjlzrq_min 
and ord_yjlzrq<=@ord_yjlzrq_max 
and ord_zw like @ord_zw_min 
and ord_zw like @ord_zw_max 
and ord_tx like @ord_tx_min 
and ord_tx like @ord_tx_max 
and ord_rzrq>=@ord_rzrq_min 
and ord_rzrq<=@ord_rzrq_max 
and ord_lxdh like @ord_lxdh_min 
and ord_lxdh like @ord_lxdh_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
order by ord_date desc,ord_gh,ord_createdepart,ord_lzzt,ord_zb,ord_zj,ord_yjlzrq desc,ord_zw,ord_tx,ord_rzrq desc,ord_lxdh,ord_creator
select ordid from @pobjs 
order by nid 
-- 返回第一页 
select top(@pagesize) a.* from order_45 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_45_users a , @pobjs 
where ordp_ordid=ordid 
and nid<=@pagesize 
and ordp_cuid=@useruid 
end 
end 
set nocount off end