IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[order_260_20_query]') AND type = 'P')
DROP PROCEDURE [dbo].[order_260_20_query]
GO
CREATE  procedure order_260_20_query(
@cmpid int,@branchid int,@userdpid int,@userotid int ,@useruid int ,@bizid int ,@pagesize int , @pagecount int , @pageobjids varchar(8000),@ord_sjmc_min nvarchar(100), @ord_sjmc_max nvarchar(100),@ord_no_min varchar(50), @ord_no_max varchar(50),@ord_fwlx_min nvarchar(100), @ord_fwlx_max nvarchar(100),@ord_wxmd_min nvarchar(100), @ord_wxmd_max nvarchar(100),@ord_imei_min nvarchar(100), @ord_imei_max nvarchar(100),@ord_xm_min nvarchar(100), @ord_xm_max nvarchar(100),@ord_status_min nvarchar(30), @ord_status_max nvarchar(30),@ord_xzppxhid_min int,@ord_xzppxhid_max int,@ord_xzppid_min int,@ord_xzppid_max int,@ord_wxgcs2_min nvarchar(100), @ord_wxgcs2_max nvarchar(100),@ord_sxly_min nvarchar(100), @ord_sxly_max nvarchar(100),@ord_sjh_min nvarchar(100), @ord_sjh_max nvarchar(100),@ord_dqzt_min nvarchar(100), @ord_dqzt_max nvarchar(100),@ord_jjmd_min nvarchar(100), @ord_jjmd_max nvarchar(100),@ord_wxgcs_min nvarchar(100), @ord_wxgcs_max nvarchar(100),@ord_branch_min nvarchar(50), @ord_branch_max nvarchar(50),@ord_zdxh_min nvarchar(100), @ord_zdxh_max nvarchar(100),@ord_xzsjppid_min int,@ord_xzsjppid_max int,@ord_xzxhid_min int,@ord_xzxhid_max int,@ord_zdpp_min nvarchar(100), @ord_zdpp_max nvarchar(100),@ord_sn_min nvarchar(100), @ord_sn_max nvarchar(100),@ord_creator_min nvarchar(50), @ord_creator_max nvarchar(50)) as begin  
set nocount on  
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
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 
create table #pobjs(
nid int  identity(1, 1),
ordid bigint  primary key not null)
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_260 a  with(nolock) ,#pobjs
where ord_id=ordid
order by nid 

--select a.* from order_260_authority a , #pobjs 
--where  orda_cuid=@useruid
--drop table #pobjs
return 
end 


if (@ord_sjmc_min is null or @ord_sjmc_min = '_none_') set @ord_sjmc_min = '' 
if (@ord_sjmc_max is null or @ord_sjmc_max = '_none_') set @ord_sjmc_max = '' 

if (@ord_no_min is null or @ord_no_min = '_none_') set @ord_no_min = '' 
if (@ord_no_max is null or @ord_no_max = '_none_') set @ord_no_max = '' 

if (@ord_fwlx_min is null or @ord_fwlx_min = '_none_') set @ord_fwlx_min = '' 
if (@ord_fwlx_max is null or @ord_fwlx_max = '_none_') set @ord_fwlx_max = '' 

if (@ord_wxmd_min is null or @ord_wxmd_min = '_none_') set @ord_wxmd_min = '' 
if (@ord_wxmd_max is null or @ord_wxmd_max = '_none_') set @ord_wxmd_max = '' 

if (@ord_imei_min is null or @ord_imei_min = '_none_') set @ord_imei_min = '' 
if (@ord_imei_max is null or @ord_imei_max = '_none_') set @ord_imei_max = '' 

if (@ord_xm_min is null or @ord_xm_min = '_none_') set @ord_xm_min = '' 
if (@ord_xm_max is null or @ord_xm_max = '_none_') set @ord_xm_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_xzppxhid_min is null or @ord_xzppxhid_min = -999999999) set @ord_xzppxhid_min = -999999999 
if (@ord_xzppxhid_max is null or @ord_xzppxhid_max = -999999999) set @ord_xzppxhid_max = 999999999

