IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[order_48_query]') AND type = 'P')
DROP PROCEDURE [dbo].[order_48_query]
GO
CREATE  procedure order_48_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_creator_min nvarchar(50), @ord_creator_max nvarchar(50),@ord_createdepart_min nvarchar(50), @ord_createdepart_max nvarchar(50),@ord_kclx_min varchar(100), @ord_kclx_max varchar(100),@ord_kjbh_min varchar(100), @ord_kjbh_max varchar(100),@ord_kcmc_min varchar(100), @ord_kcmc_max varchar(100),@ord_jsmc_min varchar(100), @ord_jsmc_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_48 a, @pobjs
where ord_id=ordid
order by nid 
select a.* from order_48_users a , @pobjs 
where ordp_ordid=ordid and ordp_cuid=@useruid
return 
end 
if (@status is null or @status = '_none_') set @status = '' 


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_kclx_min is null or @ord_kclx_min = '_none_') set @ord_kclx_min = '' 
if (@ord_kclx_max is null or @ord_kclx_max = '_none_') set @ord_kclx_max = '' 

if (@ord_kjbh_min is null or @ord_kjbh_min = '_none_') set @ord_kjbh_min = '' 
if (@ord_kjbh_max is null or @ord_kjbh_max = '_none_') set @ord_kjbh_max = '' 

if (@ord_kcmc_min is null or @ord_kcmc_min = '_none_') set @ord_kcmc_min = '' 
if (@ord_kcmc_max is null or @ord_kcmc_max = '_none_') set @ord_kcmc_max = '' 

if (@ord_jsmc_min is null or @ord_jsmc_min = '_none_') set @ord_jsmc_min = '' 
if (@ord_jsmc_max is null or @ord_jsmc_max = '_none_') set @ord_jsmc_max = '' 

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_kclx_min>'' 
begin 
if (charindex('%',@ord_kclx_min) = 0 and @ord_kclx_min!='_empty_') 
 begin
set @ord_kclx_min = '%'+@ord_kclx_min+'%';
 end else if (@ord_kclx_min='_empty_') set @ord_kclx_min='' 
end else
begin 
set @ord_kclx_min = '%' 
end
if @ord_kclx_max>'' 
begin 
if (charindex('%', @ord_kclx_max) = 0 and @ord_kclx_max!='_empty_') 
begin
set@ord_kclx_max = '%'+@ord_kclx_max+'%';
 end else if (@ord_kclx_max='_empty_') set @ord_kclx_max='' 
end else
begin 
set @ord_kclx_max = '%' 
end

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

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

if @ord_jsmc_min>'' 
begin 
if (charindex('%',@ord_jsmc_min) = 0 and @ord_jsmc_min!='_empty_') 
 begin
set @ord_jsmc_min = '%'+@ord_jsmc_min+'%';
 end else if (@ord_jsmc_min='_empty_') set @ord_jsmc_min='' 
end else
begin 
set @ord_jsmc_min = '%' 
end
if @ord_jsmc_max>'' 
begin 
if (charindex('%', @ord_jsmc_max) = 0 and @ord_jsmc_max!='_empty_') 
begin
set@ord_jsmc_max = '%'+@ord_jsmc_max+'%';
 end else if (@ord_jsmc_max='_empty_') set @ord_jsmc_max='' 
