IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[order_189_query]') AND type = 'P')
DROP PROCEDURE [dbo].[order_189_query]
GO
CREATE  procedure order_189_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_xqlx_min varchar(100), @ord_xqlx_max varchar(100),@ord_status_min nvarchar(30), @ord_status_max nvarchar(30),@ord_sjyt_min varchar(100), @ord_sjyt_max varchar(100),@ord_sjlx_min varchar(100), @ord_sjlx_max varchar(100),@ord_fzr_min varchar(100), @ord_fzr_max varchar(100),@ord_creator_min nvarchar(50), @ord_creator_max nvarchar(50),@ord_createdepart_min nvarchar(50), @ord_createdepart_max nvarchar(50),@ord_wcsj_min datetime,@ord_wcsj_max datetime,@ord_xmorhd_min varchar(100), @ord_xmorhd_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_189 a, @pobjs
where ord_id=ordid
order by nid 
select a.* from order_189_users a , @pobjs 
where ordp_ordid=ordid and ordp_cuid=@useruid
return 
end 
if (@status is null or @status = '_none_') set @status = '' 


if (@ord_xqlx_min is null or @ord_xqlx_min = '_none_') set @ord_xqlx_min = '' 
if (@ord_xqlx_max is null or @ord_xqlx_max = '_none_') set @ord_xqlx_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_sjyt_min is null or @ord_sjyt_min = '_none_') set @ord_sjyt_min = '' 
if (@ord_sjyt_max is null or @ord_sjyt_max = '_none_') set @ord_sjyt_max = '' 

if (@ord_sjlx_min is null or @ord_sjlx_min = '_none_') set @ord_sjlx_min = '' 
if (@ord_sjlx_max is null or @ord_sjlx_max = '_none_') set @ord_sjlx_max = '' 

if (@ord_fzr_min is null or @ord_fzr_min = '_none_') set @ord_fzr_min = '' 
if (@ord_fzr_max is null or @ord_fzr_max = '_none_') set @ord_fzr_max = '' 

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

if (@ord_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_wcsj_min is null or @ord_wcsj_min <= @nonedate) set @ord_wcsj_min = '1900-01-01' 
                
if (@ord_wcsj_max is null or @ord_wcsj_max <= @nonedate) set @ord_wcsj_max =  '2299-12-30 23:59:59'
else set @ord_wcsj_max = @ord_wcsj_max+ '23:59:59'
                

if (@ord_xmorhd_min is null or @ord_xmorhd_min = '_none_') set @ord_xmorhd_min = '' 
if (@ord_xmorhd_max is null or @ord_xmorhd_max = '_none_') set @ord_xmorhd_max = '' 

if @ord_xqlx_min>'' 
begin 
if (charindex('%',@ord_xqlx_min) = 0 and @ord_xqlx_min!='_empty_') 
 begin
set @ord_xqlx_min = '%'+@ord_xqlx_min+'%';
 end else if (@ord_xqlx_min='_empty_') set @ord_xqlx_min='' 
end else
begin 
set @ord_xqlx_min = '%' 
end
if @ord_xqlx_max>'' 
begin 
if (charindex('%', @ord_xqlx_max) = 0 and @ord_xqlx_max!='_empty_') 
begin
set@ord_xqlx_max = '%'+@ord_xqlx_max+'%';
 end else if (@ord_xqlx_max='_empty_') set @ord_xqlx_max='' 
end else
begin 
set @ord_xqlx_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_sjyt_min>'' 
begin 
if (charindex('%',@ord_sjyt_min) = 0 and @ord_sjyt_min!='_empty_') 
 begin
set @ord_sjyt_min = '%'+@ord_sjyt_min+'%';
 end else if (@ord_sjyt_min='_empty_') set @ord_sjyt_min='' 
