IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[order_93_query]') AND type = 'P')
DROP PROCEDURE [dbo].[order_93_query]
GO
CREATE  procedure order_93_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_ssdz_min varchar(100), @ord_ssdz_max varchar(100),@ord_ssmc_min varchar(100), @ord_ssmc_max varchar(100),@ord_date_min datetime,@ord_date_max datetime,@ord_status_min nvarchar(30), @ord_status_max nvarchar(30),@ord_zlfs_min varchar(100), @ord_zlfs_max varchar(100),@ord_szqy_min varchar(100), @ord_szqy_max varchar(100),@ord_htbh_min varchar(100), @ord_htbh_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_93 a, @pobjs
where ord_id=ordid
order by nid 
select a.* from order_93_users a , @pobjs 
where ordp_ordid=ordid and ordp_cuid=@useruid
return 
end 
if (@status is null or @status = '_none_') set @status = '' 


if (@ord_ssdz_min is null or @ord_ssdz_min = '_none_') set @ord_ssdz_min = '' 
if (@ord_ssdz_max is null or @ord_ssdz_max = '_none_') set @ord_ssdz_max = '' 

if (@ord_ssmc_min is null or @ord_ssmc_min = '_none_') set @ord_ssmc_min = '' 
if (@ord_ssmc_max is null or @ord_ssmc_max = '_none_') set @ord_ssmc_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_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_zlfs_min is null or @ord_zlfs_min = '_none_') set @ord_zlfs_min = '' 
if (@ord_zlfs_max is null or @ord_zlfs_max = '_none_') set @ord_zlfs_max = '' 

if (@ord_szqy_min is null or @ord_szqy_min = '_none_') set @ord_szqy_min = '' 
if (@ord_szqy_max is null or @ord_szqy_max = '_none_') set @ord_szqy_max = '' 

if (@ord_htbh_min is null or @ord_htbh_min = '_none_') set @ord_htbh_min = '' 
if (@ord_htbh_max is null or @ord_htbh_max = '_none_') set @ord_htbh_max = '' 

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

if @ord_ssmc_min>'' 
begin 
if (charindex('%',@ord_ssmc_min) = 0 and @ord_ssmc_min!='_empty_') 
 begin
set @ord_ssmc_min = '%'+@ord_ssmc_min+'%';
 end else if (@ord_ssmc_min='_empty_') set @ord_ssmc_min='' 
end else
begin 
set @ord_ssmc_min = '%' 
end
if @ord_ssmc_max>'' 
begin 
if (charindex('%', @ord_ssmc_max) = 0 and @ord_ssmc_max!='_empty_') 
begin
set@ord_ssmc_max = '%'+@ord_ssmc_max+'%';
 end else if (@ord_ssmc_max='_empty_') set @ord_ssmc_max='' 
end else
begin 
set @ord_ssmc_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_zlfs_min>'' 
begin 
if (charindex('%',@ord_zlfs_min) = 0 and @ord_zlfs_min!='_empty_') 
 begin
set @ord_zlfs_min = '%'+@ord_zlfs_min+'%';
 end else if (@ord_zlfs_min='_empty_') set @ord_zlfs_min='' 
end else
begin 
set @ord_zlfs_min = '%' 
end
if @ord_zlfs_max>'' 
begin 
if (charindex('%', @ord_zlfs_max) = 0 and @ord_zlfs_max!='_empty_') 
begin
set@ord_zlfs_max = '%'+@ord_zlfs_max+'%';
 end else if (@ord_zlfs_max='_empty_') set @ord_zlfs_max='' 
end else
begin 
set @ord_zlfs_max = '%' 
end

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

if @ord_htbh_min>'' 
begin 
if (charindex('%',@ord_htbh_min) = 0 and @ord_htbh_min!='_empty_') 
 begin
set @ord_htbh_min = '%'+@ord_htbh_min+'%';
 end else if (@ord_htbh_min='_empty_') set @ord_htbh_min='' 
end else
begin 
set @ord_htbh_min = '%' 
end
if @ord_htbh_max>'' 
begin 
if (charindex('%', @ord_htbh_max) = 0 and @ord_htbh_max!='_empty_') 
begin
set@ord_htbh_max = '%'+@ord_htbh_max+'%';
 end else if (@ord_htbh_max='_empty_') set @ord_htbh_max='' 
