IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_gen_prdmodify]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_gen_prdmodify]
GO
create procedure [dbo].[mps_gen_prdmodify](
@cmpid int,
@pcid int
)
as
begin
set nocount on
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
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 = @pcid * (-1) 
exec sys_sql_getTableNamePk @cmpid,'prd',@mctid,0,@tbname output,@fkey output ,@dbname output 
declare @ss nvarchar(MAX) 
declare @sql nvarchar(MAX) 
declare @ord2tag2 int 
declare @procparam nvarchar(MAX) 
set @sql = N'
-- 生成时间:'+convert(varchar(20),getdate(),120)+N' by ebs-core  
'
if (dbo.checkprocedure(@tbname+'_modify')=0) set @sql=@sql+'alter ' else set @sql=@sql+'create '
set @sql = @sql+' procedure '+@tbname+N'_modify(
@cmpid int,
@branchid int,
@userdpid int,
@useruid int,
@userotid int,
@prdid bigint, @sourcefield varchar(50)=default,'+char(13)+char(10)
    set @s1 = @dbname+@tbname  
    exec mps_gen_proc_params_default @s1,'prd_id,prd_cmpid,prd_bcid,prd_tag,prd_createtime,prd_updated',@procparam output 
--- 生成参数清单
if (len(@procparam)<=6) set @procparam = '@sys_nouse_v1 int' 
set @sql = @sql+@procparam+N' ) 
as  
begin  
set nocount on  

SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
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 @pcid int 
 select top 1 @pcid=prd_pcid,@bmcode = pc_bmcode from '+@dbname+@tbname+',mps_prdctg where prd_id=@prdid and prd_pcid=pc_id and (prd_code='''' or prd_code like ''%临%'')
 set @pcid = isnull(@pcid,0) 
 if (@pcid>0) 
 begin
 if (@bmcode>'''') 
 begin
exec mps_bm_getorset @cmpid,''prd'',@pcid,@prdid,@useruid,@bmcode,0,@newno output,1
set @prd_code = @newno 
end 
end 

 --- 以下是预处理事件中的临时表
 '
 
 if (dbo.mps_sys_checkkeywords('prd.beforemodify,prd.aftermodify',@pcid,0,'','')<>0)   -- 没有事件
 begin
 exec mps_sys_gen_updatestatement @s1,'','@','prd_id,prd_cmpid,prd_bcid,prd_tag,prd_createtime,prd_updated',@ss output 
 if (len(@ss)>10) 
 set @sql = @sql+N'update '+@dbname+@tbname+' set '+char(13)+char(10)+@ss+','+char(13)+char(10)+
 'prd_updated=getdate() '+char(13)+char(10) + 
 'where prd_id=@prdid ' 
 end else 
 begin
 exec mps_sys_gen_updatestatement @s1,'','@','prd_id,prd_cmpid,prd_bcid,prd_tag,prd_pcid,prd_createtime,prd_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 prd_id=@prdid '+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('prd.beforemodify',@pcid,0,'','')=0) 
 begin
 set @sql = @sql+'exec '+@tbname+'_modify_before @cmpid,@branchid,@userdpid,@useruid,@userotid,@prdid,@operation output,@errorinfo output   ' 
 end
 
 set @sql = @sql +N'
 if (@operation=''continue'') 
 begin
 set @nonedate = @nonedate 
 '
 if (dbo.mps_sys_checkkeywords('prd.beforemodify,prd.aftermodify',@pcid,0,'','')=0)
 begin
 set @s1 = @dbname+@tbname  
 exec mps_sys_gen_updatestatement @s1,'old','new','prd_id,prd_cmpid,prd_bcid,prd_tag,prd_createtime,prd_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.prd_updated=getdate() '+char(13)+char(10) + 
 'from '+@dbname+@tbname+' old,#new new where old.prd_id=new.prd_id ' 
 end
 if (dbo.mps_sys_checkkeywords('prd.aftermodify',@pcid,0,'','')=0) 
 begin
 set @sql = @sql+char(13)+char(10)+
 'exec '+@tbname+'_modify_after @cmpid,@branchid,@userdpid,@useruid,@userotid,@prdid '+char(13)+char(10)
 end
 if (dbo.mps_sys_checkkeywords('prd.aftermodify',@pcid,0,'#new','update')=0) 
 begin
 set @s1 = @dbname+@tbname  
 exec mps_sys_gen_updatestatement @s1,'old','new','prd_id,prd_cmpid,prd_bcid,prd_tag,prd_createtime,prd_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.prd_updated=getdate() '+char(13)+char(10) + 
 'from '+@dbname+@tbname+' old,#new new where old.prd_id=new.prd_id ' 
 end
 set @sql=@sql+N'
 select ''ok'' operation, '''' errorinfo 
end  else 
begin
select @operation operation, @errorinfo errorinfo  
end
' 
if (dbo.mps_sys_checkkeywords('prd.beforemodify,prd.aftermodify',@pcid,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'',
''@cmpid int,@branchid int,@userdpid int,@useruid int,@userotid int,@prdid bigint,@sourcefield varchar(50),'+@procparam+'''' 
execute(@sql)
set nocount off 
end