IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[order_140_query]') AND type = 'P')
DROP PROCEDURE [dbo].[order_140_query]
GO
CREATE  procedure order_140_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_zdrxm_min varchar(100), @ord_zdrxm_max varchar(100),@ord_kyrq_min datetime,@ord_kyrq_max datetime,@ord_jdr_min varchar(100), @ord_jdr_max varchar(100),@ord_dpbh_min varchar(100), @ord_dpbh_max varchar(100),@ord_createdepart_min nvarchar(50), @ord_createdepart_max nvarchar(50),@ord_date_min datetime,@ord_date_max datetime,@ord_zdlx_min varchar(100), @ord_zdlx_max varchar(100),@ord_zdmc_min varchar(100), @ord_zdmc_max varchar(100),@ord_zdrq_min datetime,@ord_zdrq_max datetime,@ord_status_min nvarchar(30), @ord_status_max nvarchar(30),@ord_lxdh_min varchar(100), @ord_lxdh_max varchar(100),@ord_no_min varchar(50), @ord_no_max varchar(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_140 a, @pobjs
where ord_id=ordid
order by nid 
select a.* from order_140_users a , @pobjs 
where ordp_ordid=ordid and ordp_cuid=@useruid
return 
end 
if (@status is null or @status = '_none_') set @status = '' 


if (@ord_zdrxm_min is null or @ord_zdrxm_min = '_none_') set @ord_zdrxm_min = '' 
if (@ord_zdrxm_max is null or @ord_zdrxm_max = '_none_') set @ord_zdrxm_max = '' 


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

if (@ord_jdr_min is null or @ord_jdr_min = '_none_') set @ord_jdr_min = '' 
if (@ord_jdr_max is null or @ord_jdr_max = '_none_') set @ord_jdr_max = '' 

if (@ord_dpbh_min is null or @ord_dpbh_min = '_none_') set @ord_dpbh_min = '' 
if (@ord_dpbh_max is null or @ord_dpbh_max = '_none_') set @ord_dpbh_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_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_zdlx_min is null or @ord_zdlx_min = '_none_') set @ord_zdlx_min = '' 
if (@ord_zdlx_max is null or @ord_zdlx_max = '_none_') set @ord_zdlx_max = '' 

if (@ord_zdmc_min is null or @ord_zdmc_min = '_none_') set @ord_zdmc_min = '' 
if (@ord_zdmc_max is null or @ord_zdmc_max = '_none_') set @ord_zdmc_max = '' 


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

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_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_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_zdrxm_min>'' 
begin 
if (charindex('%',@ord_zdrxm_min) = 0 and @ord_zdrxm_min!='_empty_') 
 begin
set @ord_zdrxm_min = '%'+@ord_zdrxm_min+'%';
 end else if (@ord_zdrxm_min='_empty_') set @ord_zdrxm_min='' 
end else
begin 
set @ord_zdrxm_min = '%' 
end
if @ord_zdrxm_max>'' 
begin 
if (charindex('%', @ord_zdrxm_max) = 0 and @ord_zdrxm_max!='_empty_') 
begin
set@ord_zdrxm_max = '%'+@ord_zdrxm_max+'%';
 end else if (@ord_zdrxm_max='_empty_') set @ord_zdrxm_max='' 
end else
begin 
set @ord_zdrxm_max = '%' 
end

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

if @ord_dpbh_min>'' 
begin 
if (charindex('%',@ord_dpbh_min) = 0 and @ord_dpbh_min!='_empty_') 
 begin
set @ord_dpbh_min = '%'+@ord_dpbh_min+'%';
 end else if (@ord_dpbh_min='_empty_') set @ord_dpbh_min='' 
end else
begin 
set @ord_dpbh_min = '%' 
end
if @ord_dpbh_max>'' 
begin 
if (charindex('%', @ord_dpbh_max) = 0 and @ord_dpbh_max!='_empty_') 
begin
set@ord_dpbh_max = '%'+@ord_dpbh_max+'%';
 end else if (@ord_dpbh_max='_empty_') set @ord_dpbh_max='' 
