IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[order_78_query]') AND type = 'P')
DROP PROCEDURE [dbo].[order_78_query]
GO
CREATE  procedure order_78_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_wgxz_min varchar(100), @ord_wgxz_max varchar(100),@ord_wgkf_min int,@ord_wgkf_max int,@ord_wglx_min varchar(100), @ord_wglx_max varchar(100),@ord_wgrq_min datetime,@ord_wgrq_max datetime,@ord_szjxs_min varchar(100), @ord_szjxs_max varchar(100),@ord_status_min nvarchar(30), @ord_status_max nvarchar(30),@ord_dpmc_min varchar(100), @ord_dpmc_max varchar(100)) as begin  
set nocount on  

declare @nonedate datetime 
set @nonedate = CONVERT(datetime,'1910-1-1') 
declare @s1 varchar(8000) 
declare @s2 varchar(100) 
declare @i int 
declare @amount int 
select @amount = @pagecount*@pagesize 
declare @pobjs table(nid int identity(1, 1), ordid bigint) 
set @s1 = @pageobjids 
while (@s1>'') 
begin 
set @i = charindex(',', @s1) 
if (@i>0) 
begin 
set @s2 = substring(@s1,1,@i-1) 
set @s1 = substring(@s1,@i+1,len(@s1)-@i) 
end else  
begin 
set @s2 = @s1 
set @s1 = '' 
end 
insert into @pobjs(ordid) values(convert(bigint,@s2)) 
end 
if (exists (select top 1 * from @pobjs)) 
begin 
select a.* from order_78 a, @pobjs
where ord_id=ordid
order by nid 
select a.* from order_78_users a , @pobjs 
where ordp_ordid=ordid and ordp_cuid=@useruid
return 
end 
if (@status is null or @status = '_none_') set @status = '' 


if (@ord_wgxz_min is null or @ord_wgxz_min = '_none_') set @ord_wgxz_min = '' 
if (@ord_wgxz_max is null or @ord_wgxz_max = '_none_') set @ord_wgxz_max = '' 

if (@ord_wgkf_min is null or @ord_wgkf_min = -999999999) set @ord_wgkf_min = -999999999 
if (@ord_wgkf_max is null or @ord_wgkf_max = -999999999) set @ord_wgkf_max = 999999999

if (@ord_wglx_min is null or @ord_wglx_min = '_none_') set @ord_wglx_min = '' 
if (@ord_wglx_max is null or @ord_wglx_max = '_none_') set @ord_wglx_max = '' 


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

if (@ord_szjxs_min is null or @ord_szjxs_min = '_none_') set @ord_szjxs_min = '' 
if (@ord_szjxs_max is null or @ord_szjxs_max = '_none_') set @ord_szjxs_max = '' 

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

if (@ord_dpmc_min is null or @ord_dpmc_min = '_none_') set @ord_dpmc_min = '' 
if (@ord_dpmc_max is null or @ord_dpmc_max = '_none_') set @ord_dpmc_max = '' 

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

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

if @ord_szjxs_min>'' 
begin 
if (charindex('%',@ord_szjxs_min) = 0 and @ord_szjxs_min!='_empty_') 
 begin
set @ord_szjxs_min = '%'+@ord_szjxs_min+'%';
 end else if (@ord_szjxs_min='_empty_') set @ord_szjxs_min='' 
end else
begin 
set @ord_szjxs_min = '%' 
end
if @ord_szjxs_max>'' 
begin 
if (charindex('%', @ord_szjxs_max) = 0 and @ord_szjxs_max!='_empty_') 
begin
set@ord_szjxs_max = '%'+@ord_szjxs_max+'%';
 end else if (@ord_szjxs_max='_empty_') set @ord_szjxs_max='' 
end else
begin 
set @ord_szjxs_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_dpmc_min>'' 
begin 
if (charindex('%',@ord_dpmc_min) = 0 and @ord_dpmc_min!='_empty_') 
 begin
set @ord_dpmc_min = '%'+@ord_dpmc_min+'%';
 end else if (@ord_dpmc_min='_empty_') set @ord_dpmc_min='' 
end else
begin 
set @ord_dpmc_min = '%' 
end
if @ord_dpmc_max>'' 
begin 
if (charindex('%', @ord_dpmc_max) = 0 and @ord_dpmc_max!='_empty_') 
begin
set@ord_dpmc_max = '%'+@ord_dpmc_max+'%';
 end else if (@ord_dpmc_max='_empty_') set @ord_dpmc_max='' 