end else
begin 
set @ord_sjyt_min = '%' 
end
if @ord_sjyt_max>'' 
begin 
if (charindex('%', @ord_sjyt_max) = 0 and @ord_sjyt_max!='_empty_') 
begin
set@ord_sjyt_max = '%'+@ord_sjyt_max+'%';
 end else if (@ord_sjyt_max='_empty_') set @ord_sjyt_max='' 
end else
begin 
set @ord_sjyt_max = '%' 
end

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

if @ord_fzr_min>'' 
begin 
if (charindex('%',@ord_fzr_min) = 0 and @ord_fzr_min!='_empty_') 
 begin
set @ord_fzr_min = '%'+@ord_fzr_min+'%';
 end else if (@ord_fzr_min='_empty_') set @ord_fzr_min='' 
end else
begin 
set @ord_fzr_min = '%' 
end
if @ord_fzr_max>'' 
begin 
if (charindex('%', @ord_fzr_max) = 0 and @ord_fzr_max!='_empty_') 
begin
set@ord_fzr_max = '%'+@ord_fzr_max+'%';
 end else if (@ord_fzr_max='_empty_') set @ord_fzr_max='' 
end else
begin 
set @ord_fzr_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_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_xmorhd_min>'' 
begin 
if (charindex('%',@ord_xmorhd_min) = 0 and @ord_xmorhd_min!='_empty_') 
 begin
set @ord_xmorhd_min = '%'+@ord_xmorhd_min+'%';
 end else if (@ord_xmorhd_min='_empty_') set @ord_xmorhd_min='' 
end else
begin 
set @ord_xmorhd_min = '%' 
end
if @ord_xmorhd_max>'' 
begin 
if (charindex('%', @ord_xmorhd_max) = 0 and @ord_xmorhd_max!='_empty_') 
begin
set@ord_xmorhd_max = '%'+@ord_xmorhd_max+'%';
 end else if (@ord_xmorhd_max='_empty_') set @ord_xmorhd_max='' 
end else
begin 
set @ord_xmorhd_max = '%' 
end
 
    
if (@finished = -11)
begin
if (@pagesize=10000)  -- 下载
begin
select a.* from order_189 a,order_189_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_xqlx like @ord_xqlx_min 
and ord_xqlx like @ord_xqlx_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_sjyt like @ord_sjyt_min 
and ord_sjyt like @ord_sjyt_max 
and ord_sjlx like @ord_sjlx_min 
and ord_sjlx like @ord_sjlx_max 
and ord_fzr like @ord_fzr_min 
and ord_fzr like @ord_fzr_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_wcsj>=@ord_wcsj_min 
and ord_wcsj<=@ord_wcsj_max 
and ord_xmorhd like @ord_xmorhd_min 
and ord_xmorhd like @ord_xmorhd_max 
order by ord_date desc,ord_xqlx,ord_status,ord_sjyt,ord_sjlx,ord_fzr,ord_creator,ord_createdepart,ord_wcsj desc,ord_xmorhd
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_189 a,order_189_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_xqlx like @ord_xqlx_min 
and ord_xqlx like @ord_xqlx_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_sjyt like @ord_sjyt_min 
and ord_sjyt like @ord_sjyt_max 
and ord_sjlx like @ord_sjlx_min 
and ord_sjlx like @ord_sjlx_max 
and ord_fzr like @ord_fzr_min 
and ord_fzr like @ord_fzr_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_wcsj>=@ord_wcsj_min 
and ord_wcsj<=@ord_wcsj_max 
and ord_xmorhd like @ord_xmorhd_min 
and ord_xmorhd like @ord_xmorhd_max 
order by ord_date desc,ord_xqlx,ord_status,ord_sjyt,ord_sjlx,ord_fzr,ord_creator,ord_createdepart,ord_wcsj desc,ord_xmorhd
select count(1) resultcount  from datalinker where useruid=@useruid 
end else
begin
select count(1) cnt from order_189 a,order_189_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_xqlx like @ord_xqlx_min 
and ord_xqlx like @ord_xqlx_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_sjyt like @ord_sjyt_min 
and ord_sjyt like @ord_sjyt_max 
and ord_sjlx like @ord_sjlx_min 
and ord_sjlx like @ord_sjlx_max 
and ord_fzr like @ord_fzr_min 
and ord_fzr like @ord_fzr_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_wcsj>=@ord_wcsj_min 
and ord_wcsj<=@ord_wcsj_max 
and ord_xmorhd like @ord_xmorhd_min 
and ord_xmorhd like @ord_xmorhd_max 