end else
begin 
set @ord_dpbh_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_zdlx_min>'' 
begin 
if (charindex('%',@ord_zdlx_min) = 0 and @ord_zdlx_min!='_empty_') 
 begin
set @ord_zdlx_min = '%'+@ord_zdlx_min+'%';
 end else if (@ord_zdlx_min='_empty_') set @ord_zdlx_min='' 
end else
begin 
set @ord_zdlx_min = '%' 
end
if @ord_zdlx_max>'' 
begin 
if (charindex('%', @ord_zdlx_max) = 0 and @ord_zdlx_max!='_empty_') 
begin
set@ord_zdlx_max = '%'+@ord_zdlx_max+'%';
 end else if (@ord_zdlx_max='_empty_') set @ord_zdlx_max='' 
end else
begin 
set @ord_zdlx_max = '%' 
end

if @ord_zdmc_min>'' 
begin 
if (charindex('%',@ord_zdmc_min) = 0 and @ord_zdmc_min!='_empty_') 
 begin
set @ord_zdmc_min = '%'+@ord_zdmc_min+'%';
 end else if (@ord_zdmc_min='_empty_') set @ord_zdmc_min='' 
end else
begin 
set @ord_zdmc_min = '%' 
end
if @ord_zdmc_max>'' 
begin 
if (charindex('%', @ord_zdmc_max) = 0 and @ord_zdmc_max!='_empty_') 
begin
set@ord_zdmc_max = '%'+@ord_zdmc_max+'%';
 end else if (@ord_zdmc_max='_empty_') set @ord_zdmc_max='' 
end else
begin 
set @ord_zdmc_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_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_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 (@finished = -11)
begin
if (@pagesize=10000)  -- 下载
begin
select a.* from order_140 a,order_140_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_zdrxm like @ord_zdrxm_min 
and ord_zdrxm like @ord_zdrxm_max 
and ord_kyrq>=@ord_kyrq_min 
and ord_kyrq<=@ord_kyrq_max 
and ord_jdr like @ord_jdr_min 
and ord_jdr like @ord_jdr_max 
and ord_dpbh like @ord_dpbh_min 
and ord_dpbh like @ord_dpbh_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_date>=@ord_date_min 
and ord_date<=@ord_date_max 
and ord_zdlx like @ord_zdlx_min 
and ord_zdlx like @ord_zdlx_max 
and ord_zdmc like @ord_zdmc_min 
and ord_zdmc like @ord_zdmc_max 
and ord_zdrq>=@ord_zdrq_min 
and ord_zdrq<=@ord_zdrq_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_lxdh like @ord_lxdh_min 
and ord_lxdh like @ord_lxdh_max 
and ord_no like @ord_no_min 
and ord_no like @ord_no_max 
order by ord_date desc,ord_zdrxm,ord_kyrq desc,ord_jdr,ord_dpbh,ord_createdepart,ord_zdlx,ord_zdmc,ord_zdrq desc,ord_status,ord_lxdh,ord_no
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_140 a,order_140_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_zdrxm like @ord_zdrxm_min 
and ord_zdrxm like @ord_zdrxm_max 
and ord_kyrq>=@ord_kyrq_min 
and ord_kyrq<=@ord_kyrq_max 
and ord_jdr like @ord_jdr_min 
and ord_jdr like @ord_jdr_max 
and ord_dpbh like @ord_dpbh_min 
and ord_dpbh like @ord_dpbh_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_date>=@ord_date_min 
and ord_date<=@ord_date_max 
and ord_zdlx like @ord_zdlx_min 
and ord_zdlx like @ord_zdlx_max 
and ord_zdmc like @ord_zdmc_min 
and ord_zdmc like @ord_zdmc_max 
and ord_zdrq>=@ord_zdrq_min 
and ord_zdrq<=@ord_zdrq_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_lxdh like @ord_lxdh_min 
and ord_lxdh like @ord_lxdh_max 
and ord_no like @ord_no_min 
and ord_no like @ord_no_max 
order by ord_date desc,ord_zdrxm,ord_kyrq desc,ord_jdr,ord_dpbh,ord_createdepart,ord_zdlx,ord_zdmc,ord_zdrq desc,ord_status,ord_lxdh,ord_no
select count(1) resultcount  from datalinker where useruid=@useruid 
end else
begin
select count(1) cnt from order_140 a,order_140_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_zdrxm like @ord_zdrxm_min 
and ord_zdrxm like @ord_zdrxm_max 
and ord_kyrq>=@ord_kyrq_min 
and ord_kyrq<=@ord_kyrq_max 
and ord_jdr like @ord_jdr_min 
and ord_jdr like @ord_jdr_max 
and ord_dpbh like @ord_dpbh_min 
and ord_dpbh like @ord_dpbh_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_date>=@ord_date_min 
and ord_date<=@ord_date_max 
and ord_zdlx like @ord_zdlx_min 
and ord_zdlx like @ord_zdlx_max 
and ord_zdmc like @ord_zdmc_min 
and ord_zdmc like @ord_zdmc_max 
and ord_zdrq>=@ord_zdrq_min 
and ord_zdrq<=@ord_zdrq_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_lxdh like @ord_lxdh_min 
and ord_lxdh like @ord_lxdh_max 
and ord_no like @ord_no_min 
and ord_no like @ord_no_max 