if (@ord_xzppid_min is null or @ord_xzppid_min = -999999999) set @ord_xzppid_min = -999999999 
if (@ord_xzppid_max is null or @ord_xzppid_max = -999999999) set @ord_xzppid_max = 999999999

if (@ord_wxgcs2_min is null or @ord_wxgcs2_min = '_none_') set @ord_wxgcs2_min = '' 
if (@ord_wxgcs2_max is null or @ord_wxgcs2_max = '_none_') set @ord_wxgcs2_max = '' 

if (@ord_sxly_min is null or @ord_sxly_min = '_none_') set @ord_sxly_min = '' 
if (@ord_sxly_max is null or @ord_sxly_max = '_none_') set @ord_sxly_max = '' 

if (@ord_sjh_min is null or @ord_sjh_min = '_none_') set @ord_sjh_min = '' 
if (@ord_sjh_max is null or @ord_sjh_max = '_none_') set @ord_sjh_max = '' 

if (@ord_dqzt_min is null or @ord_dqzt_min = '_none_') set @ord_dqzt_min = '' 
if (@ord_dqzt_max is null or @ord_dqzt_max = '_none_') set @ord_dqzt_max = '' 

if (@ord_jjmd_min is null or @ord_jjmd_min = '_none_') set @ord_jjmd_min = '' 
if (@ord_jjmd_max is null or @ord_jjmd_max = '_none_') set @ord_jjmd_max = '' 

if (@ord_wxgcs_min is null or @ord_wxgcs_min = '_none_') set @ord_wxgcs_min = '' 
if (@ord_wxgcs_max is null or @ord_wxgcs_max = '_none_') set @ord_wxgcs_max = '' 

if (@ord_branch_min is null or @ord_branch_min = '_none_') set @ord_branch_min = '' 
if (@ord_branch_max is null or @ord_branch_max = '_none_') set @ord_branch_max = '' 

if (@ord_zdxh_min is null or @ord_zdxh_min = '_none_') set @ord_zdxh_min = '' 
if (@ord_zdxh_max is null or @ord_zdxh_max = '_none_') set @ord_zdxh_max = '' 

if (@ord_xzsjppid_min is null or @ord_xzsjppid_min = -999999999) set @ord_xzsjppid_min = -999999999 
if (@ord_xzsjppid_max is null or @ord_xzsjppid_max = -999999999) set @ord_xzsjppid_max = 999999999

if (@ord_xzxhid_min is null or @ord_xzxhid_min = -999999999) set @ord_xzxhid_min = -999999999 
if (@ord_xzxhid_max is null or @ord_xzxhid_max = -999999999) set @ord_xzxhid_max = 999999999

if (@ord_zdpp_min is null or @ord_zdpp_min = '_none_') set @ord_zdpp_min = '' 
if (@ord_zdpp_max is null or @ord_zdpp_max = '_none_') set @ord_zdpp_max = '' 

if (@ord_sn_min is null or @ord_sn_min = '_none_') set @ord_sn_min = '' 
if (@ord_sn_max is null or @ord_sn_max = '_none_') set @ord_sn_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_sjmc_min>'' 
begin 
if (charindex('%',@ord_sjmc_min) = 0 and @ord_sjmc_min!='_empty_') 
 begin
set @ord_sjmc_min = '%'+@ord_sjmc_min+'%';
 end else if (@ord_sjmc_min='_empty_') set @ord_sjmc_min='' 
end else
begin 
set @ord_sjmc_min = '%' 
end
if @ord_sjmc_max>'' 
begin 
if (charindex('%', @ord_sjmc_max) = 0 and @ord_sjmc_max!='_empty_') 
begin
set@ord_sjmc_max = '%'+@ord_sjmc_max+'%';
 end else if (@ord_sjmc_max='_empty_') set @ord_sjmc_max='' 
end else
begin 
set @ord_sjmc_max = '%' 
end

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

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

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

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