end else
begin 
set @ord_dpmc_max = '%' 
end
 
    
if (@finished = -11)
begin
if (@pagesize=10000)  -- 下载
begin
select a.* from order_78 a,order_78_users 
where a.ord_cmpid=@cmpid 
and ordp_type='bizview' 
and ordp_cuid=@useruid 
and (a.ord_status=@status or @status='') 
and a.ord_finished > 0 
and ord_wgxz like @ord_wgxz_min 
and ord_wgxz like @ord_wgxz_max 
and ord_wgkf>=@ord_wgkf_min 
and ord_wgkf<=@ord_wgkf_max 
and ord_wglx like @ord_wglx_min 
and ord_wglx like @ord_wglx_max 
and ord_wgrq>=@ord_wgrq_min 
and ord_wgrq<=@ord_wgrq_max 
and ord_szjxs like @ord_szjxs_min 
and ord_szjxs like @ord_szjxs_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_dpmc like @ord_dpmc_min 
and ord_dpmc like @ord_dpmc_max 
order by ord_date desc,ord_wgxz,ord_wgkf,ord_wglx,ord_wgrq desc,ord_szjxs,ord_status,ord_dpmc
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_78 a,order_78_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_wgxz like @ord_wgxz_min 
and ord_wgxz like @ord_wgxz_max 
and ord_wgkf>=@ord_wgkf_min 
and ord_wgkf<=@ord_wgkf_max 
and ord_wglx like @ord_wglx_min 
and ord_wglx like @ord_wglx_max 
and ord_wgrq>=@ord_wgrq_min 
and ord_wgrq<=@ord_wgrq_max 
and ord_szjxs like @ord_szjxs_min 
and ord_szjxs like @ord_szjxs_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_dpmc like @ord_dpmc_min 
and ord_dpmc like @ord_dpmc_max 
order by ord_date desc,ord_wgxz,ord_wgkf,ord_wglx,ord_wgrq desc,ord_szjxs,ord_status,ord_dpmc
select count(1) resultcount  from datalinker where useruid=@useruid 
end else
begin
select count(1) cnt from order_78 a,order_78_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_wgxz like @ord_wgxz_min 
and ord_wgxz like @ord_wgxz_max 
and ord_wgkf>=@ord_wgkf_min 
and ord_wgkf<=@ord_wgkf_max 
and ord_wglx like @ord_wglx_min 
and ord_wglx like @ord_wglx_max 
and ord_wgrq>=@ord_wgrq_min 
and ord_wgrq<=@ord_wgrq_max 
and ord_szjxs like @ord_szjxs_min 
and ord_szjxs like @ord_szjxs_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_dpmc like @ord_dpmc_min 
and ord_dpmc like @ord_dpmc_max 

-- 返回所有ID
insert into @pobjs(ordid) select top(@amount) ord_id from order_78 a,order_78_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_wgxz like @ord_wgxz_min 
and ord_wgxz like @ord_wgxz_max 
and ord_wgkf>=@ord_wgkf_min 
and ord_wgkf<=@ord_wgkf_max 
and ord_wglx like @ord_wglx_min 
and ord_wglx like @ord_wglx_max 
and ord_wgrq>=@ord_wgrq_min 
and ord_wgrq<=@ord_wgrq_max 
and ord_szjxs like @ord_szjxs_min 
and ord_szjxs like @ord_szjxs_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_dpmc like @ord_dpmc_min 
and ord_dpmc like @ord_dpmc_max 
order by ord_date desc,ord_wgxz,ord_wgkf,ord_wglx,ord_wgrq desc,ord_szjxs,ord_status,ord_dpmc
select ordid from @pobjs 
order by nid 
-- 返回第一页 
select top(@pagesize) a.* from order_78 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_78_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_78 a,order_78_users 
where a.ord_cmpid=@cmpid 
and ordp_type='bizview' 
and ordp_cuid=@useruid 
and (a.ord_status=@status or @status='') 
and (a.ord_finished>0 and a.ord_createcuid=@useruid) 
and ord_wgxz like @ord_wgxz_min 
and ord_wgxz like @ord_wgxz_max 
and ord_wgkf>=@ord_wgkf_min 
and ord_wgkf<=@ord_wgkf_max 
and ord_wglx like @ord_wglx_min 
and ord_wglx like @ord_wglx_max 
and ord_wgrq>=@ord_wgrq_min 
and ord_wgrq<=@ord_wgrq_max 
and ord_szjxs like @ord_szjxs_min 
and ord_szjxs like @ord_szjxs_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_dpmc like @ord_dpmc_min 
and ord_dpmc like @ord_dpmc_max 
order by ord_date desc,ord_wgxz,ord_wgkf,ord_wglx,ord_wgrq desc,ord_szjxs,ord_status,ord_dpmc
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_78 a,order_78_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_wgxz like @ord_wgxz_min 
and ord_wgxz like @ord_wgxz_max 
and ord_wgkf>=@ord_wgkf_min 
and ord_wgkf<=@ord_wgkf_max 
and ord_wglx like @ord_wglx_min 
and ord_wglx like @ord_wglx_max 
and ord_wgrq>=@ord_wgrq_min 
and ord_wgrq<=@ord_wgrq_max 
and ord_szjxs like @ord_szjxs_min 
and ord_szjxs like @ord_szjxs_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_dpmc like @ord_dpmc_min 
and ord_dpmc like @ord_dpmc_max 
order by ord_date desc,ord_wgxz,ord_wgkf,ord_wglx,ord_wgrq desc,ord_szjxs,ord_status,ord_dpmc
select count(1) resultcount  from datalinker where useruid=@useruid 
end else
begin
select count(1) cnt from order_78 a,order_78_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_wgxz like @ord_wgxz_min 
and ord_wgxz like @ord_wgxz_max 
and ord_wgkf>=@ord_wgkf_min 
and ord_wgkf<=@ord_wgkf_max 
and ord_wglx like @ord_wglx_min 
and ord_wglx like @ord_wglx_max 
and ord_wgrq>=@ord_wgrq_min 
and ord_wgrq<=@ord_wgrq_max 
and ord_szjxs like @ord_szjxs_min 
and ord_szjxs like @ord_szjxs_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_dpmc like @ord_dpmc_min 
and ord_dpmc like @ord_dpmc_max 