-- 返回所有ID
insert into @pobjs(ordid) select top(@amount) ord_id from order_140 a,order_140_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_zdrxm like @ord_zdrxm_min 
and ord_zdrxm like @ord_zdrxm_max 
and ord_kyrq>=@ord_kyrq_min 
and ord_kyrq<=@ord_kyrq_max 
and ord_jdr like @ord_jdr_min 
and ord_jdr like @ord_jdr_max 
and ord_dpbh like @ord_dpbh_min 
and ord_dpbh like @ord_dpbh_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_date>=@ord_date_min 
and ord_date<=@ord_date_max 
and ord_zdlx like @ord_zdlx_min 
and ord_zdlx like @ord_zdlx_max 
and ord_zdmc like @ord_zdmc_min 
and ord_zdmc like @ord_zdmc_max 
and ord_zdrq>=@ord_zdrq_min 
and ord_zdrq<=@ord_zdrq_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_lxdh like @ord_lxdh_min 
and ord_lxdh like @ord_lxdh_max 
and ord_no like @ord_no_min 
and ord_no like @ord_no_max 
order by ord_date desc,ord_zdrxm,ord_kyrq desc,ord_jdr,ord_dpbh,ord_createdepart,ord_zdlx,ord_zdmc,ord_zdrq desc,ord_status,ord_lxdh,ord_no
select ordid from @pobjs 
order by nid 
-- 返回第一页 
select top(@pagesize) a.* from order_140 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_140_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_140 a,order_140_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_zdrxm like @ord_zdrxm_min 
and ord_zdrxm like @ord_zdrxm_max 
and ord_kyrq>=@ord_kyrq_min 
and ord_kyrq<=@ord_kyrq_max 
and ord_jdr like @ord_jdr_min 
and ord_jdr like @ord_jdr_max 
and ord_dpbh like @ord_dpbh_min 
and ord_dpbh like @ord_dpbh_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_date>=@ord_date_min 
and ord_date<=@ord_date_max 
and ord_zdlx like @ord_zdlx_min 
and ord_zdlx like @ord_zdlx_max 
and ord_zdmc like @ord_zdmc_min 
and ord_zdmc like @ord_zdmc_max 
and ord_zdrq>=@ord_zdrq_min 
and ord_zdrq<=@ord_zdrq_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_lxdh like @ord_lxdh_min 
and ord_lxdh like @ord_lxdh_max 
and ord_no like @ord_no_min 
and ord_no like @ord_no_max 
order by ord_date desc,ord_zdrxm,ord_kyrq desc,ord_jdr,ord_dpbh,ord_createdepart,ord_zdlx,ord_zdmc,ord_zdrq desc,ord_status,ord_lxdh,ord_no
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_140 a,order_140_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_zdrxm like @ord_zdrxm_min 
and ord_zdrxm like @ord_zdrxm_max 
and ord_kyrq>=@ord_kyrq_min 
and ord_kyrq<=@ord_kyrq_max 
and ord_jdr like @ord_jdr_min 
and ord_jdr like @ord_jdr_max 
and ord_dpbh like @ord_dpbh_min 
and ord_dpbh like @ord_dpbh_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_date>=@ord_date_min 
and ord_date<=@ord_date_max 
and ord_zdlx like @ord_zdlx_min 
and ord_zdlx like @ord_zdlx_max 
and ord_zdmc like @ord_zdmc_min 
and ord_zdmc like @ord_zdmc_max 
and ord_zdrq>=@ord_zdrq_min 
and ord_zdrq<=@ord_zdrq_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_lxdh like @ord_lxdh_min 
and ord_lxdh like @ord_lxdh_max 
and ord_no like @ord_no_min 
and ord_no like @ord_no_max 
order by ord_date desc,ord_zdrxm,ord_kyrq desc,ord_jdr,ord_dpbh,ord_createdepart,ord_zdlx,ord_zdmc,ord_zdrq desc,ord_status,ord_lxdh,ord_no
select count(1) resultcount  from datalinker where useruid=@useruid 
end else
begin
select count(1) cnt from order_140 a,order_140_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_zdrxm like @ord_zdrxm_min 
and ord_zdrxm like @ord_zdrxm_max 
and ord_kyrq>=@ord_kyrq_min 
and ord_kyrq<=@ord_kyrq_max 
and ord_jdr like @ord_jdr_min 
and ord_jdr like @ord_jdr_max 
and ord_dpbh like @ord_dpbh_min 
and ord_dpbh like @ord_dpbh_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_date>=@ord_date_min 
and ord_date<=@ord_date_max 
and ord_zdlx like @ord_zdlx_min 
and ord_zdlx like @ord_zdlx_max 
and ord_zdmc like @ord_zdmc_min 
and ord_zdmc like @ord_zdmc_max 
and ord_zdrq>=@ord_zdrq_min 
and ord_zdrq<=@ord_zdrq_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_lxdh like @ord_lxdh_min 
and ord_lxdh like @ord_lxdh_max 
and ord_no like @ord_no_min 
and ord_no like @ord_no_max 

