IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[order_29_query]') AND type = 'P')
DROP PROCEDURE [dbo].[order_29_query]
GO
CREATE  procedure order_29_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_msjg_min varchar(100), @ord_msjg_max varchar(100),@ord_ncuname_min varchar(100), @ord_ncuname_max varchar(100),@ord_zpgw_min varchar(100), @ord_zpgw_max varchar(100),@ord_xqbm_min varchar(100), @ord_xqbm_max varchar(100),@ord_zpqd_min varchar(100), @ord_zpqd_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_29 a, @pobjs
where ord_id=ordid
order by nid 
select a.* from order_29_users a , @pobjs 
where ordp_ordid=ordid and ordp_cuid=@useruid
return 
end 
if (@status is null or @status = '_none_') set @status = '' 


if (@ord_msjg_min is null or @ord_msjg_min = '_none_') set @ord_msjg_min = '' 
if (@ord_msjg_max is null or @ord_msjg_max = '_none_') set @ord_msjg_max = '' 

if (@ord_ncuname_min is null or @ord_ncuname_min = '_none_') set @ord_ncuname_min = '' 
if (@ord_ncuname_max is null or @ord_ncuname_max = '_none_') set @ord_ncuname_max = '' 

if (@ord_zpgw_min is null or @ord_zpgw_min = '_none_') set @ord_zpgw_min = '' 
if (@ord_zpgw_max is null or @ord_zpgw_max = '_none_') set @ord_zpgw_max = '' 

if (@ord_xqbm_min is null or @ord_xqbm_min = '_none_') set @ord_xqbm_min = '' 
if (@ord_xqbm_max is null or @ord_xqbm_max = '_none_') set @ord_xqbm_max = '' 

if (@ord_zpqd_min is null or @ord_zpqd_min = '_none_') set @ord_zpqd_min = '' 
if (@ord_zpqd_max is null or @ord_zpqd_max = '_none_') set @ord_zpqd_max = '' 

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

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

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

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

if @ord_zpqd_min>'' 
begin 
if (charindex('%',@ord_zpqd_min) = 0 and @ord_zpqd_min!='_empty_') 
 begin
set @ord_zpqd_min = '%'+@ord_zpqd_min+'%';
 end else if (@ord_zpqd_min='_empty_') set @ord_zpqd_min='' 
end else
begin 
set @ord_zpqd_min = '%' 
end
if @ord_zpqd_max>'' 
begin 
if (charindex('%', @ord_zpqd_max) = 0 and @ord_zpqd_max!='_empty_') 
begin
set@ord_zpqd_max = '%'+@ord_zpqd_max+'%';
 end else if (@ord_zpqd_max='_empty_') set @ord_zpqd_max='' 
end else
begin 
set @ord_zpqd_max = '%' 
end
 
    
if (@finished = -11)
begin
if (@pagesize=10000)  -- 下载
begin
select a.* from order_29 a,order_29_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_msjg like @ord_msjg_min 
and ord_msjg like @ord_msjg_max 
and ord_ncuname like @ord_ncuname_min 
and ord_ncuname like @ord_ncuname_max 
and ord_zpgw like @ord_zpgw_min 
and ord_zpgw like @ord_zpgw_max 
and ord_xqbm like @ord_xqbm_min 
and ord_xqbm like @ord_xqbm_max 
and ord_zpqd like @ord_zpqd_min 
and ord_zpqd like @ord_zpqd_max 
order by ord_date desc,ord_msjg,ord_ncuname,ord_zpgw,ord_xqbm,ord_zpqd
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_29 a,order_29_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_msjg like @ord_msjg_min 
and ord_msjg like @ord_msjg_max 
and ord_ncuname like @ord_ncuname_min 
and ord_ncuname like @ord_ncuname_max 
and ord_zpgw like @ord_zpgw_min 
and ord_zpgw like @ord_zpgw_max 
and ord_xqbm like @ord_xqbm_min 
and ord_xqbm like @ord_xqbm_max 
and ord_zpqd like @ord_zpqd_min 
and ord_zpqd like @ord_zpqd_max 
order by ord_date desc,ord_msjg,ord_ncuname,ord_zpgw,ord_xqbm,ord_zpqd
select count(1) resultcount  from datalinker where useruid=@useruid 
end else
begin
select count(1) cnt from order_29 a,order_29_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_msjg like @ord_msjg_min 
and ord_msjg like @ord_msjg_max 
and ord_ncuname like @ord_ncuname_min 
and ord_ncuname like @ord_ncuname_max 
and ord_zpgw like @ord_zpgw_min 
and ord_zpgw like @ord_zpgw_max 
and ord_xqbm like @ord_xqbm_min 
and ord_xqbm like @ord_xqbm_max 
and ord_zpqd like @ord_zpqd_min 
and ord_zpqd like @ord_zpqd_max 