end else
begin 
set @ord_htbh_max = '%' 
end
 
    
if (@finished = -11)
begin
if (@pagesize=10000)  -- 下载
begin
select a.* from order_93 a,order_93_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_ssdz like @ord_ssdz_min 
and ord_ssdz like @ord_ssdz_max 
and ord_ssmc like @ord_ssmc_min 
and ord_ssmc like @ord_ssmc_max 
and ord_date>=@ord_date_min 
and ord_date<=@ord_date_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_zlfs like @ord_zlfs_min 
and ord_zlfs like @ord_zlfs_max 
and ord_szqy like @ord_szqy_min 
and ord_szqy like @ord_szqy_max 
and ord_htbh like @ord_htbh_min 
and ord_htbh like @ord_htbh_max 
order by ord_txjzrq desc
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_93 a,order_93_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_ssdz like @ord_ssdz_min 
and ord_ssdz like @ord_ssdz_max 
and ord_ssmc like @ord_ssmc_min 
and ord_ssmc like @ord_ssmc_max 
and ord_date>=@ord_date_min 
and ord_date<=@ord_date_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_zlfs like @ord_zlfs_min 
and ord_zlfs like @ord_zlfs_max 
and ord_szqy like @ord_szqy_min 
and ord_szqy like @ord_szqy_max 
and ord_htbh like @ord_htbh_min 
and ord_htbh like @ord_htbh_max 
order by ord_txjzrq desc
select count(1) resultcount  from datalinker where useruid=@useruid 
end else
begin
select count(1) cnt from order_93 a,order_93_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_ssdz like @ord_ssdz_min 
and ord_ssdz like @ord_ssdz_max 
and ord_ssmc like @ord_ssmc_min 
and ord_ssmc like @ord_ssmc_max 
and ord_date>=@ord_date_min 
and ord_date<=@ord_date_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_zlfs like @ord_zlfs_min 
and ord_zlfs like @ord_zlfs_max 
and ord_szqy like @ord_szqy_min 
and ord_szqy like @ord_szqy_max 
and ord_htbh like @ord_htbh_min 
and ord_htbh like @ord_htbh_max 

-- 返回所有ID
insert into @pobjs(ordid) select top(@amount) ord_id from order_93 a,order_93_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_ssdz like @ord_ssdz_min 
and ord_ssdz like @ord_ssdz_max 
and ord_ssmc like @ord_ssmc_min 
and ord_ssmc like @ord_ssmc_max 
and ord_date>=@ord_date_min 
and ord_date<=@ord_date_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_zlfs like @ord_zlfs_min 
and ord_zlfs like @ord_zlfs_max 
and ord_szqy like @ord_szqy_min 
and ord_szqy like @ord_szqy_max 
and ord_htbh like @ord_htbh_min 
and ord_htbh like @ord_htbh_max 
order by ord_txjzrq desc
select ordid from @pobjs 
order by nid 
-- 返回第一页 
select top(@pagesize) a.* from order_93 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_93_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_93 a,order_93_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_ssdz like @ord_ssdz_min 
and ord_ssdz like @ord_ssdz_max 
and ord_ssmc like @ord_ssmc_min 
and ord_ssmc like @ord_ssmc_max 
and ord_date>=@ord_date_min 
and ord_date<=@ord_date_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_zlfs like @ord_zlfs_min 
and ord_zlfs like @ord_zlfs_max 
and ord_szqy like @ord_szqy_min 
and ord_szqy like @ord_szqy_max 
and ord_htbh like @ord_htbh_min 
and ord_htbh like @ord_htbh_max 
order by ord_txjzrq desc
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_93 a,order_93_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_ssdz like @ord_ssdz_min 
and ord_ssdz like @ord_ssdz_max 
and ord_ssmc like @ord_ssmc_min 
and ord_ssmc like @ord_ssmc_max 
and ord_date>=@ord_date_min 
and ord_date<=@ord_date_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_zlfs like @ord_zlfs_min 
and ord_zlfs like @ord_zlfs_max 
and ord_szqy like @ord_szqy_min 
and ord_szqy like @ord_szqy_max 
and ord_htbh like @ord_htbh_min 
and ord_htbh like @ord_htbh_max 
order by ord_txjzrq desc
select count(1) resultcount  from datalinker where useruid=@useruid 
end else
begin
select count(1) cnt from order_93 a,order_93_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_ssdz like @ord_ssdz_min 
and ord_ssdz like @ord_ssdz_max 
and ord_ssmc like @ord_ssmc_min 
and ord_ssmc like @ord_ssmc_max 
and ord_date>=@ord_date_min 
and ord_date<=@ord_date_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_zlfs like @ord_zlfs_min 
and ord_zlfs like @ord_zlfs_max 
and ord_szqy like @ord_szqy_min 
and ord_szqy like @ord_szqy_max 
and ord_htbh like @ord_htbh_min 
and ord_htbh like @ord_htbh_max 