-- 返回所有ID
insert into @pobjs(ordid) select top(@amount) ord_id from order_140 a,order_140_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_zdrxm like @ord_zdrxm_min 
and ord_zdrxm like @ord_zdrxm_max 
and ord_kyrq>=@ord_kyrq_min 
and ord_kyrq<=@ord_kyrq_max 
and ord_jdr like @ord_jdr_min 
and ord_jdr like @ord_jdr_max 
and ord_dpbh like @ord_dpbh_min 
and ord_dpbh like @ord_dpbh_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_date>=@ord_date_min 
and ord_date<=@ord_date_max 
and ord_zdlx like @ord_zdlx_min 
and ord_zdlx like @ord_zdlx_max 
and ord_zdmc like @ord_zdmc_min 
and ord_zdmc like @ord_zdmc_max 
and ord_zdrq>=@ord_zdrq_min 
and ord_zdrq<=@ord_zdrq_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_lxdh like @ord_lxdh_min 
and ord_lxdh like @ord_lxdh_max 
and ord_no like @ord_no_min 
and ord_no like @ord_no_max 
order by ord_date desc,ord_zdrxm,ord_kyrq desc,ord_jdr,ord_dpbh,ord_createdepart,ord_zdlx,ord_zdmc,ord_zdrq desc,ord_status,ord_lxdh,ord_no
select ordid from @pobjs 
order by nid 
-- 返回第一页 
select top(@pagesize) a.* from order_140 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_140_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_140 a,order_140_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_zdrxm like @ord_zdrxm_min 
and ord_zdrxm like @ord_zdrxm_max 
and ord_kyrq>=@ord_kyrq_min 
and ord_kyrq<=@ord_kyrq_max 
and ord_jdr like @ord_jdr_min 
and ord_jdr like @ord_jdr_max 
and ord_dpbh like @ord_dpbh_min 
and ord_dpbh like @ord_dpbh_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_date>=@ord_date_min 
and ord_date<=@ord_date_max 
and ord_zdlx like @ord_zdlx_min 
and ord_zdlx like @ord_zdlx_max 
and ord_zdmc like @ord_zdmc_min 
and ord_zdmc like @ord_zdmc_max 
and ord_zdrq>=@ord_zdrq_min 
and ord_zdrq<=@ord_zdrq_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_lxdh like @ord_lxdh_min 
and ord_lxdh like @ord_lxdh_max 
and ord_no like @ord_no_min 
and ord_no like @ord_no_max 
order by ord_date desc,ord_zdrxm,ord_kyrq desc,ord_jdr,ord_dpbh,ord_createdepart,ord_zdlx,ord_zdmc,ord_zdrq desc,ord_status,ord_lxdh,ord_no
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_140 a,order_140_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_zdrxm like @ord_zdrxm_min 
and ord_zdrxm like @ord_zdrxm_max 
and ord_kyrq>=@ord_kyrq_min 
and ord_kyrq<=@ord_kyrq_max 
and ord_jdr like @ord_jdr_min 
and ord_jdr like @ord_jdr_max 
and ord_dpbh like @ord_dpbh_min 
and ord_dpbh like @ord_dpbh_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_date>=@ord_date_min 
and ord_date<=@ord_date_max 
and ord_zdlx like @ord_zdlx_min 
and ord_zdlx like @ord_zdlx_max 
and ord_zdmc like @ord_zdmc_min 
and ord_zdmc like @ord_zdmc_max 
and ord_zdrq>=@ord_zdrq_min 
and ord_zdrq<=@ord_zdrq_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_lxdh like @ord_lxdh_min 
and ord_lxdh like @ord_lxdh_max 
and ord_no like @ord_no_min 
and ord_no like @ord_no_max 
order by ord_date desc,ord_zdrxm,ord_kyrq desc,ord_jdr,ord_dpbh,ord_createdepart,ord_zdlx,ord_zdmc,ord_zdrq desc,ord_status,ord_lxdh,ord_no
select count(1) resultcount  from datalinker where useruid=@useruid 
end else
begin
select count(1) cnt from order_140 a,order_140_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_zdrxm like @ord_zdrxm_min 
and ord_zdrxm like @ord_zdrxm_max 
and ord_kyrq>=@ord_kyrq_min 
and ord_kyrq<=@ord_kyrq_max 
and ord_jdr like @ord_jdr_min 
and ord_jdr like @ord_jdr_max 
and ord_dpbh like @ord_dpbh_min 
and ord_dpbh like @ord_dpbh_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_date>=@ord_date_min 
and ord_date<=@ord_date_max 
and ord_zdlx like @ord_zdlx_min 
and ord_zdlx like @ord_zdlx_max 
and ord_zdmc like @ord_zdmc_min 
and ord_zdmc like @ord_zdmc_max 
and ord_zdrq>=@ord_zdrq_min 
and ord_zdrq<=@ord_zdrq_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_lxdh like @ord_lxdh_min 
and ord_lxdh like @ord_lxdh_max 
and ord_no like @ord_no_min 
and ord_no like @ord_no_max 

