IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_gen_cstmodify]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_gen_cstmodify]
GO
create procedure [dbo].[mps_gen_cstmodify](
@cmpid int,
@ctid 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 @mctid int 
set @mctid = @ctid * (-1) 
exec sys_sql_getTableNamePk @cmpid,'cst',@mctid,0,@tbname output,@fkey output ,@dbname output 
declare @ss nvarchar(MAX) 
declare @sql nvarchar(MAX) 
declare @ord2tag2 int 
declare @procparam nvarchar(MAX) 
declare @ss2 nvarchar(MAX) 


set @sql = N'
-- 生成时间:'+convert(varchar(20),getdate(),120)+N' by ebs-core  
'
if (dbo.checkprocedure(@tbname+'_modify_'+convert(varchar(50),@ctid))=0) set @sql=@sql+'alter ' else set @sql=@sql+'create '
set @sql = @sql+' procedure '+@tbname+'_modify_'+convert(varchar(50),@ctid)+N'(
@cmpid int,
@branchid int,
@userdpid int,
@useruid int,
@userotid int,
@ccid bigint,@sourcefield varchar(50)=default, '+char(13)+char(10)
    set @s1 = @dbname+@tbname  
    exec mps_gen_proc_params_default @s1,'cc_id,cc_cmpid,cc_bcid,cc_tag,cc_ctid,cc_createtime,cc_updated',@procparam output 
exec mps_sys_gen_updatestatement @s1,'','@','cc_id,cc_cmpid,cc_bcid,cc_tag,cc_ctid,cc_createtime,cc_updated',@ss2 output 
--- 生成参数清单
if (len(@procparam)<=6) set @procparam = '@sys_nouse_v1 int' 
set @sql = @sql+@procparam+N' ) 
as  
begin  
set nocount on  
declare @nonedate datetime 
set @nonedate = CONVERT(datetime,''1910-1-1'')
 declare @operation varchar(50)
declare @errorinfo nvarchar(4000) 
 set @operation = ''continue'' 
set @errorinfo = '''' 

-- 处理编码
 declare @bmcode varchar(200) 
 declare @newno nvarchar(50) 
 declare @ctid int 
 select top 1 @ctid=cc_ctid,@bmcode = ct_bmcode from '+@dbname+@tbname+',mps_csttype where cc_id=@ccid and cc_ctid=ct_id and (cc_code='''' or cc_code like ''%临%'')
 set @ctid = isnull(@ctid,0) 
 if (@ctid>0) 
 begin
exec mps_bm_getorset @cmpid,''cst'',@ctid,@ccid,@useruid,@bmcode,0,@newno output,1
set @cc_code = @newno 
end 


if (dbo.checkprocedure('''+@tbname+'_check_'+convert(varchar(50),@ctid)+''')=0) 
begin
declare @sql1 nvarchar(max) 
exec drop_temp_table ''#old''
select * into #old from '+@dbname+@tbname+N' where cc_id=@ccid 

update #old set  '+@ss2+'  where cc_id=@ccid  

set @operation=''continue''
set @errorinfo = '''' 
set @sql1 =  N'' exec '+@tbname+'_check_'+convert(varchar(50),@ctid)+' @cmpid,@branchid,@userdpid,@useruid,@userotid,@ccid,@operation output , @errorinfo output '' 
exec sp_executesql @sql1, N''@cmpid int, @branchid int,@userdpid int,@useruid int,@userotid int,@ccid bigint,@operation varchar(50) output , @errorinfo nvarchar(4000) output'', 
@cmpid,@branchid,@userdpid,@useruid,@userotid,@ccid,@operation output , @errorinfo output  
if (@operation=''abort'') 
begin
if (@errorinfo='''') set @errorinfo=''数据验证失败''
select ''abort'' operation , @errorinfo errorinfo 
return 
end 

end 

 --- 以下是预处理事件中的临时表
 '
 
 if (dbo.mps_sys_checkkeywords('cst.beforemodify,cst.aftermodify',@ctid,0,'','')<>0)   -- 没有事件
 begin
 exec mps_sys_gen_updatestatement @s1,'','@','cc_id,cc_cmpid,cc_bcid,cc_tag,cc_ctid,cc_createtime,cc_updated',@ss output 
 if (len(@ss)>10) 
 set @sql = @sql+N'update '+@dbname+@tbname+' set '+char(13)+char(10)+@ss+','+char(13)+char(10)+
 'cc_updated=getdate() '+char(13)+char(10) + 
 'where cc_id=@ccid ' 
 end else 
 begin
 exec mps_sys_gen_updatestatement @s1,'','@','cc_id,cc_cmpid,cc_bcid,cc_tag,cc_ctid,cc_createtime,cc_updated',@ss output 
 if (len(@ss)>10) 
 set @sql = @sql+char(13)+char(10)+
 'exec drop_temp_table ''#old'' '+char(13)+char(10)+
'select * into #old from '+@dbname+@tbname+N' where cc_id=@ccid '+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) +
'exec drop_temp_table ''#new'' '+char(13)+char(10)+
'select * , case when ((@sourcefield is null) or (@sourcefield = ''_none_'')) then '''' else @sourcefield end sourcefield into #new from #old '+char(13)+char(10)+
'update #new set '+char(13)+char(10) +  @ss + char(13)+char(10) 
 end 
 
 if (dbo.mps_sys_checkkeywords('cst.beforemodify',@ctid,0,'','')=0) 
 begin
 set @sql = @sql+'exec '+@tbname+'_modify_before @cmpid,@branchid,@userdpid,@useruid,@userotid,@ccid,@operation output,@errorinfo output   ' 
 end
 
 set @sql = @sql +N'
 if (@operation=''continue'') 
 begin
 set @nonedate = @nonedate 
 '
 if (dbo.mps_sys_checkkeywords('cst.beforemodify,cst.aftermodify',@ctid,0,'','')=0)
 begin
 set @s1 = @dbname+@tbname  
 exec mps_sys_gen_updatestatement @s1,'old','new','cc_id,cc_cmpid,cc_bcid,cc_tag,cc_ctid,cc_createtime,cc_updated',@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)+
 'old.cc_updated=getdate() '+char(13)+char(10) + 
 'from '+@dbname+@tbname+' old,#new new where old.cc_id=new.cc_id ' 
 end
 if (dbo.mps_sys_checkkeywords('cst.aftermodify',@ctid,0,'','')=0) 
 begin
 set @sql = @sql+char(13)+char(10)+
 'exec '+@tbname+'_modify_after @cmpid,@branchid,@userdpid,@useruid,@userotid,@ccid '+char(13)+char(10)
 end
 if (dbo.mps_sys_checkkeywords('cst.aftermodify',@ctid,0,'#new','update')=0) 
 begin
 set @s1 = @dbname+@tbname  
 exec mps_sys_gen_updatestatement @s1,'old','new','cc_id,cc_cmpid,cc_bcid,cc_tag,cc_ctid,cc_createtime,cc_updated',@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)+
 'old.cc_updated=getdate() '+char(13)+char(10) + 
 'from '+@dbname+@tbname+' old,#new new where old.cc_id=new.cc_id ' 
 end
 set @sql=@sql+N'
 select ''ok'' operation, '''' errorinfo 
end  else 
begin
select @operation operation, @errorinfo errorinfo  
end
' 
if (dbo.mps_sys_checkkeywords('cst.beforemodify,cst.aftermodify',@ctid,0,'','')=0)
 begin
 set @sql=@sql+char(13)+char(10)+
'exec drop_temp_table ''#new'' '+char(13)+char(10)+ 
'exec drop_temp_table ''#account'' '+char(13)+char(10)+ 
'exec drop_temp_table ''#old'' '+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'_modify_'+convert(varchar(50),@ctid)+''', 
''@cmpid int,@branchid int,@userdpid int,@useruid int,@userotid int,@ccid bigint,@sourcefield varchar(50),'+@procparam+'''' 
execute(@sql)
set nocount off 
end