end else
begin 
set @ord_jsmc_max = '%' 
end
 
    
if (@finished = -11)
begin
if (@pagesize=10000)  -- 下载
begin
select a.* from order_48 a,order_48_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_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_kclx like @ord_kclx_min 
and ord_kclx like @ord_kclx_max 
and ord_kjbh like @ord_kjbh_min 
and ord_kjbh like @ord_kjbh_max 
and ord_kcmc like @ord_kcmc_min 
and ord_kcmc like @ord_kcmc_max 
and ord_jsmc like @ord_jsmc_min 
and ord_jsmc like @ord_jsmc_max 
order by ord_date desc,ord_creator,ord_createdepart,ord_kclx,ord_kjbh,ord_kcmc,ord_jsmc
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_48 a,order_48_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_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_kclx like @ord_kclx_min 
and ord_kclx like @ord_kclx_max 
and ord_kjbh like @ord_kjbh_min 
and ord_kjbh like @ord_kjbh_max 
and ord_kcmc like @ord_kcmc_min 
and ord_kcmc like @ord_kcmc_max 
and ord_jsmc like @ord_jsmc_min 
and ord_jsmc like @ord_jsmc_max 
order by ord_date desc,ord_creator,ord_createdepart,ord_kclx,ord_kjbh,ord_kcmc,ord_jsmc
select count(1) resultcount  from datalinker where useruid=@useruid 
end else
begin
select count(1) cnt from order_48 a,order_48_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_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_kclx like @ord_kclx_min 
and ord_kclx like @ord_kclx_max 
and ord_kjbh like @ord_kjbh_min 
and ord_kjbh like @ord_kjbh_max 
and ord_kcmc like @ord_kcmc_min 
and ord_kcmc like @ord_kcmc_max 
and ord_jsmc like @ord_jsmc_min 
and ord_jsmc like @ord_jsmc_max 

-- 返回所有ID
insert into @pobjs(ordid) select top(@amount) ord_id from order_48 a,order_48_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_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_kclx like @ord_kclx_min 
and ord_kclx like @ord_kclx_max 
and ord_kjbh like @ord_kjbh_min 
and ord_kjbh like @ord_kjbh_max 
and ord_kcmc like @ord_kcmc_min 
and ord_kcmc like @ord_kcmc_max 
and ord_jsmc like @ord_jsmc_min 
and ord_jsmc like @ord_jsmc_max 
order by ord_date desc,ord_creator,ord_createdepart,ord_kclx,ord_kjbh,ord_kcmc,ord_jsmc
select ordid from @pobjs 
order by nid 
-- 返回第一页 
select top(@pagesize) a.* from order_48 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_48_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_48 a,order_48_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_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_kclx like @ord_kclx_min 
and ord_kclx like @ord_kclx_max 
and ord_kjbh like @ord_kjbh_min 
and ord_kjbh like @ord_kjbh_max 
and ord_kcmc like @ord_kcmc_min 
and ord_kcmc like @ord_kcmc_max 
and ord_jsmc like @ord_jsmc_min 
and ord_jsmc like @ord_jsmc_max 
order by ord_date desc,ord_creator,ord_createdepart,ord_kclx,ord_kjbh,ord_kcmc,ord_jsmc
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_48 a,order_48_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_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_kclx like @ord_kclx_min 
and ord_kclx like @ord_kclx_max 
and ord_kjbh like @ord_kjbh_min 
and ord_kjbh like @ord_kjbh_max 
and ord_kcmc like @ord_kcmc_min 
and ord_kcmc like @ord_kcmc_max 
and ord_jsmc like @ord_jsmc_min 
and ord_jsmc like @ord_jsmc_max 
order by ord_date desc,ord_creator,ord_createdepart,ord_kclx,ord_kjbh,ord_kcmc,ord_jsmc
select count(1) resultcount  from datalinker where useruid=@useruid 
end else
begin
select count(1) cnt from order_48 a,order_48_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_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_kclx like @ord_kclx_min 
and ord_kclx like @ord_kclx_max 
and ord_kjbh like @ord_kjbh_min 
and ord_kjbh like @ord_kjbh_max 
and ord_kcmc like @ord_kcmc_min 
and ord_kcmc like @ord_kcmc_max 
and ord_jsmc like @ord_jsmc_min 
and ord_jsmc like @ord_jsmc_max 