-- 返回所有ID
insert into @pobjs(ordid) select top(@amount) ord_id from order_140 a,order_140_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_zdrxm like @ord_zdrxm_min 
and ord_zdrxm like @ord_zdrxm_max 
and ord_kyrq>=@ord_kyrq_min 
and ord_kyrq<=@ord_kyrq_max 
and ord_jdr like @ord_jdr_min 
and ord_jdr like @ord_jdr_max 
and ord_dpbh like @ord_dpbh_min 
and ord_dpbh like @ord_dpbh_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_date>=@ord_date_min 
and ord_date<=@ord_date_max 
and ord_zdlx like @ord_zdlx_min 
and ord_zdlx like @ord_zdlx_max 
and ord_zdmc like @ord_zdmc_min 
and ord_zdmc like @ord_zdmc_max 
and ord_zdrq>=@ord_zdrq_min 
and ord_zdrq<=@ord_zdrq_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_lxdh like @ord_lxdh_min 
and ord_lxdh like @ord_lxdh_max 
and ord_no like @ord_no_min 
and ord_no like @ord_no_max 
order by ord_date desc,ord_zdrxm,ord_kyrq desc,ord_jdr,ord_dpbh,ord_createdepart,ord_zdlx,ord_zdmc,ord_zdrq desc,ord_status,ord_lxdh,ord_no
select ordid from @pobjs 
order by nid 
-- 返回第一页 
select top(@pagesize) a.* from order_140 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_140_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_140 a,order_140_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_zdrxm like @ord_zdrxm_min 
and ord_zdrxm like @ord_zdrxm_max 
and ord_kyrq>=@ord_kyrq_min 
and ord_kyrq<=@ord_kyrq_max 
and ord_jdr like @ord_jdr_min 
and ord_jdr like @ord_jdr_max 
and ord_dpbh like @ord_dpbh_min 
and ord_dpbh like @ord_dpbh_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_date>=@ord_date_min 
and ord_date<=@ord_date_max 
and ord_zdlx like @ord_zdlx_min 
and ord_zdlx like @ord_zdlx_max 
and ord_zdmc like @ord_zdmc_min 
and ord_zdmc like @ord_zdmc_max 
and ord_zdrq>=@ord_zdrq_min 
and ord_zdrq<=@ord_zdrq_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_lxdh like @ord_lxdh_min 
and ord_lxdh like @ord_lxdh_max 
and ord_no like @ord_no_min 
and ord_no like @ord_no_max 
order by ord_date desc,ord_zdrxm,ord_kyrq desc,ord_jdr,ord_dpbh,ord_createdepart,ord_zdlx,ord_zdmc,ord_zdrq desc,ord_status,ord_lxdh,ord_no
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_140 a,order_140_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_zdrxm like @ord_zdrxm_min 
and ord_zdrxm like @ord_zdrxm_max 
and ord_kyrq>=@ord_kyrq_min 
and ord_kyrq<=@ord_kyrq_max 
and ord_jdr like @ord_jdr_min 
and ord_jdr like @ord_jdr_max 
and ord_dpbh like @ord_dpbh_min 
and ord_dpbh like @ord_dpbh_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_date>=@ord_date_min 
and ord_date<=@ord_date_max 
and ord_zdlx like @ord_zdlx_min 
and ord_zdlx like @ord_zdlx_max 
and ord_zdmc like @ord_zdmc_min 
and ord_zdmc like @ord_zdmc_max 
and ord_zdrq>=@ord_zdrq_min 
and ord_zdrq<=@ord_zdrq_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_lxdh like @ord_lxdh_min 
and ord_lxdh like @ord_lxdh_max 
and ord_no like @ord_no_min 
and ord_no like @ord_no_max 
order by ord_date desc,ord_zdrxm,ord_kyrq desc,ord_jdr,ord_dpbh,ord_createdepart,ord_zdlx,ord_zdmc,ord_zdrq desc,ord_status,ord_lxdh,ord_no
select count(1) resultcount  from datalinker where useruid=@useruid 
end else
begin
select count(1) cnt from order_140 a,order_140_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_zdrxm like @ord_zdrxm_min 
and ord_zdrxm like @ord_zdrxm_max 
and ord_kyrq>=@ord_kyrq_min 
and ord_kyrq<=@ord_kyrq_max 
and ord_jdr like @ord_jdr_min 
and ord_jdr like @ord_jdr_max 
and ord_dpbh like @ord_dpbh_min 
and ord_dpbh like @ord_dpbh_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_date>=@ord_date_min 
and ord_date<=@ord_date_max 
and ord_zdlx like @ord_zdlx_min 
and ord_zdlx like @ord_zdlx_max 
and ord_zdmc like @ord_zdmc_min 
and ord_zdmc like @ord_zdmc_max 
and ord_zdrq>=@ord_zdrq_min 
and ord_zdrq<=@ord_zdrq_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_lxdh like @ord_lxdh_min 
and ord_lxdh like @ord_lxdh_max 
and ord_no like @ord_no_min 
and ord_no like @ord_no_max 