-- 返回所有ID
insert into @pobjs(ordid) select top(@amount) ord_id from order_189 a,order_189_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_xqlx like @ord_xqlx_min 
and ord_xqlx like @ord_xqlx_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_sjyt like @ord_sjyt_min 
and ord_sjyt like @ord_sjyt_max 
and ord_sjlx like @ord_sjlx_min 
and ord_sjlx like @ord_sjlx_max 
and ord_fzr like @ord_fzr_min 
and ord_fzr like @ord_fzr_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_wcsj>=@ord_wcsj_min 
and ord_wcsj<=@ord_wcsj_max 
and ord_xmorhd like @ord_xmorhd_min 
and ord_xmorhd like @ord_xmorhd_max 
order by ord_date desc,ord_xqlx,ord_status,ord_sjyt,ord_sjlx,ord_fzr,ord_creator,ord_createdepart,ord_wcsj desc,ord_xmorhd
select ordid from @pobjs 
order by nid 
-- 返回第一页 
select top(@pagesize) a.* from order_189 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_189_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_189 a,order_189_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_xqlx like @ord_xqlx_min 
and ord_xqlx like @ord_xqlx_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_sjyt like @ord_sjyt_min 
and ord_sjyt like @ord_sjyt_max 
and ord_sjlx like @ord_sjlx_min 
and ord_sjlx like @ord_sjlx_max 
and ord_fzr like @ord_fzr_min 
and ord_fzr like @ord_fzr_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_wcsj>=@ord_wcsj_min 
and ord_wcsj<=@ord_wcsj_max 
and ord_xmorhd like @ord_xmorhd_min 
and ord_xmorhd like @ord_xmorhd_max 
order by ord_date desc,ord_xqlx,ord_status,ord_sjyt,ord_sjlx,ord_fzr,ord_creator,ord_createdepart,ord_wcsj desc,ord_xmorhd
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_189 a,order_189_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_xqlx like @ord_xqlx_min 
and ord_xqlx like @ord_xqlx_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_sjyt like @ord_sjyt_min 
and ord_sjyt like @ord_sjyt_max 
and ord_sjlx like @ord_sjlx_min 
and ord_sjlx like @ord_sjlx_max 
and ord_fzr like @ord_fzr_min 
and ord_fzr like @ord_fzr_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_wcsj>=@ord_wcsj_min 
and ord_wcsj<=@ord_wcsj_max 
and ord_xmorhd like @ord_xmorhd_min 
and ord_xmorhd like @ord_xmorhd_max 
order by ord_date desc,ord_xqlx,ord_status,ord_sjyt,ord_sjlx,ord_fzr,ord_creator,ord_createdepart,ord_wcsj desc,ord_xmorhd
select count(1) resultcount  from datalinker where useruid=@useruid 
end else
begin
select count(1) cnt from order_189 a,order_189_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_xqlx like @ord_xqlx_min 
and ord_xqlx like @ord_xqlx_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_sjyt like @ord_sjyt_min 
and ord_sjyt like @ord_sjyt_max 
and ord_sjlx like @ord_sjlx_min 
and ord_sjlx like @ord_sjlx_max 
and ord_fzr like @ord_fzr_min 
and ord_fzr like @ord_fzr_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_wcsj>=@ord_wcsj_min 
and ord_wcsj<=@ord_wcsj_max 
and ord_xmorhd like @ord_xmorhd_min 
and ord_xmorhd like @ord_xmorhd_max 