if @ord_xm_min>'' 
begin 
if (charindex('%',@ord_xm_min) = 0 and @ord_xm_min!='_empty_') 
 begin
set @ord_xm_min = '%'+@ord_xm_min+'%';
 end else if (@ord_xm_min='_empty_') set @ord_xm_min='' 
end else
begin 
set @ord_xm_min = '%' 
end
if @ord_xm_max>'' 
begin 
if (charindex('%', @ord_xm_max) = 0 and @ord_xm_max!='_empty_') 
begin
set@ord_xm_max = '%'+@ord_xm_max+'%';
 end else if (@ord_xm_max='_empty_') set @ord_xm_max='' 
end else
begin 
set @ord_xm_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_wxgcs2_min>'' 
begin 
if (charindex('%',@ord_wxgcs2_min) = 0 and @ord_wxgcs2_min!='_empty_') 
 begin
set @ord_wxgcs2_min = '%'+@ord_wxgcs2_min+'%';
 end else if (@ord_wxgcs2_min='_empty_') set @ord_wxgcs2_min='' 
end else
begin 
set @ord_wxgcs2_min = '%' 
end
if @ord_wxgcs2_max>'' 
begin 
if (charindex('%', @ord_wxgcs2_max) = 0 and @ord_wxgcs2_max!='_empty_') 
begin
set@ord_wxgcs2_max = '%'+@ord_wxgcs2_max+'%';
 end else if (@ord_wxgcs2_max='_empty_') set @ord_wxgcs2_max='' 
end else
begin 
set @ord_wxgcs2_max = '%' 
end

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

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

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

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

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

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

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

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

if @ord_sn_min>'' 
begin 
if (charindex('%',@ord_sn_min) = 0 and @ord_sn_min!='_empty_') 
 begin
set @ord_sn_min = '%'+@ord_sn_min+'%';
 end else if (@ord_sn_min='_empty_') set @ord_sn_min='' 
end else
begin 
set @ord_sn_min = '%' 
end
if @ord_sn_max>'' 
begin 
if (charindex('%', @ord_sn_max) = 0 and @ord_sn_max!='_empty_') 
begin
set@ord_sn_max = '%'+@ord_sn_max+'%';
 end else if (@ord_sn_max='_empty_') set @ord_sn_max='' 
end else
begin 
set @ord_sn_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 (@branchid <> 0)
begin
if (@pagesize=10000)  -- 下载
begin
select a.* from order_260 a  with(nolock) ,mps_job with(nolock)   
where  
 
a.ord_cmpid=@cmpid
and ord_sjmc like @ord_sjmc_min 
and ord_sjmc like @ord_sjmc_max 
and ord_no like @ord_no_min 
and ord_no like @ord_no_max 
and ord_fwlx like @ord_fwlx_min 
and ord_fwlx like @ord_fwlx_max 
and ord_wxmd like @ord_wxmd_min 
and ord_wxmd like @ord_wxmd_max 
and ord_imei like @ord_imei_min 
and ord_imei like @ord_imei_max 
and ord_xm like @ord_xm_min 
and ord_xm like @ord_xm_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_xzppxhid>=@ord_xzppxhid_min 
and ord_xzppxhid<=@ord_xzppxhid_max 
and ord_xzppid>=@ord_xzppid_min 
and ord_xzppid<=@ord_xzppid_max 
and ord_wxgcs2 like @ord_wxgcs2_min 
and ord_wxgcs2 like @ord_wxgcs2_max 
and ord_sxly like @ord_sxly_min 
and ord_sxly like @ord_sxly_max 
and ord_sjh like @ord_sjh_min 
and ord_sjh like @ord_sjh_max 
and ord_dqzt like @ord_dqzt_min 
and ord_dqzt like @ord_dqzt_max 
and ord_jjmd like @ord_jjmd_min 
and ord_jjmd like @ord_jjmd_max 
and ord_wxgcs like @ord_wxgcs_min 
and ord_wxgcs like @ord_wxgcs_max 
and ord_branch like @ord_branch_min 
and ord_branch like @ord_branch_max 
and ord_zdxh like @ord_zdxh_min 
and ord_zdxh like @ord_zdxh_max 
and ord_xzsjppid>=@ord_xzsjppid_min 
and ord_xzsjppid<=@ord_xzsjppid_max 
and ord_xzxhid>=@ord_xzxhid_min 
and ord_xzxhid<=@ord_xzxhid_max 
and ord_zdpp like @ord_zdpp_min 
and ord_zdpp like @ord_zdpp_max 
and ord_sn like @ord_sn_min 
and ord_sn like @ord_sn_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
and  (ord_cmpid=@cmpid and jb_crmid=260 and jb_ordid=ord_id and jb_cuid=@useruid and jb_wsid=0 ) 
and (ord_finished=1) order by ord_date desc
end else 
if (@pagesize>=100000000)  --  转存到中间表
begin
set @pagesize = @pagesize - 100000000 
if (@pagesize<=0) set @pagesize = 100000000 
delete datalinker where useruid = @useruid 
insert into datalinker(useruid,objid) 
select top(@pagesize) @useruid,a.ord_id  from order_260 a  with(nolock) ,mps_job with(nolock)   
where  

