IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_gen_biz_deletedetail]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_gen_biz_deletedetail]
GO
create procedure [dbo].[mps_gen_biz_deletedetail](
@cmpid int,
@bizid int,
@ord2tag int 
)
as
begin
set nocount on
declare @s1 nvarchar(100) 
declare @detail varchar(100)
declare @ds1 varchar(100)
declare @dss nvarchar(MAX) 
declare @order2 varchar(100)
declare @tbname varchar(50)
declare @dbname varchar(50)
declare @fkey   varchar(50) 
declare @ord2 varchar(10) 
set @ord2 = convert(varchar(10),@ord2tag) 

exec sys_sql_getTableNamePk @cmpid,'biz',@bizid,0,@tbname output,@fkey output ,@dbname output 
declare @ss nvarchar(MAX) 
declare @sql nvarchar(MAX) 
declare @procparam nvarchar(MAX) 
set @sql = N'
-- 生成时间:'+convert(varchar(20),getdate(),120)+N' by ebs-core  
'
if (dbo.checkprocedure(@tbname+'_'+@ord2+'_deletedetail')=0) set @sql=@sql+'alter ' else set @sql=@sql+'create '
set @sql = @sql+' procedure '+@tbname+N'_'+@ord2+'_deletedetail'+N'(
@cmpid int, 
@branchid int, 
@userdpid int, 
@useruid int, 
@userotid int, 
@jbid bigint, 
@ordid bigint, 
@ord2tag int,
@ord2id bigint
) 
as  
begin  
set nocount on 
declare @errorinfo nvarchar(4000) 
declare @operation varchar(50) 
set @operation = ''continue'' 
set @errorinfo = '''' 
'
if (dbo.mps_sys_checkkeywords('crm.beforedeleteord2,crm.afterdeleteord2',@bizid ,0,'#old','')=0) 
begin
set @sql = @sql +N'exec drop_temp_table ''#old'' '+char(13)+char(10)+
'select * into #old from '+@dbname+@tbname+'_'+@ord2+' where ord2_id=@ord2id '+char(13)+char(10) 
end 
if (dbo.mps_sys_checkkeywords('crm.beforedeleteord2,crm.afterdeleteord2',@bizid ,0,'#new','')=0) 
begin
set @sql = @sql +N'exec drop_temp_table ''#new'' '+char(13)+char(10)+
'select * ,'''' sourcefield into #new from '+@dbname+@tbname+'_'+@ord2+' where ord2_id=@ord2id '+char(13)+char(10) 
end 
if (dbo.mps_sys_checkkeywords('crm.beforedeleteord2',@bizid ,0,'','')=0) 
begin
set @sql = @sql +N'exec '+@tbname+'_'+@ord2+'_deletedetail_before @cmpid ,@branchid ,@userdpid,@useruid,@userotid,@jbid,@ordid,@ord2tag,@ord2id,@operation output,@errorinfo output '+char(13)+char(10) 
  end 
  set @sql = @sql +N'
if (@operation <> ''continue'') 
begin 
-- 放弃删除
if (@errorinfo = '''') set @errorinfo = ''error'' 
select @operation operation, @errorinfo errorinfo 
end else 
begin 
if ('+@ord2+'>100) 
begin
delete mps_task where tsk_cmpid=@cmpid and tsk_objtype=''biz'' and tsk_ctgid='+convert(varchar(10),@bizid)+' and tsk_objid=@ordid and tsk_objid2=@ord2id 
end
delete '+@dbname+@tbname+'_'+@ord2+' where ord2_id = @ord2id 
'
if (dbo.mps_sys_checkkeywords('crm.afterdeleteord2',@bizid ,0,'','')=0) 
set @sql = @sql +'exec '+@tbname+N'_'+@ord2+'_deletedetail_after  @cmpid ,@branchid ,@userdpid,@useruid,@userotid,@jbid,@ordid,@ord2tag,@ord2id '  
set @sql = @sql +N'
select ''ok'' operation, '''' errorinfo 
end
'
if (dbo.mps_sys_checkkeywords('crm.beforedeleteord2,crm.afterdeleteord2',@bizid ,0,'#old','')=0) 
begin
set @sql = @sql +N'exec drop_temp_table ''#old'' '+char(13)+char(10) 
end 
if (dbo.mps_sys_checkkeywords('crm.beforedeleteord2,crm.afterdeleteord2',@bizid ,0,'#new','')=0) 
begin
set @sql = @sql +N'exec drop_temp_table ''#new'' '+char(13)+char(10) 
end 
set @sql = @sql +N'
set nocount off
end '
--exec myprint @sql 
execute(@sql)  
-- 生成mps_sql 
set @sql = ' exec mps_sys_gen_mpssql '+CONVERT(varchar(10),@cmpid)+','''+@tbname+N'_'+@ord2+'_deletedetail'',
''@cmpid int,@branchid int,@userdpid int,@useruid int,@userotid int,@jbid bigint,'+
     '@ordid bigint,@ord2tag int,@ord2id bigint''' 
execute(@sql)
set nocount off 
end