-- 返回所有ID
insert into @pobjs(ordid) select top(@amount) ord_id from order_189 a,order_189_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_xqlx like @ord_xqlx_min 
and ord_xqlx like @ord_xqlx_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_sjyt like @ord_sjyt_min 
and ord_sjyt like @ord_sjyt_max 
and ord_sjlx like @ord_sjlx_min 
and ord_sjlx like @ord_sjlx_max 
and ord_fzr like @ord_fzr_min 
and ord_fzr like @ord_fzr_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_wcsj>=@ord_wcsj_min 
and ord_wcsj<=@ord_wcsj_max 
and ord_xmorhd like @ord_xmorhd_min 
and ord_xmorhd like @ord_xmorhd_max 
order by ord_date desc,ord_xqlx,ord_status,ord_sjyt,ord_sjlx,ord_fzr,ord_creator,ord_createdepart,ord_wcsj desc,ord_xmorhd
select ordid from @pobjs 
order by nid 
-- 返回第一页 
select top(@pagesize) a.* from order_189 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_189_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_189 a,order_189_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_xqlx like @ord_xqlx_min 
and ord_xqlx like @ord_xqlx_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_sjyt like @ord_sjyt_min 
and ord_sjyt like @ord_sjyt_max 
and ord_sjlx like @ord_sjlx_min 
and ord_sjlx like @ord_sjlx_max 
and ord_fzr like @ord_fzr_min 
and ord_fzr like @ord_fzr_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_wcsj>=@ord_wcsj_min 
and ord_wcsj<=@ord_wcsj_max 
and ord_xmorhd like @ord_xmorhd_min 
and ord_xmorhd like @ord_xmorhd_max 
order by ord_date desc,ord_xqlx,ord_status,ord_sjyt,ord_sjlx,ord_fzr,ord_creator,ord_createdepart,ord_wcsj desc,ord_xmorhd
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_189 a,order_189_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_xqlx like @ord_xqlx_min 
and ord_xqlx like @ord_xqlx_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_sjyt like @ord_sjyt_min 
and ord_sjyt like @ord_sjyt_max 
and ord_sjlx like @ord_sjlx_min 
and ord_sjlx like @ord_sjlx_max 
and ord_fzr like @ord_fzr_min 
and ord_fzr like @ord_fzr_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_wcsj>=@ord_wcsj_min 
and ord_wcsj<=@ord_wcsj_max 
and ord_xmorhd like @ord_xmorhd_min 
and ord_xmorhd like @ord_xmorhd_max 
order by ord_date desc,ord_xqlx,ord_status,ord_sjyt,ord_sjlx,ord_fzr,ord_creator,ord_createdepart,ord_wcsj desc,ord_xmorhd
select count(1) resultcount  from datalinker where useruid=@useruid 
end else
begin
select count(1) cnt from order_189 a,order_189_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_xqlx like @ord_xqlx_min 
and ord_xqlx like @ord_xqlx_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_sjyt like @ord_sjyt_min 
and ord_sjyt like @ord_sjyt_max 
and ord_sjlx like @ord_sjlx_min 
and ord_sjlx like @ord_sjlx_max 
and ord_fzr like @ord_fzr_min 
and ord_fzr like @ord_fzr_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_wcsj>=@ord_wcsj_min 
and ord_wcsj<=@ord_wcsj_max 
and ord_xmorhd like @ord_xmorhd_min 
and ord_xmorhd like @ord_xmorhd_max 

