IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[order_173_query]') AND type = 'P')
DROP PROCEDURE [dbo].[order_173_query]
GO
CREATE  procedure order_173_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_branch_min nvarchar(50), @ord_branch_max nvarchar(50),@ord_yfje_min int,@ord_yfje_max int,@ord_creator_min nvarchar(50), @ord_creator_max nvarchar(50),@ord_date_min datetime,@ord_date_max datetime,@ord_lxdh_min varchar(100), @ord_lxdh_max varchar(100),@ord_htyf_min varchar(100), @ord_htyf_max varchar(100),@ord_htmc_min varchar(100), @ord_htmc_max varchar(100),@ord_htje_min int,@ord_htje_max int) 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_173 a, @pobjs
where ord_id=ordid
order by nid 
select a.* from order_173_users a , @pobjs 
where ordp_ordid=ordid and ordp_cuid=@useruid
return 
end 
if (@status is null or @status = '_none_') set @status = '' 


if (@ord_branch_min is null or @ord_branch_min = '_none_') set @ord_branch_min = '' 
if (@ord_branch_max is null or @ord_branch_max = '_none_') set @ord_branch_max = '' 

if (@ord_yfje_min is null or @ord_yfje_min = -999999999) set @ord_yfje_min = -999999999 
if (@ord_yfje_max is null or @ord_yfje_max = -999999999) set @ord_yfje_max = 999999999

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


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

if (@ord_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_htyf_min is null or @ord_htyf_min = '_none_') set @ord_htyf_min = '' 
if (@ord_htyf_max is null or @ord_htyf_max = '_none_') set @ord_htyf_max = '' 

if (@ord_htmc_min is null or @ord_htmc_min = '_none_') set @ord_htmc_min = '' 
if (@ord_htmc_max is null or @ord_htmc_max = '_none_') set @ord_htmc_max = '' 

if (@ord_htje_min is null or @ord_htje_min = -999999999) set @ord_htje_min = -999999999 
if (@ord_htje_max is null or @ord_htje_max = -999999999) set @ord_htje_max = 999999999

if @ord_branch_min>'' 
begin 
if (charindex('%',@ord_branch_min) = 0 and @ord_branch_min!='_empty_') 
 begin
set @ord_branch_min = '%'+@ord_branch_min+'%';
 end else if (@ord_branch_min='_empty_') set @ord_branch_min='' 
end else
begin 
set @ord_branch_min = '%' 
end
if @ord_branch_max>'' 
begin 
if (charindex('%', @ord_branch_max) = 0 and @ord_branch_max!='_empty_') 
begin
set@ord_branch_max = '%'+@ord_branch_max+'%';
 end else if (@ord_branch_max='_empty_') set @ord_branch_max='' 
end else
begin 
set @ord_branch_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_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_htyf_min>'' 
begin 
if (charindex('%',@ord_htyf_min) = 0 and @ord_htyf_min!='_empty_') 
 begin
set @ord_htyf_min = '%'+@ord_htyf_min+'%';
 end else if (@ord_htyf_min='_empty_') set @ord_htyf_min='' 
end else
begin 
set @ord_htyf_min = '%' 
end
if @ord_htyf_max>'' 
begin 
if (charindex('%', @ord_htyf_max) = 0 and @ord_htyf_max!='_empty_') 
begin
set@ord_htyf_max = '%'+@ord_htyf_max+'%';
 end else if (@ord_htyf_max='_empty_') set @ord_htyf_max='' 
end else
begin 
set @ord_htyf_max = '%' 
end

if @ord_htmc_min>'' 
begin 
if (charindex('%',@ord_htmc_min) = 0 and @ord_htmc_min!='_empty_') 
 begin
set @ord_htmc_min = '%'+@ord_htmc_min+'%';
 end else if (@ord_htmc_min='_empty_') set @ord_htmc_min='' 
end else
begin 
set @ord_htmc_min = '%' 
end
if @ord_htmc_max>'' 
begin 
if (charindex('%', @ord_htmc_max) = 0 and @ord_htmc_max!='_empty_') 
begin
set@ord_htmc_max = '%'+@ord_htmc_max+'%';
 end else if (@ord_htmc_max='_empty_') set @ord_htmc_max='' 