-- 返回所有ID
insert into @pobjs(ordid) select top(@amount) ord_id from order_140 a,order_140_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_zdrxm like @ord_zdrxm_min 
and ord_zdrxm like @ord_zdrxm_max 
and ord_kyrq>=@ord_kyrq_min 
and ord_kyrq<=@ord_kyrq_max 
and ord_jdr like @ord_jdr_min 
and ord_jdr like @ord_jdr_max 
and ord_dpbh like @ord_dpbh_min 
and ord_dpbh like @ord_dpbh_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_date>=@ord_date_min 
and ord_date<=@ord_date_max 
and ord_zdlx like @ord_zdlx_min 
and ord_zdlx like @ord_zdlx_max 
and ord_zdmc like @ord_zdmc_min 
and ord_zdmc like @ord_zdmc_max 
and ord_zdrq>=@ord_zdrq_min 
and ord_zdrq<=@ord_zdrq_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_lxdh like @ord_lxdh_min 
and ord_lxdh like @ord_lxdh_max 
and ord_no like @ord_no_min 
and ord_no like @ord_no_max 
order by ord_date desc,ord_zdrxm,ord_kyrq desc,ord_jdr,ord_dpbh,ord_createdepart,ord_zdlx,ord_zdmc,ord_zdrq desc,ord_status,ord_lxdh,ord_no
select ordid from @pobjs 
order by nid 
-- 返回第一页 
select top(@pagesize) a.* from order_140 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_140_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_140 a,order_140_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_zdrxm like @ord_zdrxm_min 
and ord_zdrxm like @ord_zdrxm_max 
and ord_kyrq>=@ord_kyrq_min 
and ord_kyrq<=@ord_kyrq_max 
and ord_jdr like @ord_jdr_min 
and ord_jdr like @ord_jdr_max 
and ord_dpbh like @ord_dpbh_min 
and ord_dpbh like @ord_dpbh_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_date>=@ord_date_min 
and ord_date<=@ord_date_max 
and ord_zdlx like @ord_zdlx_min 
and ord_zdlx like @ord_zdlx_max 
and ord_zdmc like @ord_zdmc_min 
and ord_zdmc like @ord_zdmc_max 
and ord_zdrq>=@ord_zdrq_min 
and ord_zdrq<=@ord_zdrq_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_lxdh like @ord_lxdh_min 
and ord_lxdh like @ord_lxdh_max 
and ord_no like @ord_no_min 
and ord_no like @ord_no_max 
order by ord_date desc,ord_zdrxm,ord_kyrq desc,ord_jdr,ord_dpbh,ord_createdepart,ord_zdlx,ord_zdmc,ord_zdrq desc,ord_status,ord_lxdh,ord_no
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_140 a,order_140_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_zdrxm like @ord_zdrxm_min 
and ord_zdrxm like @ord_zdrxm_max 
and ord_kyrq>=@ord_kyrq_min 
and ord_kyrq<=@ord_kyrq_max 
and ord_jdr like @ord_jdr_min 
and ord_jdr like @ord_jdr_max 
and ord_dpbh like @ord_dpbh_min 
and ord_dpbh like @ord_dpbh_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_date>=@ord_date_min 
and ord_date<=@ord_date_max 
and ord_zdlx like @ord_zdlx_min 
and ord_zdlx like @ord_zdlx_max 
and ord_zdmc like @ord_zdmc_min 
and ord_zdmc like @ord_zdmc_max 
and ord_zdrq>=@ord_zdrq_min 
and ord_zdrq<=@ord_zdrq_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_lxdh like @ord_lxdh_min 
and ord_lxdh like @ord_lxdh_max 
and ord_no like @ord_no_min 
and ord_no like @ord_no_max 
order by ord_date desc,ord_zdrxm,ord_kyrq desc,ord_jdr,ord_dpbh,ord_createdepart,ord_zdlx,ord_zdmc,ord_zdrq desc,ord_status,ord_lxdh,ord_no
select count(1) resultcount  from datalinker where useruid=@useruid 
end else
begin
select count(1) cnt from order_140 a,order_140_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_zdrxm like @ord_zdrxm_min 
and ord_zdrxm like @ord_zdrxm_max 
and ord_kyrq>=@ord_kyrq_min 
and ord_kyrq<=@ord_kyrq_max 
and ord_jdr like @ord_jdr_min 
and ord_jdr like @ord_jdr_max 
and ord_dpbh like @ord_dpbh_min 
and ord_dpbh like @ord_dpbh_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_date>=@ord_date_min 
and ord_date<=@ord_date_max 
and ord_zdlx like @ord_zdlx_min 
and ord_zdlx like @ord_zdlx_max 
and ord_zdmc like @ord_zdmc_min 
and ord_zdmc like @ord_zdmc_max 
and ord_zdrq>=@ord_zdrq_min 
and ord_zdrq<=@ord_zdrq_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_lxdh like @ord_lxdh_min 
and ord_lxdh like @ord_lxdh_max 
and ord_no like @ord_no_min 
and ord_no like @ord_no_max 