-- 返回所有ID
insert into @pobjs(ordid) select top(@amount) ord_id from order_189 a,order_189_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_xqlx like @ord_xqlx_min 
and ord_xqlx like @ord_xqlx_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_sjyt like @ord_sjyt_min 
and ord_sjyt like @ord_sjyt_max 
and ord_sjlx like @ord_sjlx_min 
and ord_sjlx like @ord_sjlx_max 
and ord_fzr like @ord_fzr_min 
and ord_fzr like @ord_fzr_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_wcsj>=@ord_wcsj_min 
and ord_wcsj<=@ord_wcsj_max 
and ord_xmorhd like @ord_xmorhd_min 
and ord_xmorhd like @ord_xmorhd_max 
order by ord_date desc,ord_xqlx,ord_status,ord_sjyt,ord_sjlx,ord_fzr,ord_creator,ord_createdepart,ord_wcsj desc,ord_xmorhd
select ordid from @pobjs 
order by nid 
-- 返回第一页 
select top(@pagesize) a.* from order_189 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_189_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_189 a,order_189_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_xqlx like @ord_xqlx_min 
and ord_xqlx like @ord_xqlx_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_sjyt like @ord_sjyt_min 
and ord_sjyt like @ord_sjyt_max 
and ord_sjlx like @ord_sjlx_min 
and ord_sjlx like @ord_sjlx_max 
and ord_fzr like @ord_fzr_min 
and ord_fzr like @ord_fzr_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_wcsj>=@ord_wcsj_min 
and ord_wcsj<=@ord_wcsj_max 
and ord_xmorhd like @ord_xmorhd_min 
and ord_xmorhd like @ord_xmorhd_max 
order by ord_date desc,ord_xqlx,ord_status,ord_sjyt,ord_sjlx,ord_fzr,ord_creator,ord_createdepart,ord_wcsj desc,ord_xmorhd
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_189 a,order_189_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_xqlx like @ord_xqlx_min 
and ord_xqlx like @ord_xqlx_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_sjyt like @ord_sjyt_min 
and ord_sjyt like @ord_sjyt_max 
and ord_sjlx like @ord_sjlx_min 
and ord_sjlx like @ord_sjlx_max 
and ord_fzr like @ord_fzr_min 
and ord_fzr like @ord_fzr_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_wcsj>=@ord_wcsj_min 
and ord_wcsj<=@ord_wcsj_max 
and ord_xmorhd like @ord_xmorhd_min 
and ord_xmorhd like @ord_xmorhd_max 
order by ord_date desc,ord_xqlx,ord_status,ord_sjyt,ord_sjlx,ord_fzr,ord_creator,ord_createdepart,ord_wcsj desc,ord_xmorhd
select count(1) resultcount  from datalinker where useruid=@useruid 
end else
begin
select count(1) cnt from order_189 a,order_189_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_xqlx like @ord_xqlx_min 
and ord_xqlx like @ord_xqlx_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_sjyt like @ord_sjyt_min 
and ord_sjyt like @ord_sjyt_max 
and ord_sjlx like @ord_sjlx_min 
and ord_sjlx like @ord_sjlx_max 
and ord_fzr like @ord_fzr_min 
and ord_fzr like @ord_fzr_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_wcsj>=@ord_wcsj_min 
and ord_wcsj<=@ord_wcsj_max 
and ord_xmorhd like @ord_xmorhd_min 
and ord_xmorhd like @ord_xmorhd_max 