-- 返回所有ID
insert into @pobjs(ordid) select top(@amount) ord_id from order_48 a,order_48_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_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_kclx like @ord_kclx_min 
and ord_kclx like @ord_kclx_max 
and ord_kjbh like @ord_kjbh_min 
and ord_kjbh like @ord_kjbh_max 
and ord_kcmc like @ord_kcmc_min 
and ord_kcmc like @ord_kcmc_max 
and ord_jsmc like @ord_jsmc_min 
and ord_jsmc like @ord_jsmc_max 
order by ord_date desc,ord_creator,ord_createdepart,ord_kclx,ord_kjbh,ord_kcmc,ord_jsmc
select ordid from @pobjs 
order by nid 
-- 返回第一页 
select top(@pagesize) a.* from order_48 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_48_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_48 a,order_48_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_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_kclx like @ord_kclx_min 
and ord_kclx like @ord_kclx_max 
and ord_kjbh like @ord_kjbh_min 
and ord_kjbh like @ord_kjbh_max 
and ord_kcmc like @ord_kcmc_min 
and ord_kcmc like @ord_kcmc_max 
and ord_jsmc like @ord_jsmc_min 
and ord_jsmc like @ord_jsmc_max 
order by ord_date desc,ord_creator,ord_createdepart,ord_kclx,ord_kjbh,ord_kcmc,ord_jsmc
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_48 a,order_48_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_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_kclx like @ord_kclx_min 
and ord_kclx like @ord_kclx_max 
and ord_kjbh like @ord_kjbh_min 
and ord_kjbh like @ord_kjbh_max 
and ord_kcmc like @ord_kcmc_min 
and ord_kcmc like @ord_kcmc_max 
and ord_jsmc like @ord_jsmc_min 
and ord_jsmc like @ord_jsmc_max 
order by ord_date desc,ord_creator,ord_createdepart,ord_kclx,ord_kjbh,ord_kcmc,ord_jsmc
select count(1) resultcount  from datalinker where useruid=@useruid 
end else
begin
select count(1) cnt from order_48 a,order_48_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_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_kclx like @ord_kclx_min 
and ord_kclx like @ord_kclx_max 
and ord_kjbh like @ord_kjbh_min 
and ord_kjbh like @ord_kjbh_max 
and ord_kcmc like @ord_kcmc_min 
and ord_kcmc like @ord_kcmc_max 
and ord_jsmc like @ord_jsmc_min 
and ord_jsmc like @ord_jsmc_max 

-- 返回所有ID
insert into @pobjs(ordid) select top(@amount) ord_id from order_48 a,order_48_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_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_kclx like @ord_kclx_min 
and ord_kclx like @ord_kclx_max 
and ord_kjbh like @ord_kjbh_min 
and ord_kjbh like @ord_kjbh_max 
and ord_kcmc like @ord_kcmc_min 
and ord_kcmc like @ord_kcmc_max 
and ord_jsmc like @ord_jsmc_min 
and ord_jsmc like @ord_jsmc_max 
order by ord_date desc,ord_creator,ord_createdepart,ord_kclx,ord_kjbh,ord_kcmc,ord_jsmc
select ordid from @pobjs 
order by nid 
-- 返回第一页 
select top(@pagesize) a.* from order_48 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_48_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_48 a,order_48_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_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_kclx like @ord_kclx_min 
and ord_kclx like @ord_kclx_max 
and ord_kjbh like @ord_kjbh_min 
and ord_kjbh like @ord_kjbh_max 
and ord_kcmc like @ord_kcmc_min 
and ord_kcmc like @ord_kcmc_max 
and ord_jsmc like @ord_jsmc_min 
and ord_jsmc like @ord_jsmc_max 
order by ord_date desc,ord_creator,ord_createdepart,ord_kclx,ord_kjbh,ord_kcmc,ord_jsmc
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_48 a,order_48_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_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_kclx like @ord_kclx_min 
and ord_kclx like @ord_kclx_max 
and ord_kjbh like @ord_kjbh_min 
and ord_kjbh like @ord_kjbh_max 
and ord_kcmc like @ord_kcmc_min 
and ord_kcmc like @ord_kcmc_max 
and ord_jsmc like @ord_jsmc_min 
and ord_jsmc like @ord_jsmc_max 
order by ord_date desc,ord_creator,ord_createdepart,ord_kclx,ord_kjbh,ord_kcmc,ord_jsmc
select count(1) resultcount  from datalinker where useruid=@useruid 
end else
begin
select count(1) cnt from order_48 a,order_48_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_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_kclx like @ord_kclx_min 
and ord_kclx like @ord_kclx_max 
and ord_kjbh like @ord_kjbh_min 
and ord_kjbh like @ord_kjbh_max 
and ord_kcmc like @ord_kcmc_min 
and ord_kcmc like @ord_kcmc_max 
and ord_jsmc like @ord_jsmc_min 
and ord_jsmc like @ord_jsmc_max 