-- 返回所有ID
insert into @pobjs(ordid) select top(@amount) ord_id from order_78 a,order_78_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_wgxz like @ord_wgxz_min 
and ord_wgxz like @ord_wgxz_max 
and ord_wgkf>=@ord_wgkf_min 
and ord_wgkf<=@ord_wgkf_max 
and ord_wglx like @ord_wglx_min 
and ord_wglx like @ord_wglx_max 
and ord_wgrq>=@ord_wgrq_min 
and ord_wgrq<=@ord_wgrq_max 
and ord_szjxs like @ord_szjxs_min 
and ord_szjxs like @ord_szjxs_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_dpmc like @ord_dpmc_min 
and ord_dpmc like @ord_dpmc_max 
order by ord_date desc,ord_wgxz,ord_wgkf,ord_wglx,ord_wgrq desc,ord_szjxs,ord_status,ord_dpmc
select ordid from @pobjs 
order by nid 
-- 返回第一页 
select top(@pagesize) a.* from order_78 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_78_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_78 a,order_78_users 
where a.ord_cmpid=@cmpid 
and ordp_type='bizview' 
and ordp_cuid=@useruid 
and (a.ord_status=@status or @status='') 
and a.ord_finished>=2 
and ord_wgxz like @ord_wgxz_min 
and ord_wgxz like @ord_wgxz_max 
and ord_wgkf>=@ord_wgkf_min 
and ord_wgkf<=@ord_wgkf_max 
and ord_wglx like @ord_wglx_min 
and ord_wglx like @ord_wglx_max 
and ord_wgrq>=@ord_wgrq_min 
and ord_wgrq<=@ord_wgrq_max 
and ord_szjxs like @ord_szjxs_min 
and ord_szjxs like @ord_szjxs_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_dpmc like @ord_dpmc_min 
and ord_dpmc like @ord_dpmc_max 
order by ord_date desc,ord_wgxz,ord_wgkf,ord_wglx,ord_wgrq desc,ord_szjxs,ord_status,ord_dpmc
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_78 a,order_78_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_wgxz like @ord_wgxz_min 
and ord_wgxz like @ord_wgxz_max 
and ord_wgkf>=@ord_wgkf_min 
and ord_wgkf<=@ord_wgkf_max 
and ord_wglx like @ord_wglx_min 
and ord_wglx like @ord_wglx_max 
and ord_wgrq>=@ord_wgrq_min 
and ord_wgrq<=@ord_wgrq_max 
and ord_szjxs like @ord_szjxs_min 
and ord_szjxs like @ord_szjxs_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_dpmc like @ord_dpmc_min 
and ord_dpmc like @ord_dpmc_max 
order by ord_date desc,ord_wgxz,ord_wgkf,ord_wglx,ord_wgrq desc,ord_szjxs,ord_status,ord_dpmc
select count(1) resultcount  from datalinker where useruid=@useruid 
end else
begin
select count(1) cnt from order_78 a,order_78_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_wgxz like @ord_wgxz_min 
and ord_wgxz like @ord_wgxz_max 
and ord_wgkf>=@ord_wgkf_min 
and ord_wgkf<=@ord_wgkf_max 
and ord_wglx like @ord_wglx_min 
and ord_wglx like @ord_wglx_max 
and ord_wgrq>=@ord_wgrq_min 
and ord_wgrq<=@ord_wgrq_max 
and ord_szjxs like @ord_szjxs_min 
and ord_szjxs like @ord_szjxs_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_dpmc like @ord_dpmc_min 
and ord_dpmc like @ord_dpmc_max 

