IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_gen_dptnew_beforecreate]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_gen_dptnew_beforecreate]
GO
create procedure [dbo].[mps_gen_dptnew_beforecreate](
@cmpid int
)
as
begin
set nocount on
declare @s1 nvarchar(100) 

declare @tbname varchar(50)
declare @dbname varchar(50)
declare @fkey   varchar(50) 
exec sys_sql_getTableNamePk @cmpid,'dpt',@cmpid,0,@tbname output,@fkey output ,@dbname output 
declare @sql nvarchar(MAX) 
declare @ss nvarchar(MAX) 

if (dbo.mps_sys_checkkeywords('dpt.beforecreate',@cmpid,0,'','')<>0) 
begin
set @sql = 'drop procedure '+@tbname+'_newdpt_before' 
if (dbo.checkprocedure(@tbname+'_newdpt_before')=0) execute(@sql) 
return 
end

set @sql = N'
-- 生成时间:'+convert(varchar(20),getdate(),120)+N' by ebs-core  
'
if (dbo.checkprocedure(@tbname+'_newdpt_before')=0) set @sql=@sql+'alter ' else set @sql=@sql+'create '
set @sql = @sql+' procedure '+@tbname+N'_newdpt_before(
     @cmpid int,
     @branchid int,
     @userdpid int,
     @useruid int,
     @userotid int,
     @dpid bigint,
     @operation varchar(50) output,
     @errorinfo nvarchar(4000) output
  ) 
as  
begin  
set nocount on  
declare @sys_recid bigint 
set @sys_recid = 0 
declare @newdpid bigint  
 set @newdpid = @dpid  
 declare @nonedate datetime 
set @nonedate = CONVERT(datetime,''1910-1-1'')
 set @operation = ''continue''
 set @errorinfo = '''' 
 --- 以下是预处理事件中的临时表
 '
 if (dbo.mps_sys_checkkeywords('dpt.beforecreate',@cmpid,0,'#new','')=0) 
 begin
 set @sql = @sql+char(13)+char(10)+
 'exec drop_temp_table ''#new'' '+char(13)+char(10)+
'select * ,'''' sourcefield  into #new from '+@dbname+@tbname+N' where dp_id=@newdpid '
 end
 
 
 if (dbo.mps_sys_checkkeywords('dpt.beforecreate',@cmpid,0,'#old','')=0) 
 begin
 set @sql = @sql+char(13)+char(10)+
 'exec drop_temp_table ''#old'' '+char(13)+char(10)+
'select * into #old from '+@dbname+@tbname+N' where dp_id=@newdpid '
 end

set @sql=@sql+N'
 ---- 以下是开发人员写的 before new order 事件 --------------
 '
 + dbo.mps_sys_getusercode('dpt.beforecreate',@cmpid,0) +N'
 ---- 以上是开发人员写的 before new order 事件 --------------
 
 if (@operation=''continue'') 
 begin
 set @nonedate = @nonedate 
 ' 
 set @s1 = @dbname+@tbname 
 if (dbo.mps_sys_checkkeywords('dpt.beforecreate',@cmpid,0,'#new','update')=0) 
 begin
 exec mps_sys_gen_updatestatement @s1,'old','new','dp_id,dp_cmpid,dp_bcid',@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 , #new new  where old.dp_id=new.dp_id ' +char(13)+char(10) 
 end

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

 if (dbo.mps_sys_checkkeywords('dpt.beforecreate',@cmpid,0,'#new','')=0) 
 set @sql = @sql+N'exec drop_temp_table ''#new'' '+char(13)+char(10) 
 if (dbo.mps_sys_checkkeywords('dpt.beforecreate',@cmpid,0,'#old','')=0) 
 set @sql = @sql+N'exec drop_temp_table ''#old'' '+char(13)+char(10) 
 
set @sql = @sql +N'
set nocount off 
end
'
exec myprint @sql 
execute(@sql)
set nocount off 
end