IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[order_32_query]') AND type = 'P')
DROP PROCEDURE [dbo].[order_32_query]
GO
CREATE  procedure order_32_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_cutitle_min varchar(100), @ord_cutitle_max varchar(100),@ord_cuname_min varchar(100), @ord_cuname_max varchar(100),@ord_oadepart_min varchar(100), @ord_oadepart_max varchar(100),@ord_lxdh_min varchar(100), @ord_lxdh_max varchar(100),@ord_createdepart_min nvarchar(50), @ord_createdepart_max nvarchar(50)) 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_32 a, @pobjs
where ord_id=ordid
order by nid 
select a.* from order_32_users a , @pobjs 
where ordp_ordid=ordid and ordp_cuid=@useruid
return 
end 
if (@status is null or @status = '_none_') set @status = '' 


if (@ord_cutitle_min is null or @ord_cutitle_min = '_none_') set @ord_cutitle_min = '' 
if (@ord_cutitle_max is null or @ord_cutitle_max = '_none_') set @ord_cutitle_max = '' 

if (@ord_cuname_min is null or @ord_cuname_min = '_none_') set @ord_cuname_min = '' 
if (@ord_cuname_max is null or @ord_cuname_max = '_none_') set @ord_cuname_max = '' 

if (@ord_oadepart_min is null or @ord_oadepart_min = '_none_') set @ord_oadepart_min = '' 
if (@ord_oadepart_max is null or @ord_oadepart_max = '_none_') set @ord_oadepart_max = '' 

if (@ord_lxdh_min is null or @ord_lxdh_min = '_none_') set @ord_lxdh_min = '' 
if (@ord_lxdh_max is null or @ord_lxdh_max = '_none_') set @ord_lxdh_max = '' 

