IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[report_7_data]') AND type = 'P')
DROP PROCEDURE [dbo].[report_7_data]
GO
CREATE PROCEDURE report_7_data
@cmpid int,@branchid int,@userdpid int,@userotid int,@useruid int,@tableid varchar(8000),@objids varchar(8000),@pagesize int,@qry_fgs varchar(512)
,@qry_mdbh varchar(512)
,@qry_ygxm varchar(512)
,@qry_sjlx varchar(512)
,@qry_date1 varchar(512)
,@qry_date2 varchar(512)
,@qry_whzt varchar(512)
,@qry_sfcj varchar(512)
,@qry_ddzt varchar(512)
,@qry_hdlx 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_239 where ord_status != '已作废' and charindex('临',ord_no) = 0
and (@qry_fgs = '' or ord_branch = @qry_fgs)
and (@qry_mdbh = '' or ord_mdbh = @qry_mdbh)
and (@qry_ygxm = '' or ord_creator like '%' + @qry_ygxm + '%')
and ((@qry_sjlx = '建单时间' and ord_date >= @qry_date1 + ' 00:00:00' and ord_date <= @qry_date2 + ' 23:59:59')
or (@qry_sjlx = '外呼时间' and ord_whsj >= @qry_date1 + ' 00:00:00' and ord_whsj <= @qry_date2 + ' 23:59:59')
or (@qry_sjlx = '成交时间' and ord_cjsj >= @qry_date1 + ' 00:00:00' and ord_cjsj <= @qry_date2 + ' 23:59:59'))
and (@qry_whzt = '全部' or ord_whzt = @qry_whzt) 
and (@qry_sfcj = '全部' or ord_sfcj = @qry_sfcj) 
and (@qry_ddzt = '全部' or ord_ddzt = @qry_ddzt) 
and (@qry_hdlx = '全部' or ord_hdlx = @qry_hdlx) 

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

end
--select 'tb_report1' tableid, * from order_239 where ord_status != '已作废' and charindex('临',ord_no) = 0



select count(*) as dataCount from 
order_239 where ord_status != '已作废' and charindex('临',ord_no) = 0
and (@qry_fgs = '' or ord_branch = @qry_fgs)
and (@qry_mdbh = '' or ord_mdbh = @qry_mdbh)
and (@qry_ygxm = '' or ord_creator like '%' + @qry_ygxm + '%')
and ((@qry_sjlx = '建单时间' and ord_date >= @qry_date1 + ' 00:00:00' and ord_date <= @qry_date2 + ' 23:59:59')
or (@qry_sjlx = '外呼时间' and ord_whsj >= @qry_date1 + ' 00:00:00' and ord_whsj <= @qry_date2 + ' 23:59:59')
or (@qry_sjlx = '成交时间' and ord_cjsj >= @qry_date1 + ' 00:00:00' and ord_cjsj <= @qry_date2 + ' 23:59:59'))
and (@qry_whzt = '全部' or ord_whzt = @qry_whzt) 
and (@qry_sfcj = '全部' or ord_sfcj = @qry_sfcj) 
and (@qry_ddzt = '全部' or ord_ddzt = @qry_ddzt) 
and (@qry_hdlx = '全部' or ord_hdlx = @qry_hdlx) 

select top 99990 ord_id as dataIds from 
order_239 where ord_status != '已作废' and charindex('临',ord_no) = 0
and (@qry_fgs = '' or ord_branch = @qry_fgs)
and (@qry_mdbh = '' or ord_mdbh = @qry_mdbh)
and (@qry_ygxm = '' or ord_creator like '%' + @qry_ygxm + '%')
and ((@qry_sjlx = '建单时间' and ord_date >= @qry_date1 + ' 00:00:00' and ord_date <= @qry_date2 + ' 23:59:59')
or (@qry_sjlx = '外呼时间' and ord_whsj >= @qry_date1 + ' 00:00:00' and ord_whsj <= @qry_date2 + ' 23:59:59')
or (@qry_sjlx = '成交时间' and ord_cjsj >= @qry_date1 + ' 00:00:00' and ord_cjsj <= @qry_date2 + ' 23:59:59'))
and (@qry_whzt = '全部' or ord_whzt = @qry_whzt) 
and (@qry_sfcj = '全部' or ord_sfcj = @qry_sfcj) 
and (@qry_ddzt = '全部' or ord_ddzt = @qry_ddzt) 
and (@qry_hdlx = '全部' or ord_hdlx = @qry_hdlx) 
 order by ord_id
select top 10 'tb_report1' tableid ,* from 
order_239 where ord_status != '已作废' and charindex('临',ord_no) = 0
and (@qry_fgs = '' or ord_branch = @qry_fgs)
and (@qry_mdbh = '' or ord_mdbh = @qry_mdbh)
and (@qry_ygxm = '' or ord_creator like '%' + @qry_ygxm + '%')
and ((@qry_sjlx = '建单时间' and ord_date >= @qry_date1 + ' 00:00:00' and ord_date <= @qry_date2 + ' 23:59:59')
or (@qry_sjlx = '外呼时间' and ord_whsj >= @qry_date1 + ' 00:00:00' and ord_whsj <= @qry_date2 + ' 23:59:59')
or (@qry_sjlx = '成交时间' and ord_cjsj >= @qry_date1 + ' 00:00:00' and ord_cjsj <= @qry_date2 + ' 23:59:59'))
and (@qry_whzt = '全部' or ord_whzt = @qry_whzt) 
and (@qry_sfcj = '全部' or ord_sfcj = @qry_sfcj) 
and (@qry_ddzt = '全部' or ord_ddzt = @qry_ddzt) 
and (@qry_hdlx = '全部' or ord_hdlx = @qry_hdlx) 
 order by ord_id

END