a.ord_cmpid=@cmpid
and ord_sjmc like @ord_sjmc_min 
and ord_sjmc like @ord_sjmc_max 
and ord_no like @ord_no_min 
and ord_no like @ord_no_max 
and ord_fwlx like @ord_fwlx_min 
and ord_fwlx like @ord_fwlx_max 
and ord_wxmd like @ord_wxmd_min 
and ord_wxmd like @ord_wxmd_max 
and ord_imei like @ord_imei_min 
and ord_imei like @ord_imei_max 
and ord_xm like @ord_xm_min 
and ord_xm like @ord_xm_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_xzppxhid>=@ord_xzppxhid_min 
and ord_xzppxhid<=@ord_xzppxhid_max 
and ord_xzppid>=@ord_xzppid_min 
and ord_xzppid<=@ord_xzppid_max 
and ord_wxgcs2 like @ord_wxgcs2_min 
and ord_wxgcs2 like @ord_wxgcs2_max 
and ord_sxly like @ord_sxly_min 
and ord_sxly like @ord_sxly_max 
and ord_sjh like @ord_sjh_min 
and ord_sjh like @ord_sjh_max 
and ord_dqzt like @ord_dqzt_min 
and ord_dqzt like @ord_dqzt_max 
and ord_jjmd like @ord_jjmd_min 
and ord_jjmd like @ord_jjmd_max 
and ord_wxgcs like @ord_wxgcs_min 
and ord_wxgcs like @ord_wxgcs_max 
and ord_branch like @ord_branch_min 
and ord_branch like @ord_branch_max 
and ord_zdxh like @ord_zdxh_min 
and ord_zdxh like @ord_zdxh_max 
and ord_xzsjppid>=@ord_xzsjppid_min 
and ord_xzsjppid<=@ord_xzsjppid_max 
and ord_xzxhid>=@ord_xzxhid_min 
and ord_xzxhid<=@ord_xzxhid_max 
and ord_zdpp like @ord_zdpp_min 
and ord_zdpp like @ord_zdpp_max 
and ord_sn like @ord_sn_min 
and ord_sn like @ord_sn_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
and  (ord_cmpid=@cmpid and jb_crmid=260 and jb_ordid=ord_id and jb_cuid=@useruid and jb_wsid=0 ) 
and (ord_finished=1) order by ord_date desc
select count(1) resultcount  from datalinker  with(nolock) where useruid=@useruid 
end else
begin
select count(1) cnt from order_260 a  with(nolock) ,mps_job with(nolock)   
where  
 