-- 返回所有ID
insert into @pobjs(ordid) select top(@amount) ord_id from order_48 a,order_48_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_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_kclx like @ord_kclx_min 
and ord_kclx like @ord_kclx_max 
and ord_kjbh like @ord_kjbh_min 
and ord_kjbh like @ord_kjbh_max 
and ord_kcmc like @ord_kcmc_min 
and ord_kcmc like @ord_kcmc_max 
and ord_jsmc like @ord_jsmc_min 
and ord_jsmc like @ord_jsmc_max 
order by ord_date desc,ord_creator,ord_createdepart,ord_kclx,ord_kjbh,ord_kcmc,ord_jsmc
select ordid from @pobjs 
order by nid 
-- 返回第一页 
select top(@pagesize) a.* from order_48 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_48_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_48 a,order_48_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_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_kclx like @ord_kclx_min 
and ord_kclx like @ord_kclx_max 
and ord_kjbh like @ord_kjbh_min 
and ord_kjbh like @ord_kjbh_max 
and ord_kcmc like @ord_kcmc_min 
and ord_kcmc like @ord_kcmc_max 
and ord_jsmc like @ord_jsmc_min 
and ord_jsmc like @ord_jsmc_max 
order by ord_date desc,ord_creator,ord_createdepart,ord_kclx,ord_kjbh,ord_kcmc,ord_jsmc
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_48 a,order_48_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_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_kclx like @ord_kclx_min 
and ord_kclx like @ord_kclx_max 
and ord_kjbh like @ord_kjbh_min 
and ord_kjbh like @ord_kjbh_max 
and ord_kcmc like @ord_kcmc_min 
and ord_kcmc like @ord_kcmc_max 
and ord_jsmc like @ord_jsmc_min 
and ord_jsmc like @ord_jsmc_max 
order by ord_date desc,ord_creator,ord_createdepart,ord_kclx,ord_kjbh,ord_kcmc,ord_jsmc
select count(1) resultcount  from datalinker where useruid=@useruid 
end else
begin
select count(1) cnt from order_48 a,order_48_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_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_kclx like @ord_kclx_min 
and ord_kclx like @ord_kclx_max 
and ord_kjbh like @ord_kjbh_min 
and ord_kjbh like @ord_kjbh_max 
and ord_kcmc like @ord_kcmc_min 
and ord_kcmc like @ord_kcmc_max 
and ord_jsmc like @ord_jsmc_min 
and ord_jsmc like @ord_jsmc_max 

-- 返回所有ID
insert into @pobjs(ordid) select top(@amount) ord_id from order_48 a,order_48_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_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_kclx like @ord_kclx_min 
and ord_kclx like @ord_kclx_max 
and ord_kjbh like @ord_kjbh_min 
and ord_kjbh like @ord_kjbh_max 
and ord_kcmc like @ord_kcmc_min 
and ord_kcmc like @ord_kcmc_max 
and ord_jsmc like @ord_jsmc_min 
and ord_jsmc like @ord_jsmc_max 
order by ord_date desc,ord_creator,ord_createdepart,ord_kclx,ord_kjbh,ord_kcmc,ord_jsmc
select ordid from @pobjs 
order by nid 
-- 返回第一页 
select top(@pagesize) a.* from order_48 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_48_users a , @pobjs 
where ordp_ordid=ordid 
and nid<=@pagesize 
and ordp_cuid=@useruid 
end 
end 
set nocount off end