-- 返回所有ID
insert into @pobjs(ordid) select top(@amount) ord_id from order_93 a,order_93_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_ssdz like @ord_ssdz_min 
and ord_ssdz like @ord_ssdz_max 
and ord_ssmc like @ord_ssmc_min 
and ord_ssmc like @ord_ssmc_max 
and ord_date>=@ord_date_min 
and ord_date<=@ord_date_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_zlfs like @ord_zlfs_min 
and ord_zlfs like @ord_zlfs_max 
and ord_szqy like @ord_szqy_min 
and ord_szqy like @ord_szqy_max 
and ord_htbh like @ord_htbh_min 
and ord_htbh like @ord_htbh_max 
order by ord_txjzrq desc
select ordid from @pobjs 
order by nid 
-- 返回第一页 
select top(@pagesize) a.* from order_93 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_93_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_93 a,order_93_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_ssdz like @ord_ssdz_min 
and ord_ssdz like @ord_ssdz_max 
and ord_ssmc like @ord_ssmc_min 
and ord_ssmc like @ord_ssmc_max 
and ord_date>=@ord_date_min 
and ord_date<=@ord_date_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_zlfs like @ord_zlfs_min 
and ord_zlfs like @ord_zlfs_max 
and ord_szqy like @ord_szqy_min 
and ord_szqy like @ord_szqy_max 
and ord_htbh like @ord_htbh_min 
and ord_htbh like @ord_htbh_max 
order by ord_txjzrq desc
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_93 a,order_93_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_ssdz like @ord_ssdz_min 
and ord_ssdz like @ord_ssdz_max 
and ord_ssmc like @ord_ssmc_min 
and ord_ssmc like @ord_ssmc_max 
and ord_date>=@ord_date_min 
and ord_date<=@ord_date_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_zlfs like @ord_zlfs_min 
and ord_zlfs like @ord_zlfs_max 
and ord_szqy like @ord_szqy_min 
and ord_szqy like @ord_szqy_max 
and ord_htbh like @ord_htbh_min 
and ord_htbh like @ord_htbh_max 
order by ord_txjzrq desc
select count(1) resultcount  from datalinker where useruid=@useruid 
end else
begin
select count(1) cnt from order_93 a,order_93_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_ssdz like @ord_ssdz_min 
and ord_ssdz like @ord_ssdz_max 
and ord_ssmc like @ord_ssmc_min 
and ord_ssmc like @ord_ssmc_max 
and ord_date>=@ord_date_min 
and ord_date<=@ord_date_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_zlfs like @ord_zlfs_min 
and ord_zlfs like @ord_zlfs_max 
and ord_szqy like @ord_szqy_min 
and ord_szqy like @ord_szqy_max 
and ord_htbh like @ord_htbh_min 
and ord_htbh like @ord_htbh_max 

-- 返回所有ID
insert into @pobjs(ordid) select top(@amount) ord_id from order_93 a,order_93_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_ssdz like @ord_ssdz_min 
and ord_ssdz like @ord_ssdz_max 
and ord_ssmc like @ord_ssmc_min 
and ord_ssmc like @ord_ssmc_max 
and ord_date>=@ord_date_min 
and ord_date<=@ord_date_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_zlfs like @ord_zlfs_min 
and ord_zlfs like @ord_zlfs_max 
and ord_szqy like @ord_szqy_min 
and ord_szqy like @ord_szqy_max 
and ord_htbh like @ord_htbh_min 
and ord_htbh like @ord_htbh_max 
order by ord_txjzrq desc
select ordid from @pobjs 
order by nid 
-- 返回第一页 
select top(@pagesize) a.* from order_93 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_93_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_93 a,order_93_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_ssdz like @ord_ssdz_min 
and ord_ssdz like @ord_ssdz_max 
and ord_ssmc like @ord_ssmc_min 
and ord_ssmc like @ord_ssmc_max 
and ord_date>=@ord_date_min 
and ord_date<=@ord_date_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_zlfs like @ord_zlfs_min 
and ord_zlfs like @ord_zlfs_max 
and ord_szqy like @ord_szqy_min 
and ord_szqy like @ord_szqy_max 
and ord_htbh like @ord_htbh_min 
and ord_htbh like @ord_htbh_max 
order by ord_txjzrq desc
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_93 a,order_93_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_ssdz like @ord_ssdz_min 
and ord_ssdz like @ord_ssdz_max 
and ord_ssmc like @ord_ssmc_min 
and ord_ssmc like @ord_ssmc_max 
and ord_date>=@ord_date_min 
and ord_date<=@ord_date_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_zlfs like @ord_zlfs_min 
and ord_zlfs like @ord_zlfs_max 
and ord_szqy like @ord_szqy_min 
and ord_szqy like @ord_szqy_max 
and ord_htbh like @ord_htbh_min 
and ord_htbh like @ord_htbh_max 
order by ord_txjzrq desc
select count(1) resultcount  from datalinker where useruid=@useruid 
end else
begin
select count(1) cnt from order_93 a,order_93_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_ssdz like @ord_ssdz_min 
and ord_ssdz like @ord_ssdz_max 
and ord_ssmc like @ord_ssmc_min 
and ord_ssmc like @ord_ssmc_max 
and ord_date>=@ord_date_min 
and ord_date<=@ord_date_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_zlfs like @ord_zlfs_min 
and ord_zlfs like @ord_zlfs_max 
and ord_szqy like @ord_szqy_min 
and ord_szqy like @ord_szqy_max 
and ord_htbh like @ord_htbh_min 
and ord_htbh like @ord_htbh_max 