a.ord_cmpid=@cmpid
and ord_sjmc like @ord_sjmc_min 
and ord_sjmc like @ord_sjmc_max 
and ord_no like @ord_no_min 
and ord_no like @ord_no_max 
and ord_fwlx like @ord_fwlx_min 
and ord_fwlx like @ord_fwlx_max 
and ord_wxmd like @ord_wxmd_min 
and ord_wxmd like @ord_wxmd_max 
and ord_imei like @ord_imei_min 
and ord_imei like @ord_imei_max 
and ord_xm like @ord_xm_min 
and ord_xm like @ord_xm_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_xzppxhid>=@ord_xzppxhid_min 
and ord_xzppxhid<=@ord_xzppxhid_max 
and ord_xzppid>=@ord_xzppid_min 
and ord_xzppid<=@ord_xzppid_max 
and ord_wxgcs2 like @ord_wxgcs2_min 
and ord_wxgcs2 like @ord_wxgcs2_max 
and ord_sxly like @ord_sxly_min 
and ord_sxly like @ord_sxly_max 
and ord_sjh like @ord_sjh_min 
and ord_sjh like @ord_sjh_max 
and ord_dqzt like @ord_dqzt_min 
and ord_dqzt like @ord_dqzt_max 
and ord_jjmd like @ord_jjmd_min 
and ord_jjmd like @ord_jjmd_max 
and ord_wxgcs like @ord_wxgcs_min 
and ord_wxgcs like @ord_wxgcs_max 
and ord_branch like @ord_branch_min 
and ord_branch like @ord_branch_max 
and ord_zdxh like @ord_zdxh_min 
and ord_zdxh like @ord_zdxh_max 
and ord_xzsjppid>=@ord_xzsjppid_min 
and ord_xzsjppid<=@ord_xzsjppid_max 
and ord_xzxhid>=@ord_xzxhid_min 
and ord_xzxhid<=@ord_xzxhid_max 
and ord_zdpp like @ord_zdpp_min 
and ord_zdpp like @ord_zdpp_max 
and ord_sn like @ord_sn_min 
and ord_sn like @ord_sn_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
and  (ord_cmpid=@cmpid and jb_crmid=260 and jb_ordid=ord_id and jb_cuid=@useruid and jb_wsid=0 ) 
and (ord_finished=1)
-- 返回所有ID
insert into #pobjs(ordid) select top(@amount) ord_id from order_260 a  with(nolock) ,mps_job with(nolock)  
where  

a.ord_cmpid=@cmpid
and ord_sjmc like @ord_sjmc_min 
and ord_sjmc like @ord_sjmc_max 
and ord_no like @ord_no_min 
and ord_no like @ord_no_max 
and ord_fwlx like @ord_fwlx_min 
and ord_fwlx like @ord_fwlx_max 
and ord_wxmd like @ord_wxmd_min 
and ord_wxmd like @ord_wxmd_max 
and ord_imei like @ord_imei_min 
and ord_imei like @ord_imei_max 
and ord_xm like @ord_xm_min 
and ord_xm like @ord_xm_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_xzppxhid>=@ord_xzppxhid_min 
and ord_xzppxhid<=@ord_xzppxhid_max 
and ord_xzppid>=@ord_xzppid_min 
and ord_xzppid<=@ord_xzppid_max 
and ord_wxgcs2 like @ord_wxgcs2_min 
and ord_wxgcs2 like @ord_wxgcs2_max 
and ord_sxly like @ord_sxly_min 
and ord_sxly like @ord_sxly_max 
and ord_sjh like @ord_sjh_min 
and ord_sjh like @ord_sjh_max 
and ord_dqzt like @ord_dqzt_min 
and ord_dqzt like @ord_dqzt_max 
and ord_jjmd like @ord_jjmd_min 
and ord_jjmd like @ord_jjmd_max 
and ord_wxgcs like @ord_wxgcs_min 
and ord_wxgcs like @ord_wxgcs_max 
and ord_branch like @ord_branch_min 
and ord_branch like @ord_branch_max 
and ord_zdxh like @ord_zdxh_min 
and ord_zdxh like @ord_zdxh_max 
and ord_xzsjppid>=@ord_xzsjppid_min 
and ord_xzsjppid<=@ord_xzsjppid_max 
and ord_xzxhid>=@ord_xzxhid_min 
and ord_xzxhid<=@ord_xzxhid_max 
and ord_zdpp like @ord_zdpp_min 
and ord_zdpp like @ord_zdpp_max 
and ord_sn like @ord_sn_min 
and ord_sn like @ord_sn_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
and  (ord_cmpid=@cmpid and jb_crmid=260 and jb_ordid=ord_id and jb_cuid=@useruid and jb_wsid=0 ) 
and (ord_finished=1) order by ord_date desc
select ordid from #pobjs 
order by nid 
-- 返回第一页 
select top(@pagesize) a.* from order_260 a  with(nolock) ,mps_job with(nolock)  ,#pobjs 
where a.ord_id=ordid and  (ord_cmpid=@cmpid and jb_crmid=260 and jb_ordid=ord_id and jb_cuid=@useruid and jb_wsid=0 ) 

