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

declare @mctid int 
set @mctid = @ctid * (-1) 
exec sys_sql_getTableNamePk @cmpid,'cst',@mctid,0,@tbname output,@fkey output ,@dbname output 

declare @sql nvarchar(MAX) 
declare @ss nvarchar(MAX) 

if (dbo.mps_sys_checkkeywords('cst.beforedeleteord2',@ctid,0,'','')<>0) 
begin
set @sql = 'drop procedure '+@tbname+'_'+@ord2+'_deletedetail_before' 
if (dbo.checkprocedure(@tbname+'_'+@ord2+'_deletedetail_before')=0) execute(@sql)  
return 
end

set @sql = N'
-- 生成时间:'+convert(varchar(20),getdate(),120)+N' by ebs-core  
'
if (dbo.checkprocedure(@tbname+'_'+@ord2+'_deletedetail_before')=0) set @sql=@sql+'alter ' else set @sql=@sql+'create '
set @sql = @sql+' procedure '+@tbname+'_'+@ord2+N'_deletedetail_before(
     @cmpid int,
     @branchid int,
     @userdpid int,
     @useruid int,
     @userotid int,
     @jbid bigint,
     @ccid bigint,
     @cc2tag int,
     @cc2id bigint,
     @operation varchar(50) output,
     @errorinfo nvarchar(4000) output
  ) 
as  
begin  
set nocount on  
declare @sys_recid bigint 
set @sys_recid = 0 
 declare @nonedate datetime 
set @nonedate = CONVERT(datetime,''1910-1-1'')
declare @ord2group2 nvarchar(50) 
set @ord2group2 = '''' 
 set @operation = ''continue''
 set @errorinfo = '''' 
 --- 以下是预处理事件中的临时表
 --- #old  在主体中已经生成
 '
 
 if (dbo.mps_sys_checkkeywords('cst.beforedeleteord2',@ctid,0,'#task','')=0) 
 begin
 set @sql = @sql+char(13)+char(10)+
 'exec drop_temp_table ''#task'' '+char(13)+char(10)+
'create table #task(objtype varchar(20),ctgid int,objid bigint,objid2 bigint,cuid int,infotype nvarchar(50),info nvarchar(4000)) '
 end
 
 if (dbo.mps_sys_checkkeywords('cst.beforedeleteord2',@ctid,0,'#master','')=0) 
 begin
 set @sql = @sql+char(13)+char(10)+
 'exec drop_temp_table ''#master'' '+char(13)+char(10)+
'select * into #master from '+@dbname+@tbname+N' where cc_id=@ccid '
 end
 
 if (dbo.mps_sys_checkkeywords('cst.beforedeleteord2',@ctid,0,'#account','')=0) 
 begin
 set @sql = @sql+char(13)+char(10)+
 'exec drop_temp_table ''#account'' '+char(13)+char(10)+
'create table #account(acctype varchar(30),acccategory varchar(30),accdate varchar(30),fiidreal int,fiid int,objidreal bigint,objid bigint,origin int,value int,used int,suspended int,ready int,changed int,available int,actual int) '+char(13)+char(10) 
 end
 
 
 if (dbo.mps_sys_checkkeywords('cst.beforedeleteord2',@ctid,0,'#romaster','')=0) 
 begin
 set @sql = @sql+char(13)+char(10)+
 'exec drop_temp_table ''#romaster'' '+char(13)+char(10)+
'select * into #romaster from '+@dbname+@tbname+N' where cc_id=@ccid '
 end
 
 -- 以下代码是生成子表临时表
 declare c1 cursor LOCAL FORWARD_ONLY READ_ONLY for