-- 返回所有ID
insert into @pobjs(ordid) select top(@amount) ord_id from order_93 a,order_93_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_ssdz like @ord_ssdz_min 
and ord_ssdz like @ord_ssdz_max 
and ord_ssmc like @ord_ssmc_min 
and ord_ssmc like @ord_ssmc_max 
and ord_date>=@ord_date_min 
and ord_date<=@ord_date_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_zlfs like @ord_zlfs_min 
and ord_zlfs like @ord_zlfs_max 
and ord_szqy like @ord_szqy_min 
and ord_szqy like @ord_szqy_max 
and ord_htbh like @ord_htbh_min 
and ord_htbh like @ord_htbh_max 
order by ord_txjzrq desc
select ordid from @pobjs 
order by nid 
-- 返回第一页 
select top(@pagesize) a.* from order_93 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_93_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_93 a,order_93_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_ssdz like @ord_ssdz_min 
and ord_ssdz like @ord_ssdz_max 
and ord_ssmc like @ord_ssmc_min 
and ord_ssmc like @ord_ssmc_max 
and ord_date>=@ord_date_min 
and ord_date<=@ord_date_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_zlfs like @ord_zlfs_min 
and ord_zlfs like @ord_zlfs_max 
and ord_szqy like @ord_szqy_min 
and ord_szqy like @ord_szqy_max 
and ord_htbh like @ord_htbh_min 
and ord_htbh like @ord_htbh_max 
order by ord_txjzrq desc
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_93 a,order_93_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_ssdz like @ord_ssdz_min 
and ord_ssdz like @ord_ssdz_max 
and ord_ssmc like @ord_ssmc_min 
and ord_ssmc like @ord_ssmc_max 
and ord_date>=@ord_date_min 
and ord_date<=@ord_date_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_zlfs like @ord_zlfs_min 
and ord_zlfs like @ord_zlfs_max 
and ord_szqy like @ord_szqy_min 
and ord_szqy like @ord_szqy_max 
and ord_htbh like @ord_htbh_min 
and ord_htbh like @ord_htbh_max 
order by ord_txjzrq desc
select count(1) resultcount  from datalinker where useruid=@useruid 
end else
begin
select count(1) cnt from order_93 a,order_93_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_ssdz like @ord_ssdz_min 
and ord_ssdz like @ord_ssdz_max 
and ord_ssmc like @ord_ssmc_min 
and ord_ssmc like @ord_ssmc_max 
and ord_date>=@ord_date_min 
and ord_date<=@ord_date_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_zlfs like @ord_zlfs_min 
and ord_zlfs like @ord_zlfs_max 
and ord_szqy like @ord_szqy_min 
and ord_szqy like @ord_szqy_max 
and ord_htbh like @ord_htbh_min 
and ord_htbh like @ord_htbh_max 

-- 返回所有ID
insert into @pobjs(ordid) select top(@amount) ord_id from order_93 a,order_93_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_ssdz like @ord_ssdz_min 
and ord_ssdz like @ord_ssdz_max 
and ord_ssmc like @ord_ssmc_min 
and ord_ssmc like @ord_ssmc_max 
and ord_date>=@ord_date_min 
and ord_date<=@ord_date_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_zlfs like @ord_zlfs_min 
and ord_zlfs like @ord_zlfs_max 
and ord_szqy like @ord_szqy_min 
and ord_szqy like @ord_szqy_max 
and ord_htbh like @ord_htbh_min 
and ord_htbh like @ord_htbh_max 
order by ord_txjzrq desc
select ordid from @pobjs 
order by nid 
-- 返回第一页 
select top(@pagesize) a.* from order_93 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_93_users a , @pobjs 
where ordp_ordid=ordid 
and nid<=@pagesize 
and ordp_cuid=@useruid 
end 
end 
set nocount off end