IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[order_85_query]') AND type = 'P')
DROP PROCEDURE [dbo].[order_85_query]
GO

-- 生成时间:2017-12-11 10:00:48 by ebs-core  
CREATE  procedure order_85_query(
@cmpid int , 
@bizid int , 
@useruid int ,  
@pagesize int , 
@pagecount int , 
@finished int , 
@date1 datetime, 
@date2 datetime, 
@key1 nvarchar(50) , 
@key2 nvarchar(50) , 
@key3 nvarchar(50) , 
@labels varchar(2000), 
@pageobjids varchar(8000) , 
@groupkeys varchar(8000)
) 
as  
begin  
set nocount on 
set nocount on 
declare @amount int, @nonumber int,@grpkeylen int  
declare @s1 varchar(8000) 
declare @s2 varchar(100) 
declare @i int  
declare @searchcnd nvarchar(4000)
set @searchcnd = '' 
select @amount = @pagecount*@pagesize, @nonumber = 0 
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 * from order_85 a , @pobjs where ord_id=ordid order by nid 
return 
end 
declare @sys_temp_d table(groupkey varchar(100))  
--分组数据保存到临时表  
set @s1 = @groupkeys 
set @grpkeylen = len(@groupkeys) 
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  
  if (len(@s2)<100)  
    insert into @sys_temp_d(groupkey) values(@s2) 
end 
declare  @ljionname varchar(100) 
  ,@ljoinfld varchar(100) 
  ,@tablePrefix varchar(100) 
  ,@jionclause varchar(2000) 
  ,@labelClauseLen int 

select @ljionname='a' ,@ljoinfld='ord_id'     ,@tablePrefix='order_85'

exec sys_qry_decomlabels@labels ,@ljionname ,@ljoinfld ,@tablePrefix,@jionclause  output 
set @labelClauseLen = len(@jionclause) 

if (@key1 is not null and len(@key1)<1) set @key1= null 
if (@key2 is not null and len(@key2)<1) set @key2= null 
if (@key3 is not null and len(@key3)<1) set @key3= null
declare @keycond varchar(2000) 
set @keycond='' 

declare @sqlData nvarchar(4000) ,@sqlID nvarchar(4000)  ,@sqlCount nvarchar(4000),@vardef nvarchar(1000) ,@condStr nvarchar(4000) 
select @vardef='@date1 datetime,@date2 datetime,@useruid1 int' ,@condStr='',@sqlData='', @sqlCount='',@sqlID='' 
    select  @sqlData='select top ' + ltrim(rtrim(str(@pagesize))) + ' a.* from  order_85 a INNER JOIN order_85_users ON ord_id = ordp_ordid and ordp_cuid=@useruid1 and ordp_type=''bizview'' ' +char(13)  
           ,@sqlID='select top ' + ltrim(rtrim(str(@amount))) + ' ord_id from  order_85 a INNER JOIN order_85_users ON ord_id = ordp_ordid and ordp_cuid=@useruid1 and ordp_type=''bizview'' ' +char(13)  
           ,@sqlCount='select count(1) cnt  from  order_85 a INNER JOIN order_85_users ON ord_id = ordp_ordid and ordp_cuid=@useruid1 and ordp_type=''bizview'' ' +char(13)  

if (@grpkeylen>2) select  @condStr= @condStr + char(13) +  ' left join  @sys_temp_d on a.ord_groupkey=groupkey' 

if (@labelClauseLen>2) select @condStr=@condStr + char(13) + @jionclause 
select @condStr = @condStr + char(13) +  '  where ord_date >= @date1 and ord_date <= @date2  ' 
if (@key1 is not null and @searchcnd>'') 
select @condStr = @condStr + ' and  ',@vardef = @vardef + ',@key1 nvarchar(50)'
if (@key2 is not null and @searchcnd>'')
select @condStr = @condStr + ' and  ',@vardef = @vardef + ',@key2 nvarchar(50)'
if (@key3 is not null and @searchcnd>'')
select @condStr = @condStr + ' and  ',@vardef = @vardef + ',@key3 nvarchar(50)'

    select  @sqlData=@sqlData +char(13) + @condStr  ,@sqlID=@sqlID + char(13) + @condStr ,@sqlCount=@sqlCount + char(13) + @condStr 
if (@key3 is not null) 
begin 
      EXECUTE sp_executesql @sqlCount,@vardef,@date1,@date2,@useruid,@key1,@key2,@key3  
      EXECUTE sp_executesql @sqlID,@vardef,@date1,@date2,@useruid,@key1,@key2,@key3  
      EXECUTE sp_executesql @sqlData,@vardef,@date1,@date2,@useruid,@key1,@key2,@key3  
end 
else if (@key2 is not null) 
begin 
      EXECUTE sp_executesql @sqlCount,@vardef,@date1,@date2,@useruid,@key1,@key2  
      EXECUTE sp_executesql @sqlID,@vardef,@date1,@date2,@useruid,@key1,@key2 
      EXECUTE sp_executesql @sqlData,@vardef,@date1,@date2,@useruid,@key1,@key2  
end 
else if (@key1 is not null) 
begin 
      EXECUTE sp_executesql @sqlCount,@vardef,@date1,@date2,@useruid,@key1  
      EXECUTE sp_executesql @sqlID,@vardef,@date1,@date2,@useruid,@key1  
      EXECUTE sp_executesql @sqlData,@vardef,@date1,@date2,@useruid,@key1  
end 
else  
begin 
      EXECUTE sp_executesql  @sqlCount,@vardef,@date1,@date2,@useruid  
      EXECUTE sp_executesql  @sqlID,@vardef,@date1,@date2  ,@useruid 
      EXECUTE sp_executesql  @sqlData,@vardef,@date1,@date2 ,@useruid 
end  
set nocount off  
end