and a.ord_finished > 0
order by nid 
-- 返回权限 
--select a.* from order_260_authority a with(nolock)  , #pobjs 
--where 
--nid<=@pagesize 
--and orda_cuid=@useruid 
end 
end else
begin
if (@pagesize=10000)  -- 下载
begin
select a.* from order_260 a  with(nolock) ,mps_job with(nolock)  
where  
 
a.ord_cmpid=@cmpid
and ord_sjmc like @ord_sjmc_min 
and ord_sjmc like @ord_sjmc_max 
and ord_no like @ord_no_min 
and ord_no like @ord_no_max 
and ord_fwlx like @ord_fwlx_min 
and ord_fwlx like @ord_fwlx_max 
and ord_wxmd like @ord_wxmd_min 
and ord_wxmd like @ord_wxmd_max 
and ord_imei like @ord_imei_min 
and ord_imei like @ord_imei_max 
and ord_xm like @ord_xm_min 
and ord_xm like @ord_xm_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_xzppxhid>=@ord_xzppxhid_min 
and ord_xzppxhid<=@ord_xzppxhid_max 
and ord_xzppid>=@ord_xzppid_min 
and ord_xzppid<=@ord_xzppid_max 
and ord_wxgcs2 like @ord_wxgcs2_min 
and ord_wxgcs2 like @ord_wxgcs2_max 
and ord_sxly like @ord_sxly_min 
and ord_sxly like @ord_sxly_max 
and ord_sjh like @ord_sjh_min 
and ord_sjh like @ord_sjh_max 
and ord_dqzt like @ord_dqzt_min 
and ord_dqzt like @ord_dqzt_max 
and ord_jjmd like @ord_jjmd_min 
and ord_jjmd like @ord_jjmd_max 
and ord_wxgcs like @ord_wxgcs_min 
and ord_wxgcs like @ord_wxgcs_max 
and ord_branch like @ord_branch_min 
and ord_branch like @ord_branch_max 
and ord_zdxh like @ord_zdxh_min 
and ord_zdxh like @ord_zdxh_max 
and ord_xzsjppid>=@ord_xzsjppid_min 
and ord_xzsjppid<=@ord_xzsjppid_max 
and ord_xzxhid>=@ord_xzxhid_min 
and ord_xzxhid<=@ord_xzxhid_max 
and ord_zdpp like @ord_zdpp_min 
and ord_zdpp like @ord_zdpp_max 
and ord_sn like @ord_sn_min 
and ord_sn like @ord_sn_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
and  (ord_cmpid=@cmpid and jb_crmid=260 and jb_ordid=ord_id and jb_cuid=@useruid and jb_wsid=0 ) 
and (ord_finished=1) order by ord_date desc
end else 
if (@pagesize>=100000000)  --  转存到中间表
begin
set @pagesize = @pagesize - 100000000 
if (@pagesize<=0) set @pagesize = 100000000 
delete datalinker where useruid = @useruid 
insert into datalinker(useruid,objid) 
select top(@pagesize) @useruid,a.ord_id  from order_260 a  with(nolock) ,mps_job with(nolock)  
where  