end else
begin 
set @ord_htmc_max = '%' 
end
 
    
if (@finished = -11)
begin
if (@pagesize=10000)  -- 下载
begin
select a.* from order_173 a,order_173_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_branch like @ord_branch_min 
and ord_branch like @ord_branch_max 
and ord_yfje>=@ord_yfje_min 
and ord_yfje<=@ord_yfje_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
and ord_date>=@ord_date_min 
and ord_date<=@ord_date_max 
and ord_lxdh like @ord_lxdh_min 
and ord_lxdh like @ord_lxdh_max 
and ord_htyf like @ord_htyf_min 
and ord_htyf like @ord_htyf_max 
and ord_htmc like @ord_htmc_min 
and ord_htmc like @ord_htmc_max 
and ord_htje>=@ord_htje_min 
and ord_htje<=@ord_htje_max 
order by ord_date desc,ord_branch,ord_yfje,ord_creator,ord_lxdh,ord_htyf,ord_htmc,ord_htje
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_173 a,order_173_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_branch like @ord_branch_min 
and ord_branch like @ord_branch_max 
and ord_yfje>=@ord_yfje_min 
and ord_yfje<=@ord_yfje_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
and ord_date>=@ord_date_min 
and ord_date<=@ord_date_max 
and ord_lxdh like @ord_lxdh_min 
and ord_lxdh like @ord_lxdh_max 
and ord_htyf like @ord_htyf_min 
and ord_htyf like @ord_htyf_max 
and ord_htmc like @ord_htmc_min 
and ord_htmc like @ord_htmc_max 
and ord_htje>=@ord_htje_min 
and ord_htje<=@ord_htje_max 
order by ord_date desc,ord_branch,ord_yfje,ord_creator,ord_lxdh,ord_htyf,ord_htmc,ord_htje
select count(1) resultcount  from datalinker where useruid=@useruid 
end else
begin
select count(1) cnt from order_173 a,order_173_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_branch like @ord_branch_min 
and ord_branch like @ord_branch_max 
and ord_yfje>=@ord_yfje_min 
and ord_yfje<=@ord_yfje_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
and ord_date>=@ord_date_min 
and ord_date<=@ord_date_max 
and ord_lxdh like @ord_lxdh_min 
and ord_lxdh like @ord_lxdh_max 
and ord_htyf like @ord_htyf_min 
and ord_htyf like @ord_htyf_max 
and ord_htmc like @ord_htmc_min 
and ord_htmc like @ord_htmc_max 
and ord_htje>=@ord_htje_min 
and ord_htje<=@ord_htje_max 

-- 返回所有ID
insert into @pobjs(ordid) select top(@amount) ord_id from order_173 a,order_173_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_branch like @ord_branch_min 
and ord_branch like @ord_branch_max 
and ord_yfje>=@ord_yfje_min 
and ord_yfje<=@ord_yfje_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
and ord_date>=@ord_date_min 
and ord_date<=@ord_date_max 
and ord_lxdh like @ord_lxdh_min 
and ord_lxdh like @ord_lxdh_max 
and ord_htyf like @ord_htyf_min 
and ord_htyf like @ord_htyf_max 
and ord_htmc like @ord_htmc_min 
and ord_htmc like @ord_htmc_max 
and ord_htje>=@ord_htje_min 
and ord_htje<=@ord_htje_max 
order by ord_date desc,ord_branch,ord_yfje,ord_creator,ord_lxdh,ord_htyf,ord_htmc,ord_htje
select ordid from @pobjs 
order by nid 
-- 返回第一页 
select top(@pagesize) a.* from order_173 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_173_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_173 a,order_173_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_branch like @ord_branch_min 
and ord_branch like @ord_branch_max 
and ord_yfje>=@ord_yfje_min 
and ord_yfje<=@ord_yfje_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
and ord_date>=@ord_date_min 
and ord_date<=@ord_date_max 
and ord_lxdh like @ord_lxdh_min 
and ord_lxdh like @ord_lxdh_max 
and ord_htyf like @ord_htyf_min 
and ord_htyf like @ord_htyf_max 
and ord_htmc like @ord_htmc_min 
and ord_htmc like @ord_htmc_max 
and ord_htje>=@ord_htje_min 
and ord_htje<=@ord_htje_max 
order by ord_date desc,ord_branch,ord_yfje,ord_creator,ord_lxdh,ord_htyf,ord_htmc,ord_htje
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_173 a,order_173_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_branch like @ord_branch_min 
and ord_branch like @ord_branch_max 
and ord_yfje>=@ord_yfje_min 
and ord_yfje<=@ord_yfje_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
and ord_date>=@ord_date_min 
and ord_date<=@ord_date_max 
and ord_lxdh like @ord_lxdh_min 
and ord_lxdh like @ord_lxdh_max 
and ord_htyf like @ord_htyf_min 
and ord_htyf like @ord_htyf_max 
and ord_htmc like @ord_htmc_min 
and ord_htmc like @ord_htmc_max 
and ord_htje>=@ord_htje_min 
and ord_htje<=@ord_htje_max 
order by ord_date desc,ord_branch,ord_yfje,ord_creator,ord_lxdh,ord_htyf,ord_htmc,ord_htje
select count(1) resultcount  from datalinker where useruid=@useruid 
end else
begin
select count(1) cnt from order_173 a,order_173_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_branch like @ord_branch_min 
and ord_branch like @ord_branch_max 
and ord_yfje>=@ord_yfje_min 
and ord_yfje<=@ord_yfje_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
and ord_date>=@ord_date_min 
and ord_date<=@ord_date_max 
and ord_lxdh like @ord_lxdh_min 
and ord_lxdh like @ord_lxdh_max 
and ord_htyf like @ord_htyf_min 
and ord_htyf like @ord_htyf_max 
and ord_htmc like @ord_htmc_min 
and ord_htmc like @ord_htmc_max 
and ord_htje>=@ord_htje_min 
and ord_htje<=@ord_htje_max 

