IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_gen_biz_newdetail]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_gen_biz_newdetail]
GO
create procedure mps_gen_biz_newdetail(
@cmpid int,
@bizid int,
@ord2tag 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 @ord2 varchar(10) 
set @ord2 = convert(varchar(10),@ord2tag) 
declare @ord2tag2 int 
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+'_newdetail')=0) set @sql=@sql+'alter ' else set @sql=@sql+'create '
set @sql = @sql+' procedure '+@tbname+N'_'+@ord2+'_newdetail'+N'(
@cmpid int, 
@branchid int, 
@userdpid int, 
@useruid int, 
@userotid int, 
@jbid bigint, 
@ordid bigint, 
@ord2tag int, 
@ord2id bigint output, 
@ord2group nvarchar(50)
) 
as  
begin  
set nocount on 
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
declare @neword2id bigint
insert into '+@dbname+@tbname+'_'+@ord2+N'(ord2_ordid, ord2_tag, ord2_group, ord2_updated) values(@ordid, @ord2tag, @ord2group,GETDATE()) 
declare @nonedate datetime 
set @nonedate = CONVERT(datetime,''1910-1-1'')
--set @neword2id = @@Identity 
select  top 1 @neword2id = ord2_id from '+@dbname+@tbname+'_'+@ord2+' with(nolock)  where ord2_ordid=@ordid  order by ord2_id desc 
set @neword2id = isnull(@neword2id,0) 
declare @ord2group2 nvarchar(50) 
set @ord2group2 = '''' 
declare @sys_recid bigint 
set @sys_recid = 0 
declare @errorinfo nvarchar(4000) 
declare @operation varchar(50) 
set @operation = ''continue'' 
set @errorinfo = '''' 

declare @noreturn int 
set @noreturn = 1 
if (@ord2id=-1) 
begin
set @ord2id = 0 
set @noreturn = 0 
end
-- 考虑到批量添加或导入功能，事件处理程序不在此完成 
'
if (dbo.mps_sys_checkkeywords('crm.beforeneword2,crm.afterneword2',@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+' with(nolock) where ord2_id=@neword2id '+char(13)+char(10) 
end 

 
if (dbo.mps_sys_checkkeywords('crm.beforeneword2,crm.afterneword2',@bizid ,0,'#new','')=0) 
begin
set @sql = @sql +N'exec drop_temp_table ''#new'' '+char(13)+char(10)+
'select * ,null recid ,'''' sourcefield into #new from '+@dbname+@tbname+'_'+@ord2+' with(nolock) where ord2_id=@neword2id '+char(13)+char(10) 
end 

if (dbo.mps_sys_checkkeywords('crm.beforeneword2,crm.afterneword2',@bizid ,0,'#account','')=0) 
begin
set @sql = @sql +N'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('crm.beforeneword2,crm.afterneword2',@bizid,0,'#new','insert')=0) 
begin
set @ds1 = @dbname+@tbname+'_'+@ord2 
exec mps_gen_default_code @ds1 , '#new' , 'ord2_id' , @dss output 
set @sql = @sql+char(13)+char(10)+ @dss+char(13)+char(10) 
end

if (dbo.mps_sys_checkkeywords('crm.beforeneword2,crm.afterneword2',@bizid,0,'#new','insert,update')=0)
begin
set @sql = @sql+N'update #new set recid=ord2_id '+char(13)+char(10) 
end

if (dbo.mps_sys_checkkeywords('crm.beforeneword2',@bizid ,0,'','')=0) 
begin
set @sql = @sql +N'exec '+@tbname+'_'+@ord2+'_newdetail_before @cmpid,@branchid,@userdpid,@useruid,@userotid,@jbid,@ordid ,@ord2tag,@ord2group ,@neword2id,@operation output,@errorinfo output  '+char(13)+char(10) 
  end 
  set @sql = @sql +N'
if (@operation <> ''continue'') 
begin 
-- 将新增的那条记录删除 
set @nonedate = @nonedate   -- here useless 
'+char(13)+char(10)
if (dbo.mps_sys_checkkeywords('crm.beforeneword2,crm.afterneword2',@bizid ,0,'#new','')=0) 
begin
set @sql=@sql+'delete '+@dbname+@tbname+'_'+@ord2+' with(rowlock) from #new a where '+@dbname+@tbname+'_'+@ord2+'.ord2_id = a.ord2_id '+char(13)+char(10) 
end else 
begin
set @sql=@sql+'delete '+@dbname+@tbname+'_'+@ord2+' with(rowlock) where ord2_id = @neword2id '+char(13)+char(10) 
end 
set @sql=@sql+N'
if (@errorinfo = '''') set @errorinfo = ''error'' 
set @neword2id = 0 
if (@noreturn>0) select 0 none where 0 = 1 
if (@noreturn=0) set @ord2id=@neword2id 
end else 
begin 
  set @nonedate=@nonedate  -- here useless 
'
if (dbo.mps_sys_checkkeywords('crm.afterneword2',@bizid ,0,'','')=0) 
set @sql = @sql +'exec '+@tbname+N'_'+@ord2+'_newdetail_after  @cmpid,@branchid,@userdpid,@useruid,@userotid,@jbid,@ordid ,@ord2tag,@ord2group ,@neword2id '  +char(13)+char(10) 

set @sql = @sql +'if (@noreturn>0)  select * from '+@dbname+@tbname+'_'+@ord2+' with(nolock) where ord2_id = @neword2id 
else set @ord2id=@neword2id 
end
'
if (dbo.mps_sys_checkkeywords('crm.beforeneword2,crm.afterneword2',@bizid ,0,'#new','')=0) 
begin
set @sql = @sql +N'exec drop_temp_table ''#new'' '+char(13)+char(10) 
end 
if (dbo.mps_sys_checkkeywords('crm.beforeneword2,crm.afterneword2',@bizid ,0,'#account','')=0) 
begin
set @sql = @sql +N'exec drop_temp_table ''#account'' '+char(13)+char(10) 
end 
if (dbo.mps_sys_checkkeywords('crm.beforeneword2,crm.afterneword2',@bizid ,0,'#old','')=0) 
begin
set @sql = @sql +N'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'_'+@ord2+'_newdetail'',
''@cmpid int,@branchid int,@userdpid int,@useruid int,@userotid int,@jbid bigint,'+
'@ordid bigint,@ord2tag int,@ord2id bigint,@ord2group nvarchar(50)''' 
execute(@sql)
set nocount off 
end