IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[report_6_data]') AND type = 'P')
DROP PROCEDURE [dbo].[report_6_data]
GO
CREATE PROCEDURE report_6_data
@cmpid int,@branchid int,@userdpid int,@userotid int,@useruid int,@tableid varchar(8000),@objids varchar(8000),@pagesize int,@qry_drsj1 varchar(512)
,@qry_drsj2 varchar(512)
,@qry_fgs varchar(512)
,@qry_cjr 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)

create table #temp (
     tableid varchar(100),
     ord_id bigint,
     ord_no varchar(100),
     ord_branch varchar(100),
     ord_creator varchar(100),
     ord_date datetime,
     ord_hdmc varchar(100),
     ord_zs int DEFAULT 0,
     ord_ysy int DEFAULT 0,
     ord_jxz int DEFAULT 0,
     ord_wsy int DEFAULT 0
 )
 
insert into #temp (tableid,ord_id,ord_date,ord_no,ord_branch,ord_creator,ord_hdmc)
select 'tb_report1',ord_id,ord_date,ord_no,ord_branch,ord_creator,ord_yxhd  
from order_241 
where ord_date >= @qry_drsj1 + ' 00:00:00' and  ord_date <= @qry_drsj2 + ' 23:59:59'
and (ord_branch = @qry_fgs or @qry_fgs = '')
and (ord_creator = @qry_cjr or @qry_cjr = '')
and ord_status != '已作废' and charindex('临',ord_no) = 0

update #temp set ord_zs = (select COUNT(*) from order_241_2 where ord2_ordid = ord_id  )
update #temp set ord_ysy = (select COUNT(a.ord2_id) from order_241_2 a inner join order_239 b on  a.ord2_whordid = b.ord_id
                            where a.ord2_ordid = #temp.ord_id and a.ord2_sfbd = '是' and b.ord_status = '已完成')
update #temp set ord_jxz = (select COUNT(a.ord2_id) from order_241_2 a inner join order_239 b on  a.ord2_whordid = b.ord_id
                            where a.ord2_ordid = #temp.ord_id and a.ord2_sfbd = '是' and b.ord_status != '已完成')
update #temp set ord_wsy = (select COUNT(*) from order_241_2 where ord2_ordid = ord_id  and ord2_sfbd = '否')

select * from #temp where ( ord_zs > 0 or ord_ysy > 0 or ord_jxz > 0 or ord_wsy > 0)
END