-- 返回所有ID
insert into @pobjs(ordid) select top(@amount) ord_id from order_173 a,order_173_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_branch like @ord_branch_min 
and ord_branch like @ord_branch_max 
and ord_yfje>=@ord_yfje_min 
and ord_yfje<=@ord_yfje_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
and ord_date>=@ord_date_min 
and ord_date<=@ord_date_max 
and ord_lxdh like @ord_lxdh_min 
and ord_lxdh like @ord_lxdh_max 
and ord_htyf like @ord_htyf_min 
and ord_htyf like @ord_htyf_max 
and ord_htmc like @ord_htmc_min 
and ord_htmc like @ord_htmc_max 
and ord_htje>=@ord_htje_min 
and ord_htje<=@ord_htje_max 
order by ord_date desc,ord_branch,ord_yfje,ord_creator,ord_lxdh,ord_htyf,ord_htmc,ord_htje
select ordid from @pobjs 
order by nid 
-- 返回第一页 
select top(@pagesize) a.* from order_173 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_173_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_173 a,order_173_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_branch like @ord_branch_min 
and ord_branch like @ord_branch_max 
and ord_yfje>=@ord_yfje_min 
and ord_yfje<=@ord_yfje_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
and ord_date>=@ord_date_min 
and ord_date<=@ord_date_max 
and ord_lxdh like @ord_lxdh_min 
and ord_lxdh like @ord_lxdh_max 
and ord_htyf like @ord_htyf_min 
and ord_htyf like @ord_htyf_max 
and ord_htmc like @ord_htmc_min 
and ord_htmc like @ord_htmc_max 
and ord_htje>=@ord_htje_min 
and ord_htje<=@ord_htje_max 
order by ord_date desc,ord_branch,ord_yfje,ord_creator,ord_lxdh,ord_htyf,ord_htmc,ord_htje
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_173 a,order_173_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_branch like @ord_branch_min 
and ord_branch like @ord_branch_max 
and ord_yfje>=@ord_yfje_min 
and ord_yfje<=@ord_yfje_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
and ord_date>=@ord_date_min 
and ord_date<=@ord_date_max 
and ord_lxdh like @ord_lxdh_min 
and ord_lxdh like @ord_lxdh_max 
and ord_htyf like @ord_htyf_min 
and ord_htyf like @ord_htyf_max 
and ord_htmc like @ord_htmc_min 
and ord_htmc like @ord_htmc_max 
and ord_htje>=@ord_htje_min 
and ord_htje<=@ord_htje_max 
order by ord_date desc,ord_branch,ord_yfje,ord_creator,ord_lxdh,ord_htyf,ord_htmc,ord_htje
select count(1) resultcount  from datalinker where useruid=@useruid 
end else
begin
select count(1) cnt from order_173 a,order_173_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_branch like @ord_branch_min 
and ord_branch like @ord_branch_max 
and ord_yfje>=@ord_yfje_min 
and ord_yfje<=@ord_yfje_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
and ord_date>=@ord_date_min 
and ord_date<=@ord_date_max 
and ord_lxdh like @ord_lxdh_min 
and ord_lxdh like @ord_lxdh_max 
and ord_htyf like @ord_htyf_min 
and ord_htyf like @ord_htyf_max 
and ord_htmc like @ord_htmc_min 
and ord_htmc like @ord_htmc_max 
and ord_htje>=@ord_htje_min 
and ord_htje<=@ord_htje_max 

