IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[report_39_data]') AND type = 'P')
DROP PROCEDURE [dbo].[report_39_data]
GO
CREATE PROCEDURE report_39_data
@cmpid int,@branchid int,@userdpid int,@userotid int,@useruid int,@tableid varchar(8000),@objids varchar(8000),@pagesize int,@qry_fgs varchar(512)
,@qry_creator varchar(512)
,@qry_date1 varchar(512)
,@qry_date2 varchar(512)
,@sffprw varchar(512)
,@rwwcqk varchar(512)

AS
BEGIN
 set nocount on 

declare @dataIds table(objid int)
if @objids!='_NONE' and @objids!='_none_' and @objids!='' 
insert into @dataIds(objid)
select value from dbo.fn_split_int(@objids)
if @tableid='tb_report1'
begin
    if @pagesize=10000
    begin
       select * from 
order_247 where ord_status != '已作废' and charindex('临',ord_no) = 0
and (@qry_fgs = '' or ord_xzfgs = @qry_fgs)
and (@qry_creator = '' or ord_creator = @qry_creator)
and (ord_date >= @qry_date1 + ' 00:00:00' and ord_date <= @qry_date2 + ' 23:59:59')
and (@sffprw = ''
    or (@sffprw = '否' and ord_id not in (select distinct f.ord_id from order_243 s,order_247 f 
        where s.ord_fordid = f.ord_id and s.ord_fbizid = 247 ))
    or (@sffprw = '是' and @rwwcqk = '' and ord_id in (select distinct f.ord_id from order_243 s,order_247 f 
            where s.ord_fordid = f.ord_id and s.ord_fbizid = 247 and s.ord_status != '已作废')
         )
    or (@sffprw = '是' and @rwwcqk = '已完成' and ord_id in (select distinct f.ord_id from order_243 s,order_247 f 
            where s.ord_fordid = f.ord_id and s.ord_fbizid = 247 and s.ord_status = '已完成')
         )
    or (@sffprw = '是' and @rwwcqk = '未完成' and ord_id in (select distinct f.ord_id from order_243 s,order_247 f 
            where s.ord_fordid = f.ord_id and s.ord_fbizid = 247 and s.ord_status != '已作废' and s.ord_status != '已完成')
         )
    )

       return
    end
    if @objids!='_none_'
    begin
       select * from @dataIds,order_247 where objid=ord_id
       return
    end

end



select count(*) as dataCount from 
order_247 where ord_status != '已作废' and charindex('临',ord_no) = 0
and (@qry_fgs = '' or ord_xzfgs = @qry_fgs)
and (@qry_creator = '' or ord_creator = @qry_creator)
and (ord_date >= @qry_date1 + ' 00:00:00' and ord_date <= @qry_date2 + ' 23:59:59')
and (@sffprw = ''
    or (@sffprw = '否' and ord_id not in (select distinct f.ord_id from order_243 s,order_247 f 
        where s.ord_fordid = f.ord_id and s.ord_fbizid = 247 ))
    or (@sffprw = '是' and @rwwcqk = '' and ord_id in (select distinct f.ord_id from order_243 s,order_247 f 
            where s.ord_fordid = f.ord_id and s.ord_fbizid = 247 and s.ord_status != '已作废')
         )
    or (@sffprw = '是' and @rwwcqk = '已完成' and ord_id in (select distinct f.ord_id from order_243 s,order_247 f 
            where s.ord_fordid = f.ord_id and s.ord_fbizid = 247 and s.ord_status = '已完成')
         )
    or (@sffprw = '是' and @rwwcqk = '未完成' and ord_id in (select distinct f.ord_id from order_243 s,order_247 f 
            where s.ord_fordid = f.ord_id and s.ord_fbizid = 247 and s.ord_status != '已作废' and s.ord_status != '已完成')
         )
    )

select top 99990 ord_id as dataIds from 
order_247 where ord_status != '已作废' and charindex('临',ord_no) = 0
and (@qry_fgs = '' or ord_xzfgs = @qry_fgs)
and (@qry_creator = '' or ord_creator = @qry_creator)
and (ord_date >= @qry_date1 + ' 00:00:00' and ord_date <= @qry_date2 + ' 23:59:59')
and (@sffprw = ''
    or (@sffprw = '否' and ord_id not in (select distinct f.ord_id from order_243 s,order_247 f 
        where s.ord_fordid = f.ord_id and s.ord_fbizid = 247 ))
    or (@sffprw = '是' and @rwwcqk = '' and ord_id in (select distinct f.ord_id from order_243 s,order_247 f 
            where s.ord_fordid = f.ord_id and s.ord_fbizid = 247 and s.ord_status != '已作废')
         )
    or (@sffprw = '是' and @rwwcqk = '已完成' and ord_id in (select distinct f.ord_id from order_243 s,order_247 f 
            where s.ord_fordid = f.ord_id and s.ord_fbizid = 247 and s.ord_status = '已完成')
         )
    or (@sffprw = '是' and @rwwcqk = '未完成' and ord_id in (select distinct f.ord_id from order_243 s,order_247 f 
            where s.ord_fordid = f.ord_id and s.ord_fbizid = 247 and s.ord_status != '已作废' and s.ord_status != '已完成')
         )
    )
 order by ord_id
select top 10 'tb_report1' tableid ,* from 
order_247 where ord_status != '已作废' and charindex('临',ord_no) = 0
and (@qry_fgs = '' or ord_xzfgs = @qry_fgs)
and (@qry_creator = '' or ord_creator = @qry_creator)
and (ord_date >= @qry_date1 + ' 00:00:00' and ord_date <= @qry_date2 + ' 23:59:59')
and (@sffprw = ''
    or (@sffprw = '否' and ord_id not in (select distinct f.ord_id from order_243 s,order_247 f 
        where s.ord_fordid = f.ord_id and s.ord_fbizid = 247 ))
    or (@sffprw = '是' and @rwwcqk = '' and ord_id in (select distinct f.ord_id from order_243 s,order_247 f 
            where s.ord_fordid = f.ord_id and s.ord_fbizid = 247 and s.ord_status != '已作废')
         )
    or (@sffprw = '是' and @rwwcqk = '已完成' and ord_id in (select distinct f.ord_id from order_243 s,order_247 f 
            where s.ord_fordid = f.ord_id and s.ord_fbizid = 247 and s.ord_status = '已完成')
         )
    or (@sffprw = '是' and @rwwcqk = '未完成' and ord_id in (select distinct f.ord_id from order_243 s,order_247 f 
            where s.ord_fordid = f.ord_id and s.ord_fbizid = 247 and s.ord_status != '已作废' and s.ord_status != '已完成')
         )
    )
 order by ord_id


END