-- 返回所有ID
insert into @pobjs(ordid) select top(@amount) ord_id from order_140 a,order_140_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_zdrxm like @ord_zdrxm_min 
and ord_zdrxm like @ord_zdrxm_max 
and ord_kyrq>=@ord_kyrq_min 
and ord_kyrq<=@ord_kyrq_max 
and ord_jdr like @ord_jdr_min 
and ord_jdr like @ord_jdr_max 
and ord_dpbh like @ord_dpbh_min 
and ord_dpbh like @ord_dpbh_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_date>=@ord_date_min 
and ord_date<=@ord_date_max 
and ord_zdlx like @ord_zdlx_min 
and ord_zdlx like @ord_zdlx_max 
and ord_zdmc like @ord_zdmc_min 
and ord_zdmc like @ord_zdmc_max 
and ord_zdrq>=@ord_zdrq_min 
and ord_zdrq<=@ord_zdrq_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_lxdh like @ord_lxdh_min 
and ord_lxdh like @ord_lxdh_max 
and ord_no like @ord_no_min 
and ord_no like @ord_no_max 
order by ord_date desc,ord_zdrxm,ord_kyrq desc,ord_jdr,ord_dpbh,ord_createdepart,ord_zdlx,ord_zdmc,ord_zdrq desc,ord_status,ord_lxdh,ord_no
select ordid from @pobjs 
order by nid 
-- 返回第一页 
select top(@pagesize) a.* from order_140 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_140_users a , @pobjs 
where ordp_ordid=ordid 
and nid<=@pagesize 
and ordp_cuid=@useruid 
end 
end 
set nocount off end