-- 返回所有ID
insert into @pobjs(ordid) select top(@amount) ord_id from order_173 a,order_173_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_branch like @ord_branch_min 
and ord_branch like @ord_branch_max 
and ord_yfje>=@ord_yfje_min 
and ord_yfje<=@ord_yfje_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
and ord_date>=@ord_date_min 
and ord_date<=@ord_date_max 
and ord_lxdh like @ord_lxdh_min 
and ord_lxdh like @ord_lxdh_max 
and ord_htyf like @ord_htyf_min 
and ord_htyf like @ord_htyf_max 
and ord_htmc like @ord_htmc_min 
and ord_htmc like @ord_htmc_max 
and ord_htje>=@ord_htje_min 
and ord_htje<=@ord_htje_max 
order by ord_date desc,ord_branch,ord_yfje,ord_creator,ord_lxdh,ord_htyf,ord_htmc,ord_htje
select ordid from @pobjs 
order by nid 
-- 返回第一页 
select top(@pagesize) a.* from order_173 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_173_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_173 a,order_173_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_branch like @ord_branch_min 
and ord_branch like @ord_branch_max 
and ord_yfje>=@ord_yfje_min 
and ord_yfje<=@ord_yfje_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
and ord_date>=@ord_date_min 
and ord_date<=@ord_date_max 
and ord_lxdh like @ord_lxdh_min 
and ord_lxdh like @ord_lxdh_max 
and ord_htyf like @ord_htyf_min 
and ord_htyf like @ord_htyf_max 
and ord_htmc like @ord_htmc_min 
and ord_htmc like @ord_htmc_max 
and ord_htje>=@ord_htje_min 
and ord_htje<=@ord_htje_max 
order by ord_date desc,ord_branch,ord_yfje,ord_creator,ord_lxdh,ord_htyf,ord_htmc,ord_htje
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_173 a,order_173_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_branch like @ord_branch_min 
and ord_branch like @ord_branch_max 
and ord_yfje>=@ord_yfje_min 
and ord_yfje<=@ord_yfje_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
and ord_date>=@ord_date_min 
and ord_date<=@ord_date_max 
and ord_lxdh like @ord_lxdh_min 
and ord_lxdh like @ord_lxdh_max 
and ord_htyf like @ord_htyf_min 
and ord_htyf like @ord_htyf_max 
and ord_htmc like @ord_htmc_min 
and ord_htmc like @ord_htmc_max 
and ord_htje>=@ord_htje_min 
and ord_htje<=@ord_htje_max 
order by ord_date desc,ord_branch,ord_yfje,ord_creator,ord_lxdh,ord_htyf,ord_htmc,ord_htje
select count(1) resultcount  from datalinker where useruid=@useruid 
end else
begin
select count(1) cnt from order_173 a,order_173_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_branch like @ord_branch_min 
and ord_branch like @ord_branch_max 
and ord_yfje>=@ord_yfje_min 
and ord_yfje<=@ord_yfje_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
and ord_date>=@ord_date_min 
and ord_date<=@ord_date_max 
and ord_lxdh like @ord_lxdh_min 
and ord_lxdh like @ord_lxdh_max 
and ord_htyf like @ord_htyf_min 
and ord_htyf like @ord_htyf_max 
and ord_htmc like @ord_htmc_min 
and ord_htmc like @ord_htmc_max 
and ord_htje>=@ord_htje_min 
and ord_htje<=@ord_htje_max 