a.ord_cmpid=@cmpid
and ord_sjmc like @ord_sjmc_min 
and ord_sjmc like @ord_sjmc_max 
and ord_no like @ord_no_min 
and ord_no like @ord_no_max 
and ord_fwlx like @ord_fwlx_min 
and ord_fwlx like @ord_fwlx_max 
and ord_wxmd like @ord_wxmd_min 
and ord_wxmd like @ord_wxmd_max 
and ord_imei like @ord_imei_min 
and ord_imei like @ord_imei_max 
and ord_xm like @ord_xm_min 
and ord_xm like @ord_xm_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_xzppxhid>=@ord_xzppxhid_min 
and ord_xzppxhid<=@ord_xzppxhid_max 
and ord_xzppid>=@ord_xzppid_min 
and ord_xzppid<=@ord_xzppid_max 
and ord_wxgcs2 like @ord_wxgcs2_min 
and ord_wxgcs2 like @ord_wxgcs2_max 
and ord_sxly like @ord_sxly_min 
and ord_sxly like @ord_sxly_max 
and ord_sjh like @ord_sjh_min 
and ord_sjh like @ord_sjh_max 
and ord_dqzt like @ord_dqzt_min 
and ord_dqzt like @ord_dqzt_max 
and ord_jjmd like @ord_jjmd_min 
and ord_jjmd like @ord_jjmd_max 
and ord_wxgcs like @ord_wxgcs_min 
and ord_wxgcs like @ord_wxgcs_max 
and ord_branch like @ord_branch_min 
and ord_branch like @ord_branch_max 
and ord_zdxh like @ord_zdxh_min 
and ord_zdxh like @ord_zdxh_max 
and ord_xzsjppid>=@ord_xzsjppid_min 
and ord_xzsjppid<=@ord_xzsjppid_max 
and ord_xzxhid>=@ord_xzxhid_min 
and ord_xzxhid<=@ord_xzxhid_max 
and ord_zdpp like @ord_zdpp_min 
and ord_zdpp like @ord_zdpp_max 
and ord_sn like @ord_sn_min 
and ord_sn like @ord_sn_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
and  (ord_cmpid=@cmpid and jb_crmid=260 and jb_ordid=ord_id and jb_cuid=@useruid and jb_wsid=0 ) 
and (ord_finished=1) order by ord_date desc
select count(1) resultcount  from datalinker  with(nolock) where useruid=@useruid 
end else
begin
select count(1) cnt from order_260 a  with(nolock) ,mps_job with(nolock) 
where  
 
a.ord_cmpid=@cmpid
and ord_sjmc like @ord_sjmc_min 
and ord_sjmc like @ord_sjmc_max 
and ord_no like @ord_no_min 
and ord_no like @ord_no_max 
and ord_fwlx like @ord_fwlx_min 
and ord_fwlx like @ord_fwlx_max 
and ord_wxmd like @ord_wxmd_min 
and ord_wxmd like @ord_wxmd_max 
and ord_imei like @ord_imei_min 
and ord_imei like @ord_imei_max 
and ord_xm like @ord_xm_min 
and ord_xm like @ord_xm_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_xzppxhid>=@ord_xzppxhid_min 
and ord_xzppxhid<=@ord_xzppxhid_max 
and ord_xzppid>=@ord_xzppid_min 
and ord_xzppid<=@ord_xzppid_max 
and ord_wxgcs2 like @ord_wxgcs2_min 
and ord_wxgcs2 like @ord_wxgcs2_max 
and ord_sxly like @ord_sxly_min 
and ord_sxly like @ord_sxly_max 
and ord_sjh like @ord_sjh_min 
and ord_sjh like @ord_sjh_max 
and ord_dqzt like @ord_dqzt_min 
and ord_dqzt like @ord_dqzt_max 
and ord_jjmd like @ord_jjmd_min 
and ord_jjmd like @ord_jjmd_max 
and ord_wxgcs like @ord_wxgcs_min 
and ord_wxgcs like @ord_wxgcs_max 
and ord_branch like @ord_branch_min 
and ord_branch like @ord_branch_max 
and ord_zdxh like @ord_zdxh_min 
and ord_zdxh like @ord_zdxh_max 
and ord_xzsjppid>=@ord_xzsjppid_min 
and ord_xzsjppid<=@ord_xzsjppid_max 
and ord_xzxhid>=@ord_xzxhid_min 
and ord_xzxhid<=@ord_xzxhid_max 
and ord_zdpp like @ord_zdpp_min 
and ord_zdpp like @ord_zdpp_max 
and ord_sn like @ord_sn_min 
and ord_sn like @ord_sn_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
and  (ord_cmpid=@cmpid and jb_crmid=260 and jb_ordid=ord_id and jb_cuid=@useruid and jb_wsid=0 ) 
and (ord_finished=1)
-- 返回所有ID
insert into #pobjs(ordid) select top(@amount) ord_id from order_260 a  with(nolock) ,mps_job with(nolock)  
where  