-- 返回所有ID
insert into @pobjs(ordid) select top(@amount) ord_id from order_78 a,order_78_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_wgxz like @ord_wgxz_min 
and ord_wgxz like @ord_wgxz_max 
and ord_wgkf>=@ord_wgkf_min 
and ord_wgkf<=@ord_wgkf_max 
and ord_wglx like @ord_wglx_min 
and ord_wglx like @ord_wglx_max 
and ord_wgrq>=@ord_wgrq_min 
and ord_wgrq<=@ord_wgrq_max 
and ord_szjxs like @ord_szjxs_min 
and ord_szjxs like @ord_szjxs_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_dpmc like @ord_dpmc_min 
and ord_dpmc like @ord_dpmc_max 
order by ord_date desc,ord_wgxz,ord_wgkf,ord_wglx,ord_wgrq desc,ord_szjxs,ord_status,ord_dpmc
select ordid from @pobjs 
order by nid 
-- 返回第一页 
select top(@pagesize) a.* from order_78 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_78_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_78 a,order_78_users 
where a.ord_cmpid=@cmpid 
and ordp_type='bizview' 
and ordp_cuid=@useruid 
and (a.ord_status=@status or @status='') 
and (a.ord_finished>0 and a.ord_finished<=1)
and ord_wgxz like @ord_wgxz_min 
and ord_wgxz like @ord_wgxz_max 
and ord_wgkf>=@ord_wgkf_min 
and ord_wgkf<=@ord_wgkf_max 
and ord_wglx like @ord_wglx_min 
and ord_wglx like @ord_wglx_max 
and ord_wgrq>=@ord_wgrq_min 
and ord_wgrq<=@ord_wgrq_max 
and ord_szjxs like @ord_szjxs_min 
and ord_szjxs like @ord_szjxs_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_dpmc like @ord_dpmc_min 
and ord_dpmc like @ord_dpmc_max 
order by ord_date desc,ord_wgxz,ord_wgkf,ord_wglx,ord_wgrq desc,ord_szjxs,ord_status,ord_dpmc
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_78 a,order_78_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_wgxz like @ord_wgxz_min 
and ord_wgxz like @ord_wgxz_max 
and ord_wgkf>=@ord_wgkf_min 
and ord_wgkf<=@ord_wgkf_max 
and ord_wglx like @ord_wglx_min 
and ord_wglx like @ord_wglx_max 
and ord_wgrq>=@ord_wgrq_min 
and ord_wgrq<=@ord_wgrq_max 
and ord_szjxs like @ord_szjxs_min 
and ord_szjxs like @ord_szjxs_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_dpmc like @ord_dpmc_min 
and ord_dpmc like @ord_dpmc_max 
order by ord_date desc,ord_wgxz,ord_wgkf,ord_wglx,ord_wgrq desc,ord_szjxs,ord_status,ord_dpmc
select count(1) resultcount  from datalinker where useruid=@useruid 
end else
begin
select count(1) cnt from order_78 a,order_78_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_wgxz like @ord_wgxz_min 
and ord_wgxz like @ord_wgxz_max 
and ord_wgkf>=@ord_wgkf_min 
and ord_wgkf<=@ord_wgkf_max 
and ord_wglx like @ord_wglx_min 
and ord_wglx like @ord_wglx_max 
and ord_wgrq>=@ord_wgrq_min 
and ord_wgrq<=@ord_wgrq_max 
and ord_szjxs like @ord_szjxs_min 
and ord_szjxs like @ord_szjxs_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_dpmc like @ord_dpmc_min 
and ord_dpmc like @ord_dpmc_max 