if (@ord_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_cutitle_min>'' 
begin 
if (charindex('%',@ord_cutitle_min) = 0 and @ord_cutitle_min!='_empty_') 
 begin
set @ord_cutitle_min = '%'+@ord_cutitle_min+'%';
 end else if (@ord_cutitle_min='_empty_') set @ord_cutitle_min='' 
end else
begin 
set @ord_cutitle_min = '%' 
end
if @ord_cutitle_max>'' 
begin 
if (charindex('%', @ord_cutitle_max) = 0 and @ord_cutitle_max!='_empty_') 
begin
set@ord_cutitle_max = '%'+@ord_cutitle_max+'%';
 end else if (@ord_cutitle_max='_empty_') set @ord_cutitle_max='' 
end else
begin 
set @ord_cutitle_max = '%' 
end

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

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

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

if @ord_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 (@finished = -11)
begin
if (@pagesize=10000)  -- 下载
begin
select a.* from order_32 a,order_32_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_cutitle like @ord_cutitle_min 
and ord_cutitle like @ord_cutitle_max 
and ord_cuname like @ord_cuname_min 
and ord_cuname like @ord_cuname_max 
and ord_oadepart like @ord_oadepart_min 
and ord_oadepart like @ord_oadepart_max 
and ord_lxdh like @ord_lxdh_min 
and ord_lxdh like @ord_lxdh_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
order by ord_date desc,ord_cutitle,ord_cuname,ord_oadepart,ord_lxdh,ord_createdepart
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_32 a,order_32_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_cutitle like @ord_cutitle_min 
and ord_cutitle like @ord_cutitle_max 
and ord_cuname like @ord_cuname_min 
and ord_cuname like @ord_cuname_max 
and ord_oadepart like @ord_oadepart_min 
and ord_oadepart like @ord_oadepart_max 
and ord_lxdh like @ord_lxdh_min 
and ord_lxdh like @ord_lxdh_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
order by ord_date desc,ord_cutitle,ord_cuname,ord_oadepart,ord_lxdh,ord_createdepart
select count(1) resultcount  from datalinker where useruid=@useruid 
end else
begin
select count(1) cnt from order_32 a,order_32_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_cutitle like @ord_cutitle_min 
and ord_cutitle like @ord_cutitle_max 
and ord_cuname like @ord_cuname_min 
and ord_cuname like @ord_cuname_max 
and ord_oadepart like @ord_oadepart_min 
and ord_oadepart like @ord_oadepart_max 
and ord_lxdh like @ord_lxdh_min 
and ord_lxdh like @ord_lxdh_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 

-- 返回所有ID
insert into @pobjs(ordid) select top(@amount) ord_id from order_32 a,order_32_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_cutitle like @ord_cutitle_min 
and ord_cutitle like @ord_cutitle_max 
and ord_cuname like @ord_cuname_min 
and ord_cuname like @ord_cuname_max 
and ord_oadepart like @ord_oadepart_min 
and ord_oadepart like @ord_oadepart_max 
and ord_lxdh like @ord_lxdh_min 
and ord_lxdh like @ord_lxdh_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
order by ord_date desc,ord_cutitle,ord_cuname,ord_oadepart,ord_lxdh,ord_createdepart
select ordid from @pobjs 
order by nid 
-- 返回第一页 
select top(@pagesize) a.* from order_32 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_32_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_32 a,order_32_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_cutitle like @ord_cutitle_min 
and ord_cutitle like @ord_cutitle_max 
and ord_cuname like @ord_cuname_min 
and ord_cuname like @ord_cuname_max 
and ord_oadepart like @ord_oadepart_min 
and ord_oadepart like @ord_oadepart_max 
and ord_lxdh like @ord_lxdh_min 
and ord_lxdh like @ord_lxdh_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
order by ord_date desc,ord_cutitle,ord_cuname,ord_oadepart,ord_lxdh,ord_createdepart
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_32 a,order_32_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_cutitle like @ord_cutitle_min 
and ord_cutitle like @ord_cutitle_max 
and ord_cuname like @ord_cuname_min 
and ord_cuname like @ord_cuname_max 
and ord_oadepart like @ord_oadepart_min 
and ord_oadepart like @ord_oadepart_max 
and ord_lxdh like @ord_lxdh_min 
and ord_lxdh like @ord_lxdh_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
order by ord_date desc,ord_cutitle,ord_cuname,ord_oadepart,ord_lxdh,ord_createdepart
select count(1) resultcount  from datalinker where useruid=@useruid 
end else
begin
select count(1) cnt from order_32 a,order_32_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_cutitle like @ord_cutitle_min 
and ord_cutitle like @ord_cutitle_max 
and ord_cuname like @ord_cuname_min 
and ord_cuname like @ord_cuname_max 
and ord_oadepart like @ord_oadepart_min 
and ord_oadepart like @ord_oadepart_max 
and ord_lxdh like @ord_lxdh_min 
and ord_lxdh like @ord_lxdh_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 

-- 返回所有ID
insert into @pobjs(ordid) select top(@amount) ord_id from order_32 a,order_32_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_cutitle like @ord_cutitle_min 
and ord_cutitle like @ord_cutitle_max 
and ord_cuname like @ord_cuname_min 
and ord_cuname like @ord_cuname_max 
and ord_oadepart like @ord_oadepart_min 
and ord_oadepart like @ord_oadepart_max 
and ord_lxdh like @ord_lxdh_min 
and ord_lxdh like @ord_lxdh_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
order by ord_date desc,ord_cutitle,ord_cuname,ord_oadepart,ord_lxdh,ord_createdepart
select ordid from @pobjs 
order by nid 
-- 返回第一页 
select top(@pagesize) a.* from order_32 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_32_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_32 a,order_32_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_cutitle like @ord_cutitle_min 
and ord_cutitle like @ord_cutitle_max 
and ord_cuname like @ord_cuname_min 
and ord_cuname like @ord_cuname_max 
and ord_oadepart like @ord_oadepart_min 
and ord_oadepart like @ord_oadepart_max 
and ord_lxdh like @ord_lxdh_min 
and ord_lxdh like @ord_lxdh_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
order by ord_date desc,ord_cutitle,ord_cuname,ord_oadepart,ord_lxdh,ord_createdepart
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_32 a,order_32_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_cutitle like @ord_cutitle_min 
and ord_cutitle like @ord_cutitle_max 
and ord_cuname like @ord_cuname_min 
and ord_cuname like @ord_cuname_max 
and ord_oadepart like @ord_oadepart_min 
and ord_oadepart like @ord_oadepart_max 
and ord_lxdh like @ord_lxdh_min 
and ord_lxdh like @ord_lxdh_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
order by ord_date desc,ord_cutitle,ord_cuname,ord_oadepart,ord_lxdh,ord_createdepart
select count(1) resultcount  from datalinker where useruid=@useruid 
end else
begin
select count(1) cnt from order_32 a,order_32_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_cutitle like @ord_cutitle_min 
and ord_cutitle like @ord_cutitle_max 
and ord_cuname like @ord_cuname_min 
and ord_cuname like @ord_cuname_max 
and ord_oadepart like @ord_oadepart_min 
and ord_oadepart like @ord_oadepart_max 
and ord_lxdh like @ord_lxdh_min 
and ord_lxdh like @ord_lxdh_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 

-- 返回所有ID
insert into @pobjs(ordid) select top(@amount) ord_id from order_32 a,order_32_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_cutitle like @ord_cutitle_min 
and ord_cutitle like @ord_cutitle_max 
and ord_cuname like @ord_cuname_min 
and ord_cuname like @ord_cuname_max 
and ord_oadepart like @ord_oadepart_min 
and ord_oadepart like @ord_oadepart_max 
and ord_lxdh like @ord_lxdh_min 
and ord_lxdh like @ord_lxdh_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
order by ord_date desc,ord_cutitle,ord_cuname,ord_oadepart,ord_lxdh,ord_createdepart
select ordid from @pobjs 
order by nid 
-- 返回第一页 
select top(@pagesize) a.* from order_32 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_32_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_32 a,order_32_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_cutitle like @ord_cutitle_min 
and ord_cutitle like @ord_cutitle_max 
and ord_cuname like @ord_cuname_min 
and ord_cuname like @ord_cuname_max 
and ord_oadepart like @ord_oadepart_min 
and ord_oadepart like @ord_oadepart_max 
and ord_lxdh like @ord_lxdh_min 
and ord_lxdh like @ord_lxdh_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
order by ord_date desc,ord_cutitle,ord_cuname,ord_oadepart,ord_lxdh,ord_createdepart
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_32 a,order_32_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_cutitle like @ord_cutitle_min 
and ord_cutitle like @ord_cutitle_max 
and ord_cuname like @ord_cuname_min 
and ord_cuname like @ord_cuname_max 
and ord_oadepart like @ord_oadepart_min 
and ord_oadepart like @ord_oadepart_max 
and ord_lxdh like @ord_lxdh_min 
and ord_lxdh like @ord_lxdh_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
order by ord_date desc,ord_cutitle,ord_cuname,ord_oadepart,ord_lxdh,ord_createdepart
select count(1) resultcount  from datalinker where useruid=@useruid 
end else
begin
select count(1) cnt from order_32 a,order_32_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_cutitle like @ord_cutitle_min 
and ord_cutitle like @ord_cutitle_max 
and ord_cuname like @ord_cuname_min 
and ord_cuname like @ord_cuname_max 
and ord_oadepart like @ord_oadepart_min 
and ord_oadepart like @ord_oadepart_max 
and ord_lxdh like @ord_lxdh_min 
and ord_lxdh like @ord_lxdh_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 

-- 返回所有ID
insert into @pobjs(ordid) select top(@amount) ord_id from order_32 a,order_32_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_cutitle like @ord_cutitle_min 
and ord_cutitle like @ord_cutitle_max 
and ord_cuname like @ord_cuname_min 
and ord_cuname like @ord_cuname_max 
and ord_oadepart like @ord_oadepart_min 
and ord_oadepart like @ord_oadepart_max 
and ord_lxdh like @ord_lxdh_min 
and ord_lxdh like @ord_lxdh_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
order by ord_date desc,ord_cutitle,ord_cuname,ord_oadepart,ord_lxdh,ord_createdepart
select ordid from @pobjs 
order by nid 
-- 返回第一页 
select top(@pagesize) a.* from order_32 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_32_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_32 a,order_32_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_cutitle like @ord_cutitle_min 
and ord_cutitle like @ord_cutitle_max 
and ord_cuname like @ord_cuname_min 
and ord_cuname like @ord_cuname_max 
and ord_oadepart like @ord_oadepart_min 
and ord_oadepart like @ord_oadepart_max 
and ord_lxdh like @ord_lxdh_min 
and ord_lxdh like @ord_lxdh_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
order by ord_date desc,ord_cutitle,ord_cuname,ord_oadepart,ord_lxdh,ord_createdepart
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_32 a,order_32_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_cutitle like @ord_cutitle_min 
and ord_cutitle like @ord_cutitle_max 
and ord_cuname like @ord_cuname_min 
and ord_cuname like @ord_cuname_max 
and ord_oadepart like @ord_oadepart_min 
and ord_oadepart like @ord_oadepart_max 
and ord_lxdh like @ord_lxdh_min 
and ord_lxdh like @ord_lxdh_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
order by ord_date desc,ord_cutitle,ord_cuname,ord_oadepart,ord_lxdh,ord_createdepart
select count(1) resultcount  from datalinker where useruid=@useruid 
end else
begin
select count(1) cnt from order_32 a,order_32_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_cutitle like @ord_cutitle_min 
and ord_cutitle like @ord_cutitle_max 
and ord_cuname like @ord_cuname_min 
and ord_cuname like @ord_cuname_max 
and ord_oadepart like @ord_oadepart_min 
and ord_oadepart like @ord_oadepart_max 
and ord_lxdh like @ord_lxdh_min 
and ord_lxdh like @ord_lxdh_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 

-- 返回所有ID
insert into @pobjs(ordid) select top(@amount) ord_id from order_32 a,order_32_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_cutitle like @ord_cutitle_min 
and ord_cutitle like @ord_cutitle_max 
and ord_cuname like @ord_cuname_min 
and ord_cuname like @ord_cuname_max 
and ord_oadepart like @ord_oadepart_min 
and ord_oadepart like @ord_oadepart_max 
and ord_lxdh like @ord_lxdh_min 
and ord_lxdh like @ord_lxdh_max 
and ord_createdepart like @ord_createdepart_min 
and ord_createdepart like @ord_createdepart_max 
order by ord_date desc,ord_cutitle,ord_cuname,ord_oadepart,ord_lxdh,ord_createdepart
select ordid from @pobjs 
order by nid 
-- 返回第一页 
select top(@pagesize) a.* from order_32 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_32_users a , @pobjs 
where ordp_ordid=ordid 
and nid<=@pagesize 
and ordp_cuid=@useruid 
end 
end 
set nocount off end