-- 返回所有ID
insert into @pobjs(ordid) select top(@amount) ord_id from order_29 a,order_29_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_msjg like @ord_msjg_min 
and ord_msjg like @ord_msjg_max 
and ord_ncuname like @ord_ncuname_min 
and ord_ncuname like @ord_ncuname_max 
and ord_zpgw like @ord_zpgw_min 
and ord_zpgw like @ord_zpgw_max 
and ord_xqbm like @ord_xqbm_min 
and ord_xqbm like @ord_xqbm_max 
and ord_zpqd like @ord_zpqd_min 
and ord_zpqd like @ord_zpqd_max 
order by ord_date desc,ord_msjg,ord_ncuname,ord_zpgw,ord_xqbm,ord_zpqd
select ordid from @pobjs 
order by nid 
-- 返回第一页 
select top(@pagesize) a.* from order_29 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_29_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_29 a,order_29_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_msjg like @ord_msjg_min 
and ord_msjg like @ord_msjg_max 
and ord_ncuname like @ord_ncuname_min 
and ord_ncuname like @ord_ncuname_max 
and ord_zpgw like @ord_zpgw_min 
and ord_zpgw like @ord_zpgw_max 
and ord_xqbm like @ord_xqbm_min 
and ord_xqbm like @ord_xqbm_max 
and ord_zpqd like @ord_zpqd_min 
and ord_zpqd like @ord_zpqd_max 
order by ord_date desc,ord_msjg,ord_ncuname,ord_zpgw,ord_xqbm,ord_zpqd
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_29 a,order_29_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_msjg like @ord_msjg_min 
and ord_msjg like @ord_msjg_max 
and ord_ncuname like @ord_ncuname_min 
and ord_ncuname like @ord_ncuname_max 
and ord_zpgw like @ord_zpgw_min 
and ord_zpgw like @ord_zpgw_max 
and ord_xqbm like @ord_xqbm_min 
and ord_xqbm like @ord_xqbm_max 
and ord_zpqd like @ord_zpqd_min 
and ord_zpqd like @ord_zpqd_max 
order by ord_date desc,ord_msjg,ord_ncuname,ord_zpgw,ord_xqbm,ord_zpqd
select count(1) resultcount  from datalinker where useruid=@useruid 
end else
begin
select count(1) cnt from order_29 a,order_29_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_msjg like @ord_msjg_min 
and ord_msjg like @ord_msjg_max 
and ord_ncuname like @ord_ncuname_min 
and ord_ncuname like @ord_ncuname_max 
and ord_zpgw like @ord_zpgw_min 
and ord_zpgw like @ord_zpgw_max 
and ord_xqbm like @ord_xqbm_min 
and ord_xqbm like @ord_xqbm_max 
and ord_zpqd like @ord_zpqd_min 
and ord_zpqd like @ord_zpqd_max 