-- 返回所有ID
insert into @pobjs(ordid) select top(@amount) ord_id from order_173 a,order_173_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_branch like @ord_branch_min 
and ord_branch like @ord_branch_max 
and ord_yfje>=@ord_yfje_min 
and ord_yfje<=@ord_yfje_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
and ord_date>=@ord_date_min 
and ord_date<=@ord_date_max 
and ord_lxdh like @ord_lxdh_min 
and ord_lxdh like @ord_lxdh_max 
and ord_htyf like @ord_htyf_min 
and ord_htyf like @ord_htyf_max 
and ord_htmc like @ord_htmc_min 
and ord_htmc like @ord_htmc_max 
and ord_htje>=@ord_htje_min 
and ord_htje<=@ord_htje_max 
order by ord_date desc,ord_branch,ord_yfje,ord_creator,ord_lxdh,ord_htyf,ord_htmc,ord_htje
select ordid from @pobjs 
order by nid 
-- 返回第一页 
select top(@pagesize) a.* from order_173 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_173_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_173 a,order_173_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_branch like @ord_branch_min 
and ord_branch like @ord_branch_max 
and ord_yfje>=@ord_yfje_min 
and ord_yfje<=@ord_yfje_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
and ord_date>=@ord_date_min 
and ord_date<=@ord_date_max 
and ord_lxdh like @ord_lxdh_min 
and ord_lxdh like @ord_lxdh_max 
and ord_htyf like @ord_htyf_min 
and ord_htyf like @ord_htyf_max 
and ord_htmc like @ord_htmc_min 
and ord_htmc like @ord_htmc_max 
and ord_htje>=@ord_htje_min 
and ord_htje<=@ord_htje_max 
order by ord_date desc,ord_branch,ord_yfje,ord_creator,ord_lxdh,ord_htyf,ord_htmc,ord_htje
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_173 a,order_173_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_branch like @ord_branch_min 
and ord_branch like @ord_branch_max 
and ord_yfje>=@ord_yfje_min 
and ord_yfje<=@ord_yfje_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
and ord_date>=@ord_date_min 
and ord_date<=@ord_date_max 
and ord_lxdh like @ord_lxdh_min 
and ord_lxdh like @ord_lxdh_max 
and ord_htyf like @ord_htyf_min 
and ord_htyf like @ord_htyf_max 
and ord_htmc like @ord_htmc_min 
and ord_htmc like @ord_htmc_max 
and ord_htje>=@ord_htje_min 
and ord_htje<=@ord_htje_max 
order by ord_date desc,ord_branch,ord_yfje,ord_creator,ord_lxdh,ord_htyf,ord_htmc,ord_htje
select count(1) resultcount  from datalinker where useruid=@useruid 
end else
begin
select count(1) cnt from order_173 a,order_173_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_branch like @ord_branch_min 
and ord_branch like @ord_branch_max 
and ord_yfje>=@ord_yfje_min 
and ord_yfje<=@ord_yfje_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
and ord_date>=@ord_date_min 
and ord_date<=@ord_date_max 
and ord_lxdh like @ord_lxdh_min 
and ord_lxdh like @ord_lxdh_max 
and ord_htyf like @ord_htyf_min 
and ord_htyf like @ord_htyf_max 
and ord_htmc like @ord_htmc_min 
and ord_htmc like @ord_htmc_max 
and ord_htje>=@ord_htje_min 
and ord_htje<=@ord_htje_max 

-- 返回所有ID
insert into @pobjs(ordid) select top(@amount) ord_id from order_173 a,order_173_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_branch like @ord_branch_min 
and ord_branch like @ord_branch_max 
and ord_yfje>=@ord_yfje_min 
and ord_yfje<=@ord_yfje_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
and ord_date>=@ord_date_min 
and ord_date<=@ord_date_max 
and ord_lxdh like @ord_lxdh_min 
and ord_lxdh like @ord_lxdh_max 
and ord_htyf like @ord_htyf_min 
and ord_htyf like @ord_htyf_max 
and ord_htmc like @ord_htmc_min 
and ord_htmc like @ord_htmc_max 
and ord_htje>=@ord_htje_min 
and ord_htje<=@ord_htje_max 
order by ord_date desc,ord_branch,ord_yfje,ord_creator,ord_lxdh,ord_htyf,ord_htmc,ord_htje
select ordid from @pobjs 
order by nid 
-- 返回第一页 
select top(@pagesize) a.* from order_173 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_173_users a , @pobjs 
where ordp_ordid=ordid 
and nid<=@pagesize 
and ordp_cuid=@useruid 
end 
end 
set nocount off end