-- 返回所有ID
insert into @pobjs(ordid) select top(@amount) ord_id from order_78 a,order_78_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_wgxz like @ord_wgxz_min 
and ord_wgxz like @ord_wgxz_max 
and ord_wgkf>=@ord_wgkf_min 
and ord_wgkf<=@ord_wgkf_max 
and ord_wglx like @ord_wglx_min 
and ord_wglx like @ord_wglx_max 
and ord_wgrq>=@ord_wgrq_min 
and ord_wgrq<=@ord_wgrq_max 
and ord_szjxs like @ord_szjxs_min 
and ord_szjxs like @ord_szjxs_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_dpmc like @ord_dpmc_min 
and ord_dpmc like @ord_dpmc_max 
order by ord_date desc,ord_wgxz,ord_wgkf,ord_wglx,ord_wgrq desc,ord_szjxs,ord_status,ord_dpmc
select ordid from @pobjs 
order by nid 
-- 返回第一页 
select top(@pagesize) a.* from order_78 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_78_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_78 a,order_78_users 
where a.ord_cmpid=@cmpid 
and ordp_type='bizview' 
and ordp_cuid=@useruid 
and (a.ord_status=@status or @status='') 
and ord_finished=-1 
and ord_wgxz like @ord_wgxz_min 
and ord_wgxz like @ord_wgxz_max 
and ord_wgkf>=@ord_wgkf_min 
and ord_wgkf<=@ord_wgkf_max 
and ord_wglx like @ord_wglx_min 
and ord_wglx like @ord_wglx_max 
and ord_wgrq>=@ord_wgrq_min 
and ord_wgrq<=@ord_wgrq_max 
and ord_szjxs like @ord_szjxs_min 
and ord_szjxs like @ord_szjxs_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_dpmc like @ord_dpmc_min 
and ord_dpmc like @ord_dpmc_max 
order by ord_date desc,ord_wgxz,ord_wgkf,ord_wglx,ord_wgrq desc,ord_szjxs,ord_status,ord_dpmc
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_78 a,order_78_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_wgxz like @ord_wgxz_min 
and ord_wgxz like @ord_wgxz_max 
and ord_wgkf>=@ord_wgkf_min 
and ord_wgkf<=@ord_wgkf_max 
and ord_wglx like @ord_wglx_min 
and ord_wglx like @ord_wglx_max 
and ord_wgrq>=@ord_wgrq_min 
and ord_wgrq<=@ord_wgrq_max 
and ord_szjxs like @ord_szjxs_min 
and ord_szjxs like @ord_szjxs_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_dpmc like @ord_dpmc_min 
and ord_dpmc like @ord_dpmc_max 
order by ord_date desc,ord_wgxz,ord_wgkf,ord_wglx,ord_wgrq desc,ord_szjxs,ord_status,ord_dpmc
select count(1) resultcount  from datalinker where useruid=@useruid 
end else
begin
select count(1) cnt from order_78 a,order_78_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_wgxz like @ord_wgxz_min 
and ord_wgxz like @ord_wgxz_max 
and ord_wgkf>=@ord_wgkf_min 
and ord_wgkf<=@ord_wgkf_max 
and ord_wglx like @ord_wglx_min 
and ord_wglx like @ord_wglx_max 
and ord_wgrq>=@ord_wgrq_min 
and ord_wgrq<=@ord_wgrq_max 
and ord_szjxs like @ord_szjxs_min 
and ord_szjxs like @ord_szjxs_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_dpmc like @ord_dpmc_min 
and ord_dpmc like @ord_dpmc_max 

-- 返回所有ID
insert into @pobjs(ordid) select top(@amount) ord_id from order_78 a,order_78_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_wgxz like @ord_wgxz_min 
and ord_wgxz like @ord_wgxz_max 
and ord_wgkf>=@ord_wgkf_min 
and ord_wgkf<=@ord_wgkf_max 
and ord_wglx like @ord_wglx_min 
and ord_wglx like @ord_wglx_max 
and ord_wgrq>=@ord_wgrq_min 
and ord_wgrq<=@ord_wgrq_max 
and ord_szjxs like @ord_szjxs_min 
and ord_szjxs like @ord_szjxs_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_dpmc like @ord_dpmc_min 
and ord_dpmc like @ord_dpmc_max 
order by ord_date desc,ord_wgxz,ord_wgkf,ord_wglx,ord_wgrq desc,ord_szjxs,ord_status,ord_dpmc
select ordid from @pobjs 
order by nid 
-- 返回第一页 
select top(@pagesize) a.* from order_78 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_78_users a , @pobjs 
where ordp_ordid=ordid 
and nid<=@pagesize 
and ordp_cuid=@useruid 
end 
end 
set nocount off end