-- 返回所有ID
insert into @pobjs(ordid) select top(@amount) ord_id from order_189 a,order_189_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_xqlx like @ord_xqlx_min 
and ord_xqlx like @ord_xqlx_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_sjyt like @ord_sjyt_min 
and ord_sjyt like @ord_sjyt_max 
and ord_sjlx like @ord_sjlx_min 
and ord_sjlx like @ord_sjlx_max 
and ord_fzr like @ord_fzr_min 
and ord_fzr like @ord_fzr_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_wcsj>=@ord_wcsj_min 
and ord_wcsj<=@ord_wcsj_max 
and ord_xmorhd like @ord_xmorhd_min 
and ord_xmorhd like @ord_xmorhd_max 
order by ord_date desc,ord_xqlx,ord_status,ord_sjyt,ord_sjlx,ord_fzr,ord_creator,ord_createdepart,ord_wcsj desc,ord_xmorhd
select ordid from @pobjs 
order by nid 
-- 返回第一页 
select top(@pagesize) a.* from order_189 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_189_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_189 a,order_189_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_xqlx like @ord_xqlx_min 
and ord_xqlx like @ord_xqlx_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_sjyt like @ord_sjyt_min 
and ord_sjyt like @ord_sjyt_max 
and ord_sjlx like @ord_sjlx_min 
and ord_sjlx like @ord_sjlx_max 
and ord_fzr like @ord_fzr_min 
and ord_fzr like @ord_fzr_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_wcsj>=@ord_wcsj_min 
and ord_wcsj<=@ord_wcsj_max 
and ord_xmorhd like @ord_xmorhd_min 
and ord_xmorhd like @ord_xmorhd_max 
order by ord_date desc,ord_xqlx,ord_status,ord_sjyt,ord_sjlx,ord_fzr,ord_creator,ord_createdepart,ord_wcsj desc,ord_xmorhd
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_189 a,order_189_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_xqlx like @ord_xqlx_min 
and ord_xqlx like @ord_xqlx_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_sjyt like @ord_sjyt_min 
and ord_sjyt like @ord_sjyt_max 
and ord_sjlx like @ord_sjlx_min 
and ord_sjlx like @ord_sjlx_max 
and ord_fzr like @ord_fzr_min 
and ord_fzr like @ord_fzr_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_wcsj>=@ord_wcsj_min 
and ord_wcsj<=@ord_wcsj_max 
and ord_xmorhd like @ord_xmorhd_min 
and ord_xmorhd like @ord_xmorhd_max 
order by ord_date desc,ord_xqlx,ord_status,ord_sjyt,ord_sjlx,ord_fzr,ord_creator,ord_createdepart,ord_wcsj desc,ord_xmorhd
select count(1) resultcount  from datalinker where useruid=@useruid 
end else
begin
select count(1) cnt from order_189 a,order_189_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_xqlx like @ord_xqlx_min 
and ord_xqlx like @ord_xqlx_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_sjyt like @ord_sjyt_min 
and ord_sjyt like @ord_sjyt_max 
and ord_sjlx like @ord_sjlx_min 
and ord_sjlx like @ord_sjlx_max 
and ord_fzr like @ord_fzr_min 
and ord_fzr like @ord_fzr_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_wcsj>=@ord_wcsj_min 
and ord_wcsj<=@ord_wcsj_max 
and ord_xmorhd like @ord_xmorhd_min 
and ord_xmorhd like @ord_xmorhd_max 

-- 返回所有ID
insert into @pobjs(ordid) select top(@amount) ord_id from order_189 a,order_189_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_xqlx like @ord_xqlx_min 
and ord_xqlx like @ord_xqlx_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_sjyt like @ord_sjyt_min 
and ord_sjyt like @ord_sjyt_max 
and ord_sjlx like @ord_sjlx_min 
and ord_sjlx like @ord_sjlx_max 
and ord_fzr like @ord_fzr_min 
and ord_fzr like @ord_fzr_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
and ord_wcsj>=@ord_wcsj_min 
and ord_wcsj<=@ord_wcsj_max 
and ord_xmorhd like @ord_xmorhd_min 
and ord_xmorhd like @ord_xmorhd_max 
order by ord_date desc,ord_xqlx,ord_status,ord_sjyt,ord_sjlx,ord_fzr,ord_creator,ord_createdepart,ord_wcsj desc,ord_xmorhd
select ordid from @pobjs 
order by nid 
-- 返回第一页 
select top(@pagesize) a.* from order_189 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_189_users a , @pobjs 
where ordp_ordid=ordid 
and nid<=@pagesize 
and ordp_cuid=@useruid 
end 
end 
set nocount off end