IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[order_240_62_query_simple]') AND type = 'P')
DROP PROCEDURE [dbo].[order_240_62_query_simple]
GO
CREATE  procedure order_240_62_query_simple(
@cmpid int,@branchid int,@userdpid int,@userotid int ,@useruid int ,@bizid int ,@pagesize int , @pagecount int , @pageobjids varchar(8000),@date1 datetime ,@date2 datetime ,@key1 nvarchar(50),@key2 nvarchar(50),@key3 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_240 a  with(nolock) ,#pobjs
where ord_id=ordid
order by nid 

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


if (@key1='') set @key1='%' 
if (charindex('%',@key1)<=0 and @key1>'') set @key1='%'+@key1+'%'

if (@key2='') set @key2='%' 
if (charindex('%',@key2)<=0 and @key2>'') set @key2='%'+@key2+'%'

if (@key3='') set @key3='%' 
if (charindex('%',@key3)<=0 and @key3>'') set @key3='%'+@key3+'%'


if (@branchid <> 0)
begin
if (@key1='%')
 begin
if (@pagesize=10000)  -- 下载
begin
select a.* from order_240 a  with(nolock) ,order_240_authority  with(nolock)
where  
(orda_ordafid=ord_authority_factor
and orda_cuid=@useruid) and   
a.ord_cmpid=@cmpid
and
(ord_date >= @date1 and ord_date <= @date2 )  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_240 a  with(nolock) ,order_240_authority  with(nolock) 
where  
(orda_ordafid=ord_authority_factor
and orda_cuid=@useruid) and  
a.ord_cmpid=@cmpid
and
(ord_date >= @date1 and ord_date <= @date2 )  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_240 a  with(nolock) ,order_240_authority  with(nolock) 
where  
(orda_ordafid=ord_authority_factor
and orda_cuid=@useruid) and   
a.ord_cmpid=@cmpid
and
(ord_date >= @date1 and ord_date <= @date2 )  and (ord_finished>=1)

-- 返回所有ID
insert into #pobjs(ordid) select top(@amount) ord_id from order_240 a  with(nolock) ,order_240_authority  with(nolock)
where  
(orda_ordafid=ord_authority_factor
and orda_cuid=@useruid) and  
a.ord_cmpid=@cmpid
and
(ord_date >= @date1 and ord_date <= @date2 )  and (ord_finished>=1)
order by ord_date desc
select ordid from #pobjs 
order by nid 
-- 返回第一页 
select top(@pagesize) a.* from order_240 a  with(nolock)  ,#pobjs 
where a.ord_id=ordid 
and a.ord_finished > 0
order by nid 
-- 返回权限 
--select a.* from order_240_authority a with(nolock)  , #pobjs 
--where 
--nid<=@pagesize 
--and orda_cuid=@useruid 
end 
end
if (@key1!='%' and @key2='%')
    begin
if (@pagesize=10000)  -- 下载
begin
select a.* from order_240 a  with(nolock) ,order_240_authority  with(nolock)
where  
(orda_ordafid=ord_authority_factor
and orda_cuid=@useruid) and   
a.ord_cmpid=@cmpid
and (
(ord_no like @key1 ) or 
(ord_creator like @key1 ) ) and 
(ord_date >= @date1 and ord_date <= @date2 )  
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_240 a  with(nolock) ,order_240_authority  with(nolock) 
where  
(orda_ordafid=ord_authority_factor
and orda_cuid=@useruid) and  
a.ord_cmpid=@cmpid
and (
(ord_no like @key1 ) or 
(ord_creator like @key1 ) ) and 
(ord_date >= @date1 and ord_date <= @date2 )  
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_240 a  with(nolock) ,order_240_authority  with(nolock) 
where  
(orda_ordafid=ord_authority_factor
and orda_cuid=@useruid) and   
a.ord_cmpid=@cmpid
and (
(ord_no like @key1 ) or 
(ord_creator like @key1 ) ) and 
(ord_date >= @date1 and ord_date <= @date2 )  
and (ord_finished>=1)

-- 返回所有ID
insert into #pobjs(ordid) select top(@amount) ord_id from order_240 a  with(nolock) ,order_240_authority  with(nolock) 
where  
(orda_ordafid=ord_authority_factor
and orda_cuid=@useruid) and  
a.ord_cmpid=@cmpid
and (
(ord_no like @key1 ) or 
(ord_creator like @key1 ) ) and 
(ord_date >= @date1 and ord_date <= @date2 )  
and (ord_finished>=1)
order by ord_date desc
select ordid from #pobjs 
order by nid 
-- 返回第一页 
select top(@pagesize) a.* from order_240 a  with(nolock)  ,#pobjs 
where a.ord_id=ordid 
and a.ord_finished > 0
order by nid 
-- 返回权限 
--select a.* from order_240_authority a with(nolock)  , #pobjs 
--where 
--nid<=@pagesize 
--and orda_cuid=@useruid 
end 
end
if (@key1!='%' and @key2!='%' and @key3='%')
    begin
if (@pagesize=10000)  -- 下载
begin
select a.* from order_240 a  with(nolock) ,order_240_authority  with(nolock) 
where  
(orda_ordafid=ord_authority_factor
and orda_cuid=@useruid) and   
a.ord_cmpid=@cmpid
and (
(ord_no like @key1 and ord_no like @key2 ) or 
(ord_creator like @key1 and ord_creator like @key2 ) ) and 
(ord_date >= @date1 and ord_date <= @date2 )  
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_240 a  with(nolock) ,order_240_authority  with(nolock) 
where  
(orda_ordafid=ord_authority_factor
and orda_cuid=@useruid) and  
a.ord_cmpid=@cmpid
and (
(ord_no like @key1 and ord_no like @key2 ) or 
(ord_creator like @key1 and ord_creator like @key2 ) ) and 
(ord_date >= @date1 and ord_date <= @date2 )  
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_240 a  with(nolock) ,order_240_authority  with(nolock) 
where  
(orda_ordafid=ord_authority_factor
and orda_cuid=@useruid) and   
a.ord_cmpid=@cmpid
and (
(ord_no like @key1 and ord_no like @key2 ) or 
(ord_creator like @key1 and ord_creator like @key2 ) ) and 
(ord_date >= @date1 and ord_date <= @date2 )  
and (ord_finished>=1)

-- 返回所有ID
insert into #pobjs(ordid) select top(@amount) ord_id from order_240 a  with(nolock) ,order_240_authority  with(nolock) 
where  
(orda_ordafid=ord_authority_factor
and orda_cuid=@useruid) and  
a.ord_cmpid=@cmpid
and (
(ord_no like @key1 and ord_no like @key2 ) or 
(ord_creator like @key1 and ord_creator like @key2 ) ) and 
(ord_date >= @date1 and ord_date <= @date2 )  
and (ord_finished>=1)
order by ord_date desc
select ordid from #pobjs 
order by nid 
-- 返回第一页 
select top(@pagesize) a.* from order_240 a  with(nolock)  ,#pobjs 
where a.ord_id=ordid 
and a.ord_finished > 0
order by nid 
-- 返回权限 
--select a.* from order_240_authority a with(nolock)  , #pobjs 
--where 
--nid<=@pagesize 
--and orda_cuid=@useruid 
end 
end
if (@key1!='%' and @key2!='%' and @key1!='%')
    begin
if (@pagesize=10000)  -- 下载
begin
select a.* from order_240 a  with(nolock) ,order_240_authority  with(nolock)
where  
(orda_ordafid=ord_authority_factor
and orda_cuid=@useruid) and   
a.ord_cmpid=@cmpid
and (
(ord_no like @key1 and ord_no like @key2 and ord_no like @key3 ) or 
(ord_creator like @key1 and ord_creator like @key2 and ord_creator like @key3 ) ) and 
(ord_date >= @date1 and ord_date <= @date2 )  
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_240 a  with(nolock) ,order_240_authority  with(nolock) 
where  
(orda_ordafid=ord_authority_factor
and orda_cuid=@useruid) and  
a.ord_cmpid=@cmpid
and (
(ord_no like @key1 and ord_no like @key2 and ord_no like @key3 ) or 
(ord_creator like @key1 and ord_creator like @key2 and ord_creator like @key3 ) ) and 
(ord_date >= @date1 and ord_date <= @date2 )  
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_240 a  with(nolock) ,order_240_authority  with(nolock) 
where  
(orda_ordafid=ord_authority_factor
and orda_cuid=@useruid) and   
a.ord_cmpid=@cmpid
and (
(ord_no like @key1 and ord_no like @key2 and ord_no like @key3 ) or 
(ord_creator like @key1 and ord_creator like @key2 and ord_creator like @key3 ) ) and 
(ord_date >= @date1 and ord_date <= @date2 )  
and (ord_finished>=1)

-- 返回所有ID
insert into #pobjs(ordid) select top(@amount) ord_id from order_240 a  with(nolock) ,order_240_authority  with(nolock) 
where  
(orda_ordafid=ord_authority_factor
and orda_cuid=@useruid) and  
a.ord_cmpid=@cmpid
and (
(ord_no like @key1 and ord_no like @key2 and ord_no like @key3 ) or 
(ord_creator like @key1 and ord_creator like @key2 and ord_creator like @key3 ) ) and 
(ord_date >= @date1 and ord_date <= @date2 )  
and (ord_finished>=1)
order by ord_date desc
select ordid from #pobjs 
order by nid 
-- 返回第一页 
select top(@pagesize) a.* from order_240 a  with(nolock)  ,#pobjs 
where a.ord_id=ordid 
and a.ord_finished > 0
order by nid 
-- 返回权限 
--select a.* from order_240_authority a with(nolock)  , #pobjs 
--where 
--nid<=@pagesize 
--and orda_cuid=@useruid 
end 
end
end else 
begin
if (@key1='%')
    begin
if (@pagesize=10000)  -- 下载
begin
select a.* from order_240 a  with(nolock) ,order_240_authority  with(nolock)
where  
(orda_ordafid=ord_authority_factor
and orda_cuid=@useruid) and   
a.ord_cmpid=@cmpid
and
(ord_date >= @date1 and ord_date <= @date2 )  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_240 a  with(nolock) ,order_240_authority  with(nolock)
where  
(orda_ordafid=ord_authority_factor
and orda_cuid=@useruid) and  
a.ord_cmpid=@cmpid
and
(ord_date >= @date1 and ord_date <= @date2 )  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_240 a  with(nolock) ,order_240_authority  with(nolock) 
where  
(orda_ordafid=ord_authority_factor
and orda_cuid=@useruid) and   
a.ord_cmpid=@cmpid
and
(ord_date >= @date1 and ord_date <= @date2 )  and (ord_finished>=1)

-- 返回所有ID
insert into #pobjs(ordid) select top(@amount) ord_id from order_240 a  with(nolock) ,order_240_authority  with(nolock) 
where  
(orda_ordafid=ord_authority_factor
and orda_cuid=@useruid) and  
a.ord_cmpid=@cmpid
and
(ord_date >= @date1 and ord_date <= @date2 )  and (ord_finished>=1)
order by ord_date desc
select ordid from #pobjs 
order by nid 
-- 返回第一页 
select top(@pagesize) a.* from order_240 a  with(nolock)  ,#pobjs 
where a.ord_id=ordid 
and a.ord_finished > 0
order by nid 
-- 返回权限 
--select a.* from order_240_authority a with(nolock)  , #pobjs 
--where 
--nid<=@pagesize 
--and orda_cuid=@useruid 
end 
end
if (@key1!='%' and @key2='%')
    begin
if (@pagesize=10000)  -- 下载
begin
select a.* from order_240 a  with(nolock) ,order_240_authority  with(nolock)
where  
(orda_ordafid=ord_authority_factor
and orda_cuid=@useruid) and   
a.ord_cmpid=@cmpid
and (
(ord_no like @key1 ) or 
(ord_creator like @key1 ) ) and 
(ord_date >= @date1 and ord_date <= @date2 )  
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_240 a  with(nolock) ,order_240_authority  with(nolock) 
where 
(orda_ordafid=ord_authority_factor
and orda_cuid=@useruid) and  
a.ord_cmpid=@cmpid
and (
(ord_no like @key1 ) or 
(ord_creator like @key1 ) ) and 
(ord_date >= @date1 and ord_date <= @date2 )  
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_240 a  with(nolock) ,order_240_authority  with(nolock)
where  
(orda_ordafid=ord_authority_factor
and orda_cuid=@useruid) and   
a.ord_cmpid=@cmpid
and (
(ord_no like @key1 ) or 
(ord_creator like @key1 ) ) and 
(ord_date >= @date1 and ord_date <= @date2 )  
and (ord_finished>=1)

-- 返回所有ID
insert into #pobjs(ordid) select top(@amount) ord_id from order_240 a  with(nolock) ,order_240_authority  with(nolock) 
where  
(orda_ordafid=ord_authority_factor
and orda_cuid=@useruid) and  
a.ord_cmpid=@cmpid
and (
(ord_no like @key1 ) or 
(ord_creator like @key1 ) ) and 
(ord_date >= @date1 and ord_date <= @date2 )  
and (ord_finished>=1)
order by ord_date desc
select ordid from #pobjs 
order by nid 
-- 返回第一页 
select top(@pagesize) a.* from order_240 a  with(nolock)  ,#pobjs 
where a.ord_id=ordid 
and a.ord_finished > 0
order by nid 
-- 返回权限 
--select a.* from order_240_authority a with(nolock)  , #pobjs 
--where 
--nid<=@pagesize 
--and orda_cuid=@useruid 
end 
end
if (@key1!='%' and @key2!='%' and @key3='%')
    begin
if (@pagesize=10000)  -- 下载
begin
select a.* from order_240 a  with(nolock) ,order_240_authority  with(nolock) 
where  
(orda_ordafid=ord_authority_factor
and orda_cuid=@useruid) and   
a.ord_cmpid=@cmpid
and (
(ord_no like @key1 and ord_no like @key2 ) or 
(ord_creator like @key1 and ord_creator like @key2 ) ) and 
(ord_date >= @date1 and ord_date <= @date2 )  
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_240 a  with(nolock) ,order_240_authority  with(nolock) 
where  
(orda_ordafid=ord_authority_factor
and orda_cuid=@useruid) and  
a.ord_cmpid=@cmpid
and (
(ord_no like @key1 and ord_no like @key2 ) or 
(ord_creator like @key1 and ord_creator like @key2 ) ) and 
(ord_date >= @date1 and ord_date <= @date2 )  
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_240 a  with(nolock) ,order_240_authority  with(nolock)
where  
(orda_ordafid=ord_authority_factor
and orda_cuid=@useruid) and   
a.ord_cmpid=@cmpid
and (
(ord_no like @key1 and ord_no like @key2 ) or 
(ord_creator like @key1 and ord_creator like @key2 ) ) and 
(ord_date >= @date1 and ord_date <= @date2 )  
and (ord_finished>=1)

-- 返回所有ID
insert into #pobjs(ordid) select top(@amount) ord_id from order_240 a  with(nolock) ,order_240_authority  with(nolock)
where 
(orda_ordafid=ord_authority_factor
and orda_cuid=@useruid) and  
a.ord_cmpid=@cmpid
and (
(ord_no like @key1 and ord_no like @key2 ) or 
(ord_creator like @key1 and ord_creator like @key2 ) ) and 
(ord_date >= @date1 and ord_date <= @date2 )  
and (ord_finished>=1)
order by ord_date desc
select ordid from #pobjs 
order by nid 
-- 返回第一页 
select top(@pagesize) a.* from order_240 a  with(nolock)  ,#pobjs 
where a.ord_id=ordid 
and a.ord_finished > 0
order by nid 
-- 返回权限 
--select a.* from order_240_authority a with(nolock)  , #pobjs 
--where 
--nid<=@pagesize 
--and orda_cuid=@useruid 
end 
end
if (@key1!='%' and @key2!='%' and @key1!='%')
    begin
if (@pagesize=10000)  -- 下载
begin
select a.* from order_240 a  with(nolock) ,order_240_authority  with(nolock)
where  
(orda_ordafid=ord_authority_factor
and orda_cuid=@useruid) and   
a.ord_cmpid=@cmpid
and (
(ord_no like @key1 and ord_no like @key2 and ord_no like @key3 ) or 
(ord_creator like @key1 and ord_creator like @key2 and ord_creator like @key3 ) ) and 
(ord_date >= @date1 and ord_date <= @date2 )  
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_240 a  with(nolock) ,order_240_authority  with(nolock) 
where  
(orda_ordafid=ord_authority_factor
and orda_cuid=@useruid) and  
a.ord_cmpid=@cmpid
and (
(ord_no like @key1 and ord_no like @key2 and ord_no like @key3 ) or 
(ord_creator like @key1 and ord_creator like @key2 and ord_creator like @key3 ) ) and 
(ord_date >= @date1 and ord_date <= @date2 )  
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_240 a  with(nolock) ,order_240_authority  with(nolock)
where  
(orda_ordafid=ord_authority_factor
and orda_cuid=@useruid) and   
a.ord_cmpid=@cmpid
and (
(ord_no like @key1 and ord_no like @key2 and ord_no like @key3 ) or 
(ord_creator like @key1 and ord_creator like @key2 and ord_creator like @key3 ) ) and 
(ord_date >= @date1 and ord_date <= @date2 )  
and (ord_finished>=1)

-- 返回所有ID
insert into #pobjs(ordid) select top(@amount) ord_id from order_240 a  with(nolock) ,order_240_authority  with(nolock) 
where  
(orda_ordafid=ord_authority_factor
and orda_cuid=@useruid) and  
a.ord_cmpid=@cmpid
and (
(ord_no like @key1 and ord_no like @key2 and ord_no like @key3 ) or 
(ord_creator like @key1 and ord_creator like @key2 and ord_creator like @key3 ) ) and 
(ord_date >= @date1 and ord_date <= @date2 )  
and (ord_finished>=1)
order by ord_date desc
select ordid from #pobjs 
order by nid 
-- 返回第一页 
select top(@pagesize) a.* from order_240 a  with(nolock)  ,#pobjs 
where a.ord_id=ordid 
and a.ord_finished > 0
order by nid 
-- 返回权限 
--select a.* from order_240_authority a with(nolock)  , #pobjs 
--where 
--nid<=@pagesize 
--and orda_cuid=@useruid 
end 
end
    end
drop table #pobjs
set nocount off end