-- 返回所有ID
insert into @pobjs(ordid) select top(@amount) ord_id from order_29 a,order_29_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_msjg like @ord_msjg_min 
and ord_msjg like @ord_msjg_max 
and ord_ncuname like @ord_ncuname_min 
and ord_ncuname like @ord_ncuname_max 
and ord_zpgw like @ord_zpgw_min 
and ord_zpgw like @ord_zpgw_max 
and ord_xqbm like @ord_xqbm_min 
and ord_xqbm like @ord_xqbm_max 
and ord_zpqd like @ord_zpqd_min 
and ord_zpqd like @ord_zpqd_max 
order by ord_date desc,ord_msjg,ord_ncuname,ord_zpgw,ord_xqbm,ord_zpqd
select ordid from @pobjs 
order by nid 
-- 返回第一页 
select top(@pagesize) a.* from order_29 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_29_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_29 a,order_29_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_msjg like @ord_msjg_min 
and ord_msjg like @ord_msjg_max 
and ord_ncuname like @ord_ncuname_min 
and ord_ncuname like @ord_ncuname_max 
and ord_zpgw like @ord_zpgw_min 
and ord_zpgw like @ord_zpgw_max 
and ord_xqbm like @ord_xqbm_min 
and ord_xqbm like @ord_xqbm_max 
and ord_zpqd like @ord_zpqd_min 
and ord_zpqd like @ord_zpqd_max 
order by ord_date desc,ord_msjg,ord_ncuname,ord_zpgw,ord_xqbm,ord_zpqd
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_29 a,order_29_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_msjg like @ord_msjg_min 
and ord_msjg like @ord_msjg_max 
and ord_ncuname like @ord_ncuname_min 
and ord_ncuname like @ord_ncuname_max 
and ord_zpgw like @ord_zpgw_min 
and ord_zpgw like @ord_zpgw_max 
and ord_xqbm like @ord_xqbm_min 
and ord_xqbm like @ord_xqbm_max 
and ord_zpqd like @ord_zpqd_min 
and ord_zpqd like @ord_zpqd_max 
order by ord_date desc,ord_msjg,ord_ncuname,ord_zpgw,ord_xqbm,ord_zpqd
select count(1) resultcount  from datalinker where useruid=@useruid 
end else
begin
select count(1) cnt from order_29 a,order_29_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_msjg like @ord_msjg_min 
and ord_msjg like @ord_msjg_max 
and ord_ncuname like @ord_ncuname_min 
and ord_ncuname like @ord_ncuname_max 
and ord_zpgw like @ord_zpgw_min 
and ord_zpgw like @ord_zpgw_max 
and ord_xqbm like @ord_xqbm_min 
and ord_xqbm like @ord_xqbm_max 
and ord_zpqd like @ord_zpqd_min 
and ord_zpqd like @ord_zpqd_max 

-- 返回所有ID
insert into @pobjs(ordid) select top(@amount) ord_id from order_29 a,order_29_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_msjg like @ord_msjg_min 
and ord_msjg like @ord_msjg_max 
and ord_ncuname like @ord_ncuname_min 
and ord_ncuname like @ord_ncuname_max 
and ord_zpgw like @ord_zpgw_min 
and ord_zpgw like @ord_zpgw_max 
and ord_xqbm like @ord_xqbm_min 
and ord_xqbm like @ord_xqbm_max 
and ord_zpqd like @ord_zpqd_min 
and ord_zpqd like @ord_zpqd_max 
order by ord_date desc,ord_msjg,ord_ncuname,ord_zpgw,ord_xqbm,ord_zpqd
select ordid from @pobjs 
order by nid 
-- 返回第一页 
select top(@pagesize) a.* from order_29 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_29_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_29 a,order_29_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_msjg like @ord_msjg_min 
and ord_msjg like @ord_msjg_max 
and ord_ncuname like @ord_ncuname_min 
and ord_ncuname like @ord_ncuname_max 
and ord_zpgw like @ord_zpgw_min 
and ord_zpgw like @ord_zpgw_max 
and ord_xqbm like @ord_xqbm_min 
and ord_xqbm like @ord_xqbm_max 
and ord_zpqd like @ord_zpqd_min 
and ord_zpqd like @ord_zpqd_max 
order by ord_date desc,ord_msjg,ord_ncuname,ord_zpgw,ord_xqbm,ord_zpqd
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_29 a,order_29_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_msjg like @ord_msjg_min 
and ord_msjg like @ord_msjg_max 
and ord_ncuname like @ord_ncuname_min 
and ord_ncuname like @ord_ncuname_max 
and ord_zpgw like @ord_zpgw_min 
and ord_zpgw like @ord_zpgw_max 
and ord_xqbm like @ord_xqbm_min 
and ord_xqbm like @ord_xqbm_max 
and ord_zpqd like @ord_zpqd_min 
and ord_zpqd like @ord_zpqd_max 
order by ord_date desc,ord_msjg,ord_ncuname,ord_zpgw,ord_xqbm,ord_zpqd
select count(1) resultcount  from datalinker where useruid=@useruid 
end else
begin
select count(1) cnt from order_29 a,order_29_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_msjg like @ord_msjg_min 
and ord_msjg like @ord_msjg_max 
and ord_ncuname like @ord_ncuname_min 
and ord_ncuname like @ord_ncuname_max 
and ord_zpgw like @ord_zpgw_min 
and ord_zpgw like @ord_zpgw_max 
and ord_xqbm like @ord_xqbm_min 
and ord_xqbm like @ord_xqbm_max 
and ord_zpqd like @ord_zpqd_min 
and ord_zpqd like @ord_zpqd_max 