select distinct ct2_tag  from mps_csttype2  where ct2_ctid=@ctid 
open c1
FETCH NEXT FROM c1 INTO @ord2tag2 
WHILE (@@FETCH_STATUS = 0)
BEGIN
set @detail = '#detail'+convert(varchar(10),@ord2tag2) 
set @detailold = '#rodetail'+convert(varchar(10),@ord2tag2) 
set @order2 = @dbname+@tbname+'_'+convert(varchar(10),@ord2tag2) 
if (dbo.mps_sys_checkkeywords('cst.beforedeleteord2',@ctid,0,@detailold,'')=0) 
begin
set @sql = @sql+char(13)+char(10)+
'exec drop_temp_table '''+@detailold+''' '+char(13)+char(10)+
'select * into '+@detailold+' from '+@order2+N' where cc2_ccid=@ccid  '+char(13)+char(10) 
end
if (dbo.mps_sys_checkkeywords('cst.beforedeleteord2',@ctid,0,@detail,'')=0) 
begin
 set @sql = @sql+char(13)+char(10)+
'exec drop_temp_table '''+@detail+''' '+char(13)+char(10)+
'select *,null recid into '+@detail+' from '+@order2+N' where cc2_ccid=@ccid  '+char(13)+char(10) 
if (dbo.mps_sys_checkkeywords('cst.beforedeleteord2',@ctid,0,@detail,'insert')=0) 
begin
set @ds1 = @order2 
exec mps_gen_default_code @ds1 , @detail , 'cc2_id' , @dss output 
set @sql = @sql+char(13)+char(10)+ @dss+char(13)+char(10) 
end
if (dbo.mps_sys_checkkeywords('cst.beforedeleteord2',@ctid,0,@detail,'insert,update')=0)
set @sql = @sql+N'update '+@detail+N' set recid=cc2_id '+char(13)+char(10) 

if (dbo.mps_sys_checkkeywords('cst.beforedeleteord2',@ctid,0,@detail,'delete')=0)
set @sql = @sql+N'declare @detail'+convert(varchar(10),@ord2tag2)+'_old  table (cc2_id bigint) '+char(13)+char(10)+
'insert  into @detail'+convert(varchar(10),@ord2tag2)+'_old (cc2_id) select cc2_id from '+@detail +char(13)+char(10)
 end
 FETCH NEXT FROM c1 INTO @ord2tag2 
END
CLOSE c1
DEALLOCATE c1 

set @sql=@sql+N'
 ---- 以下是开发人员写的 before delete details order 事件 --------------
 '
 + dbo.mps_sys_getusercode('cst.beforedeleteord2',@ctid,0) +N'
 ---- 以上是开发人员写的 before delete details order 事件 --------------
 
 if (@operation=''continue'') 
 begin
 set @nonedate =  @nonedate  -- here useless 
 ' 
 set @s1 = @dbname+@tbname 
 if (dbo.mps_sys_checkkeywords('cst.beforedeleteord2',@ctid,0,'#master','update')=0) 
 begin
 -- 回写主表
 exec mps_sys_gen_updatestatement @s1,'old','new','cc_id,cc_ctid,cc_cmpid,cc_updated,cc_bcid,cc_groupkey,cc_finished,cc_date',@ss output 
 if (len(@ss)>10) 
 set @sql = @sql+char(13)+char(10)+
'update old set '+char(13)+char(10)+@ss + char(13)+char(10)+
'from '+@dbname+@tbname+N' old , #master new  where old.cc_id=new.cc_id ' +char(13)+char(10) 
 end
 
 -- 回写明细表
 declare c1 cursor LOCAL FORWARD_ONLY READ_ONLY for
select distinct ct2_tag  from mps_csttype2  where ct2_ctid=@ctid 
open c1
FETCH NEXT FROM c1 INTO @ord2tag2 
WHILE (@@FETCH_STATUS = 0)
BEGIN
set @detail = '#detail'+convert(varchar(10),@ord2tag2) 
set @order2 = @dbname+@tbname+'_'+convert(varchar(10),@ord2tag2) 
if (dbo.mps_sys_checkkeywords('cst.beforedeleteord2',@ctid,0,@detail,'insert')=0) 
begin
set @sql = @sql+char(13)+char(10)+
' set @ord2group2='''' '+char(13)+char(10)+
' select top 1 @ord2group2=cc2_group from '+@detail+N' where cc2_group>'''' '+char(13)+char(10)+
' set @ord2group2 = isnull(@ord2group2 , '''') '+char(13)+char(10) 
set @ds1=@order2  
 exec mps_gen_insert_code @ds1,'','cc2_id,cc2_date,cc2_updated,cc2_tag,cc2_group,cc2_ccid',@dss output 
 if (len(@dss)>5) 
 begin
 set @sql = @sql+char(13)+char(10)+
' insert into '+@ds1+'(cc2_date,cc2_updated,cc2_tag,cc2_group,cc2_ccid,'+@dss+') '+char(13)+char(10)+
' select case when new.cc2_date>getdate()-40000 then new.cc2_date else getdate() end ,getdate(),'+
 convert(varchar(10),@ord2tag2)+','+char(13)+char(10)+
