IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_delete_order]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_delete_order]
GO
CREATE procedure [dbo].[mps_delete_order](
@cmpid int ,
@useruid int,
@bizid int,
@ordid bigint,
@reason nvarchar(1000)=default 
)
as
begin

SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
declare @db varchar(50)
declare @tbid int
select @db=biz_database , @tbid=biz_table from   mps_biztype with(nolock)where biz_id=@bizid 
if (@tbid>0) select @db=biz_database , @tbid=biz_id from   mps_biztype with(nolock)where biz_id=@tbid 
if (@db>'') set @db=@db+'.dbo.' ;
if (@tbid=0) set @db=@db+'order_'+ltrim(rtrim(str(@bizid))) else set @db=@db+'order_'+ltrim(rtrim(str(@tbid))) 

declare @sql nvarchar(1000) 

set @sql = '
insert into mps_system_operation_log(op_cmpid,op_operation,op_objtype,op_objid,op_objid2,op_tid,
op_tcaption,op_reason,op_useruid,op_branch,op_bcid,op_depart,op_dpid,op_name) 
select @cmpid,''delete'',''biz'',@bizid,0,@ordid,ord_no+''/''+ord_title,@reason,
@useruid,cu_branch, cu_bcid,cu_depart,cu_dpid,cu_callname 
from '+@db+'  with(nolock), mps_login  with(nolock)where ord_id=@ordid and cu_id=@useruid ' 

exec sp_executesql @sql, N'@cmpid int ,@bizid int ,@ordid bigint , @useruid int,@reason nvarchar(1000) ', 
@cmpid,@bizid,@ordid,@useruid,@reason 

set @sql = 'delete '+@db+'  with(rowlock)where ord_id='+ltrim(rtrim(str(@ordid))) 
execute(@sql) 

declare status1 cursor LOCAL FORWARD_ONLY READ_ONLY for
select biz2_tag  from mps_biztype2   with(nolock)where biz2_bizid=@bizid 
open status1
declare @ord2tag int 
FETCH NEXT FROM status1 INTO @ord2tag 
WHILE (@@FETCH_STATUS = 0)
BEGIN
 set @sql = 'delete '+@db+'_'+rtrim(ltrim(str(@ord2tag)))+' with(rowlock) where ord2_ordid='+ ltrim(rtrim(str(@ordid))) 
exec(@sql)
 FETCH NEXT FROM status1 INTO @ord2tag 
END
CLOSE status1
DEALLOCATE status1 
set @sql = 'delete '+@db+'_0  with(rowlock)where ord3_ordid='+ltrim(rtrim(str(@ordid))) 
execute(@sql) 

set @sql = 'delete '+@db+'_relation  with(rowlock)where ordr_ordid='+ltrim(rtrim(str(@ordid))) 
execute(@sql) 

--set @sql = 'delete '+@db+'_users where ordp_ordid='+ltrim(rtrim(str(@ordid))) 
--execute(@sql) 

update mps_account_operation  with(rowlock) set accop_back=0 
from mps_job with(nolock)
where accop_jbid=jb_id and jb_crmid=@bizid and jb_ordid=@ordid and jb_wsid=0  and accop_back=1

update mps_warehouse_operation   with(rowlock)set whop_back=0 
from mps_job with(nolock)
where whop_jbid=jb_id and jb_crmid=@bizid and jb_ordid=@ordid and jb_wsid=0  and whop_back=1 

delete mps_job_users   with(rowlock)
from mps_job  with(nolock)where jb_crmid=@bizid and jb_ordid=@ordid and jp_jbid=jb_id 

delete mps_job_task   with(rowlock)from mps_job  with(nolock)
where jb_crmid=@bizid and jb_ordid=@ordid and 
wt_cmpid=@cmpid and wt_bizid=@bizid and wt_ordid=@ordid and wt_jbid=jb_id and wt_type='wrkalarm'

delete mps_job_available   with(rowlock)where jbn_crmid=@bizid and jbn_ordid=@ordid 

delete mps_job   with(rowlock)where jb_crmid=@bizid and jb_ordid=@ordid 

delete mps_task   with(rowlock)where tsk_objtype='biz' and tsk_ctgid=@bizid and tsk_objid=@ordid 

delete mps_job_task  with(rowlock) where wt_cmpid=@cmpid and wt_bizid=@bizid and wt_ordid=@ordid and wt_type='bizalarm'

delete mps_job_task   with(rowlock)where wt_cmpid=@cmpid and wt_bizid=@bizid and wt_ordid=@ordid and wt_type='job'

delete mps_alarm_message  with(rowlock) where almmsg_objtype='biz' and almmsg_objid=@ordid 
end