-- 返回所有ID
insert into @pobjs(ordid) select top(@amount) ord_id from order_29 a,order_29_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_msjg like @ord_msjg_min 
and ord_msjg like @ord_msjg_max 
and ord_ncuname like @ord_ncuname_min 
and ord_ncuname like @ord_ncuname_max 
and ord_zpgw like @ord_zpgw_min 
and ord_zpgw like @ord_zpgw_max 
and ord_xqbm like @ord_xqbm_min 
and ord_xqbm like @ord_xqbm_max 
and ord_zpqd like @ord_zpqd_min 
and ord_zpqd like @ord_zpqd_max 
order by ord_date desc,ord_msjg,ord_ncuname,ord_zpgw,ord_xqbm,ord_zpqd
select ordid from @pobjs 
order by nid 
-- 返回第一页 
select top(@pagesize) a.* from order_29 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_29_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_29 a,order_29_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_msjg like @ord_msjg_min 
and ord_msjg like @ord_msjg_max 
and ord_ncuname like @ord_ncuname_min 
and ord_ncuname like @ord_ncuname_max 
and ord_zpgw like @ord_zpgw_min 
and ord_zpgw like @ord_zpgw_max 
and ord_xqbm like @ord_xqbm_min 
and ord_xqbm like @ord_xqbm_max 
and ord_zpqd like @ord_zpqd_min 
and ord_zpqd like @ord_zpqd_max 
order by ord_date desc,ord_msjg,ord_ncuname,ord_zpgw,ord_xqbm,ord_zpqd
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_29 a,order_29_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_msjg like @ord_msjg_min 
and ord_msjg like @ord_msjg_max 
and ord_ncuname like @ord_ncuname_min 
and ord_ncuname like @ord_ncuname_max 
and ord_zpgw like @ord_zpgw_min 
and ord_zpgw like @ord_zpgw_max 
and ord_xqbm like @ord_xqbm_min 
and ord_xqbm like @ord_xqbm_max 
and ord_zpqd like @ord_zpqd_min 
and ord_zpqd like @ord_zpqd_max 
order by ord_date desc,ord_msjg,ord_ncuname,ord_zpgw,ord_xqbm,ord_zpqd
select count(1) resultcount  from datalinker where useruid=@useruid 
end else
begin
select count(1) cnt from order_29 a,order_29_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_msjg like @ord_msjg_min 
and ord_msjg like @ord_msjg_max 
and ord_ncuname like @ord_ncuname_min 
and ord_ncuname like @ord_ncuname_max 
and ord_zpgw like @ord_zpgw_min 
and ord_zpgw like @ord_zpgw_max 
and ord_xqbm like @ord_xqbm_min 
and ord_xqbm like @ord_xqbm_max 
and ord_zpqd like @ord_zpqd_min 
and ord_zpqd like @ord_zpqd_max 

-- 返回所有ID
insert into @pobjs(ordid) select top(@amount) ord_id from order_29 a,order_29_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_msjg like @ord_msjg_min 
and ord_msjg like @ord_msjg_max 
and ord_ncuname like @ord_ncuname_min 
and ord_ncuname like @ord_ncuname_max 
and ord_zpgw like @ord_zpgw_min 
and ord_zpgw like @ord_zpgw_max 
and ord_xqbm like @ord_xqbm_min 
and ord_xqbm like @ord_xqbm_max 
and ord_zpqd like @ord_zpqd_min 
and ord_zpqd like @ord_zpqd_max 
order by ord_date desc,ord_msjg,ord_ncuname,ord_zpgw,ord_xqbm,ord_zpqd
select ordid from @pobjs 
order by nid 
-- 返回第一页 
select top(@pagesize) a.* from order_29 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_29_users a , @pobjs 
where ordp_ordid=ordid 
and nid<=@pagesize 
and ordp_cuid=@useruid 
end 
end 
set nocount off end