a.ord_cmpid=@cmpid
and ord_sjmc like @ord_sjmc_min 
and ord_sjmc like @ord_sjmc_max 
and ord_no like @ord_no_min 
and ord_no like @ord_no_max 
and ord_fwlx like @ord_fwlx_min 
and ord_fwlx like @ord_fwlx_max 
and ord_wxmd like @ord_wxmd_min 
and ord_wxmd like @ord_wxmd_max 
and ord_imei like @ord_imei_min 
and ord_imei like @ord_imei_max 
and ord_xm like @ord_xm_min 
and ord_xm like @ord_xm_max 
and ord_status like @ord_status_min 
and ord_status like @ord_status_max 
and ord_xzppxhid>=@ord_xzppxhid_min 
and ord_xzppxhid<=@ord_xzppxhid_max 
and ord_xzppid>=@ord_xzppid_min 
and ord_xzppid<=@ord_xzppid_max 
and ord_wxgcs2 like @ord_wxgcs2_min 
and ord_wxgcs2 like @ord_wxgcs2_max 
and ord_sxly like @ord_sxly_min 
and ord_sxly like @ord_sxly_max 
and ord_sjh like @ord_sjh_min 
and ord_sjh like @ord_sjh_max 
and ord_dqzt like @ord_dqzt_min 
and ord_dqzt like @ord_dqzt_max 
and ord_jjmd like @ord_jjmd_min 
and ord_jjmd like @ord_jjmd_max 
and ord_wxgcs like @ord_wxgcs_min 
and ord_wxgcs like @ord_wxgcs_max 
and ord_branch like @ord_branch_min 
and ord_branch like @ord_branch_max 
and ord_zdxh like @ord_zdxh_min 
and ord_zdxh like @ord_zdxh_max 
and ord_xzsjppid>=@ord_xzsjppid_min 
and ord_xzsjppid<=@ord_xzsjppid_max 
and ord_xzxhid>=@ord_xzxhid_min 
and ord_xzxhid<=@ord_xzxhid_max 
and ord_zdpp like @ord_zdpp_min 
and ord_zdpp like @ord_zdpp_max 
and ord_sn like @ord_sn_min 
and ord_sn like @ord_sn_max 
and ord_creator like @ord_creator_min 
and ord_creator like @ord_creator_max 
and  (ord_cmpid=@cmpid and jb_crmid=260 and jb_ordid=ord_id and jb_cuid=@useruid and jb_wsid=0 ) 
and (ord_finished=1) order by ord_date desc
select ordid from #pobjs 
order by nid 
-- 返回第一页 
select top(@pagesize) a.* from order_260 a  with(nolock) ,mps_job with(nolock)  ,#pobjs 
where a.ord_id=ordid and  (ord_cmpid=@cmpid and jb_crmid=260 and jb_ordid=ord_id and jb_cuid=@useruid and jb_wsid=0 ) 

and a.ord_finished > 0
order by nid 
-- 返回权限 
--select a.* from order_260_authority a with(nolock)  , #pobjs 
--where 
  --  nid<=@pagesize 
--and orda_cuid=@useruid 
end 
end
drop table #pobjs
set nocount off end