'    case when new.cc2_group>'''' then new.cc2_group else @ord2group2 end , @ccid,'
 exec mps_gen_insert_code @ds1,'new','cc2_id,cc2_date,cc2_updated,cc2_tag,cc2_group,cc2_ccid',@dss output 
 set @sql = @sql+@dss+char(13)+char(10)+
' from '+@detail+' new where recid is null '+char(13)+char(10) 
end 
end 


if (dbo.mps_sys_checkkeywords('cst.beforedeleteord2',@ctid,0,@detail,'update')=0) 
begin
  exec mps_sys_gen_updatestatement @order2,'old','new','cc2_id,cc2_ccid,cc2_tag,cc2_updated,cc2_date',@ss output 
  if (len(@ss)>10) 
set @sql = @sql+char(13)+char(10)+
'update old set '+char(13)+char(10)+@ss+char(13)+char(10)+
'from '+@order2+' old , '+@detail+N' new  '+char(13)+char(10)+
'where old.cc2_id=new.cc2_id and (new.recid is not null)  ' +char(13)+char(10) 
end 

if (dbo.mps_sys_checkkeywords('cst.beforedeleteord2',@ctid,0,@detail,'delete')=0) 
set @sql = @sql+char(13)+char(10)+
'delete '+@order2+char(13)+char(10)+
'from  (select cc2_id from @detail'+CONVERT(varchar(10),@ord2tag2)+'_old  where cc2_id not in ( '+char(13)+char(10)+
'select recid from '+@detail+')) a where '+@order2+'.cc2_id=a.cc2_id '+char(13)+char(10) 
 
 FETCH NEXT FROM c1 INTO @ord2tag2 
END
CLOSE c1
DEALLOCATE c1

set @sql = @sql +N'
end 
--- 清除临时表 
'

 if (dbo.mps_sys_checkkeywords('cst.beforedeleteord2',@ctid,0,'#master','')=0) 
 set @sql = @sql+N'exec drop_temp_table ''#master'' '+char(13)+char(10) 
 
 if (dbo.mps_sys_checkkeywords('cst.beforedeleteord2',@ctid,0,'#account','')=0) 
 set @sql = @sql+N'exec drop_temp_table ''#account'' '+char(13)+char(10) 
 
 if (dbo.mps_sys_checkkeywords('cst.beforedeleteord2',@ctid,0,'#romaster','')=0) 
 set @sql = @sql+N'exec drop_temp_table ''#romaster'' '+char(13)+char(10) 
 
 declare c1 cursor LOCAL FORWARD_ONLY READ_ONLY for
select distinct ct2_tag  from mps_csttype2  where ct2_ctid=@ctid 
open c1
FETCH NEXT FROM c1 INTO @ord2tag2 
WHILE (@@FETCH_STATUS = 0)
BEGIN
set @detail = '#detail'+convert(varchar(10),@ord2tag2) 
set @detailold = '#rodetail'+convert(varchar(10),@ord2tag2) 
if (dbo.mps_sys_checkkeywords('cst.beforedeleteord2',@ctid,0,@detail,'')=0) 
set @sql = @sql+N'exec drop_temp_table '''+@detail+''' '+char(13)+char(10) 
if (dbo.mps_sys_checkkeywords('cst.beforedeleteord2',@ctid,0,@detailold,'')=0) 
set @sql = @sql+N'exec drop_temp_table '''+@detailold+''' '+char(13)+char(10) 
 FETCH NEXT FROM c1 INTO @ord2tag2 
END
CLOSE c1
DEALLOCATE c1 

if (dbo.mps_sys_checkkeywords('cst.beforedeleteord2',@ctid,0,'#task','')=0) 
 begin
 set @sql = @sql+char(13)+char(10)+
 ' delete #task where cuid is null or info is null or objid is null  or ctgid is null '+
 ' insert into mps_task(tsk_cmpid,tsk_bcid,tsk_branch,tsk_dpid,tsk_depart,tsk_cuid,tsk_cuname,tsk_date,tsk_type,tsk_objtype,tsk_ctgid,tsk_objid,tsk_objid2,tsk_message) '+char(13)+char(10)+
 ' select distinct cu_cmpid,cu_bcid,cu_branch,cu_dpid,cu_depart,cu_id,cu_callname,getdate(),'+char(13)+char(10)+
 'case when infotype is null then ''普通任务'' else infotype end , '+char(13)+char(10)+
 'case when objtype is null or objtype='''' then ''cst'' else objtype end , '+char(13)+char(10)+
 'case when ctgid is null or ctgid=0 then '+convert(varchar(10),@ctid)+' else ctgid end , '+char(13)+char(10)+
 'case when objid  is null or objid=0 then @ccid else objid end , '+char(13)+char(10)+
 'case when objid2  is null then 0 else objid2 end , '+char(13)+char(10)+
 'case when info  is null then '''' else info end '+char(13)+char(10)+
 'from #task , mps_login where cu_id=cuid '+char(13)+char(10)+
 'exec drop_temp_table ''#task'' '+char(13)+char(10) 
 end
 
set @sql = @sql +N'
set nocount off 
end
'
--exec myprint @sql 
execute(@sql)
set nocount off 
end