IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_g_picksource_value]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_g_picksource_value]
GO
 
 
CREATE procedure [dbo].[mps_g_picksource_value](
@cmpid int ,
@branchid int ,
@userdpid int ,
@useruid int ,
@userotid int ,
@objid bigint, -- 这个值可能是ord_id ord2_id , cu_id , prd_id , prd2_id .... 
@ordid bigint,  -- 以下三个变量，是需要新建记录里需要的。新建只有一种情况 ，明细表
@ord2tag int,
@ord2group nvarchar(50) , 
@bizsid int ,  -- biz_source 中的id 
@objids varchar(8000)  ,-- 这是选择的源的ID，有可能多个，如果是多个，以,方式分隔  如果为-1，表示要清除库里的值
@objidname varchar(50)=default 
)

as
begin
set nocount on
declare @jbid bigint 
declare @ord2id bigint 
set @jbid = 0 
set @ord2id = 0 
declare @ttype varchar(20)
declare @tobjid int
declare @tobjid_ int
declare @tobjid2 int
declare @stype varchar(20) 
declare @sfld varchar(50) 
declare @sctgid int 
declare @sctgid2 int 
declare @orderf varchar(200) 
declare @inssql  nvarchar(4000) 
declare @clearsql  nvarchar(4000) 
declare @inssqlold  nvarchar(4000) 
declare @dsql nvarchar(1000) 
declare @allowduplicate int 


declare @hasprdserialno int 
set @hasprdserialno = 1
declare @flag int 
set @flag = charindex('|',@objids) 
if (@flag>0) 
begin
set @allowduplicate=convert(int,SUBSTRING(@objids,1,@flag-1)) 
set @objids=SUBSTRING(@objids,@flag+1,LEN(@objids)-@flag) 
end else
begin
set @allowduplicate = 1 
end  
set @flag = 0 
declare @ctid int 
set @ctid =0 


declare @keyfld varchar(30)
declare @keyfld2 varchar(30)

declare @tbid int 
declare @dbname varchar(50) 
declare @tbname varchar(50)
declare @extcond nvarchar(4000) 



declare @newobjid bigint 

select @ttype=bizs_type , @tobjid=bizs_bizid,@tobjid2=bizs_ord2tag,@stype=bizs_sourcetype , @sctgid=bizs_ctgid , @sctgid2=bizs_ctg2id,
@orderf=bizs_orderfields, @inssql=bizs_insertsql  , @sfld=bizs_sourcefield 
from mps_biztype_source where bizs_id=@bizsid 

set @tobjid_ = (-1) * @tobjid 
if ((@objidname is null) or (@objidname='_none_')) set @objidname=@sfld 
if (@sctgid>=100000000) 
begin
set @flag = @sctgid / 100000000 
set @sctgid = @sctgid - @flag * 100000000 
end 

if (@flag>=5) 
begin
--set @sctgid = @sctgid - @flag * 100000000 
set @ctid = @sctgid 
if (@stype='cst') 
begin
select @sctgid = ct_tag from mps_csttype where ct_id=@sctgid 
end 
if (@stype='prd') 
begin
select @sctgid = pc_tag from mps_prdctg where pc_id=@sctgid 
end 
if (@stype='equ') 
begin
select @sctgid = ec_tag from mps_equctg where ec_id=@sctgid 
end 
if (@stype='etc') 
begin
select @sctgid = etc_tag from mps_ext_resource where etc_id=@sctgid 
end 
end 


if (@inssql>'')
begin
if ((charindex('old.'+@sfld,@inssql)=0) and (@sfld>'')) 
begin
set @inssql=@inssql+'|old.'+@sfld+'=new.{mykeyfld} ' 
end 
end 

set @inssqlold = @inssql
set @clearsql = '' 
set @stype = lower(@stype) 
set @ttype = lower(@ttype) 
declare @sql nvarchar(max) 
declare @sqlnew nvarchar(max) 
 
declare @i int 
declare @s1 nvarchar(1000) 
while (@inssql>'') 
begin
set @i = charindex('|',@inssql) 
if (@i>0)
begin
set @s1 = substring(@inssql , 1 , @i - 1) 
set @inssql = substring(@inssql,@i+1 ,len(@inssql)-@i) 
end else
begin
set @s1 = @inssql 
set @inssql = '' 
end
if (@s1>'') 
begin
set @i = charindex('=',@s1) 
if (@i>0)
begin
if (@clearsql>'') set @clearsql=@clearsql+',' 
set @clearsql = @clearsql +substring(@s1,1,@i-1)+'='''' ' 
end
end
end
set @inssql = @inssqlold 
if (@inssql>'')
begin
set @inssql='update old set '+replace(@inssql,'|',',')   
if (charindex('old.'+@sfld,@inssql)=0)  
begin
set @inssql=@inssql+',old.'+@sfld+'=new.{mykeyfld} ' 
set @clearsql=@clearsql+',old.'+@sfld+'='''' ' 
end 
set @inssql=@inssql+' from ' 
set @clearsql='update old set  '+@clearsql+' from ' 
end 
--insesql  old.ord_bcid=new.cu_depart|old.ord_bizid=new.cu_depart|old.ord_branch=new.cu_depart|old.ord_cmpid=fdsfds|old.ord_createcuid=new.cu_code
-- cu_code|员工编号||center;cu_depart|部门名称||left;cu_name|员工姓名||left;cu_title|岗位||leftcu_name,cu_title 


exec drop_temp_table '#objids' 
declare @sobjid bigint 
create  table #objids(objid__ bigint,objid2__ bigint,ord__ int,tobjid__ bigint,avaliableamount int,selectedamount int)   -- tobjid 用于去重
declare @curInd int,@strlen int 
set @curInd=1
set @strlen=LEN(@objids) 
set @s1='' 
declare @s11 varchar(100)
declare @s12 varchar(100)
declare @s13 varchar(100)

declare @ic int 
set @ic = 0 
while (@curInd<=@strlen) 
begin 

set @i=charindex(',',@objids,@curInd) 
if (@i>0) 
begin 
set @s1=SUBSTRING(@objids,@curInd,@i-@curInd) 
set @curInd=@i+1 
end 
else if (@curInd<=@strlen) 
begin 
select @s1=SUBSTRING(@objids,@curInd,@strlen-@curInd+1) 
set @curInd=@strlen+1 
end 
if (@s1>'') 
begin

set @s11 = @s1 
set @i =  CHARINDEX(':',@s11) 
if (@i>0)
begin
set @s12 = substring(@s11,@i+1,len(@s11)-@i) 
set @s11 = substring(@s11,1,@i-1) 
if (@s12>'')
begin
set @i =  CHARINDEX(':',@s12) 
if (@i>0)
begin
set @s13 = substring(@s12,@i+1,len(@s12)-@i) 
set @s12 = substring(@s12,1,@i-1) 
if (@s13='') set @s13 = '0'
end else
begin
set @s13 = '0'
end 
end else 
begin
set @s12 = '0'
set @s13 = '0'
end
end else
begin
set @s12 = '0'
set @s13 = '0'
end
--insert into #objids(objid__,objid2__,ord__,tobjid__) values(convert(bigint,@s1),@ic,@ic,0) 
if (@allowduplicate=0 and convert(int,@s13)>=1 and (@stype='prd' or @stype='private' or @stype='web' or @stype='rfc'))   -- 一物一码 
begin
set @hasprdserialno = 0
set @i = 1 
while (@i<=convert(int,@s13)) 
begin
set @i = @i + 1 
set @ic = @ic + 1 
insert into #objids(objid__,objid2__,ord__,tobjid__,avaliableamount,selectedamount) 
values(convert(bigint,@s11),case when @objid>0 then @objid else @ic end ,@ic,0,convert(int,@s12),1) 
end 
end else 
begin
set @ic = @ic + 1 
insert into #objids(objid__,objid2__,ord__,tobjid__,avaliableamount,selectedamount) 
values(convert(bigint,@s11),case when @objid>0 then @objid else @ic end ,@ic,0,
convert(int,@s12),convert(int,@s13)) 
end 
end 
end

select top 1 @sobjid = objid__ from #objids 
set @sobjid=isnull(@sobjid,0) 
 
declare @tkeyfld varchar(50)
declare @skeyfld varchar(50) 
declare @ttablename varchar(50)
declare @stablename varchar(50)

exec sys_sql_getTableNamePk @cmpid,@stype,@sctgid,0,@tbname output , @skeyfld output , @dbname output 
if (@tbname='mps_branch') set @tbname = 'mps_groupbranch' ;
set @stablename = @dbname+@tbname 

declare @rtnsql nvarchar(4000) 
set @rtnsql = ''  -- 将调整后的结果取回到本地
if (@ttype='emp') 
begin
if (@inssql>'') set @inssql=@inssql +' mps_employee old , ' 
set @tkeyfld='cu_id'
set @ttablename = 'mps_employee' 
set @clearsql=@clearsql +' '+@ttablename+' old where  '+@tkeyfld+'=@objid '  
if (@sobjid<0 and @inssql>'' and @sfld>'') exec sp_executesql @clearsql, N'@objid bigint', @objid  
if (@sfld>'') 
set @rtnsql = ' * , 0 targetresult from '+@ttablename +' where '+@tkeyfld+'=@objid '  
else 
begin
set @rtnsql = ' * , 1 targetresult from '+@stablename +',#objids where '+@skeyfld+'=objid__ '  
end 
end else 
if (@ttype='web' or @ttype='rfc' or @ttype='svc') 
begin
if (@inssql>'') set @inssql=@inssql +' zsvc_temp_'+LTRIM(rtrim(str(@tobjid)))+' old , ' 
set @tkeyfld='svc_id'
set @ttablename = 'zsvc_temp_' + LTRIM(rtrim(str(@tobjid))) 
set @clearsql=@clearsql +' '+@ttablename+' old where  '+@tkeyfld+'=@objid '  
if (@sobjid<0 and @inssql>'' and @sfld>'') exec sp_executesql @clearsql, N'@objid bigint', @objid  
if (@sfld>'')
set @rtnsql = ' *, 0 targetresult  from '+@ttablename +' where '+@tkeyfld+'=@objid '  
else set @rtnsql = ' *, 1 targetresult  from '+@stablename +',#objids where '+@skeyfld+'=objid__ '  
end else 
if (@ttype='dpt') 
begin
if (@inssql>'') set @inssql=@inssql +' mps_depart old , ' 
set @tkeyfld='dp_id'
set @ttablename = 'mps_depart' 
set @clearsql=@clearsql +' '+@ttablename+' old where  '+@tkeyfld+'=@objid '  
if (@sobjid<0 and @inssql>'' and @sfld>'') exec sp_executesql @clearsql, N'@objid bigint', @objid  
if (@sfld>'') 
set @rtnsql = ' *, 0 targetresult  from '+@ttablename +' where '+@tkeyfld+'=@objid '  
else set @rtnsql = ' * , 1 targetresult from '+@stablename +',#objids where '+@skeyfld+'=objid__ '  
end else 
if (@ttype='shp') 
begin
if (@inssql>'') set @inssql=@inssql +' mps_depart old , ' 
set @tkeyfld='dp_id'
set @ttablename = 'mps_depart' 
set @clearsql=@clearsql +' '+@ttablename+' old where  '+@tkeyfld+'=@objid '  
if (@sobjid<0 and @inssql>'' and @sfld>'') exec sp_executesql @clearsql, N'@objid bigint', @objid  
if (@sfld>'')
set @rtnsql = ' * , 0 targetresult from '+@ttablename +' where '+@tkeyfld+'=@objid '  
else set @rtnsql = ' *, 1 targetresult  from '+@stablename +',#objids where '+@skeyfld+'=objid__ '  
end else 
if (@ttype='brch') 
begin
if (@inssql>'') set @inssql=@inssql +' mps_groupbranch old , ' 
set @tkeyfld='bc_id'
set @ttablename = 'mps_groupbranch' 
set @clearsql=@clearsql +' '+@ttablename+' old where  '+@tkeyfld+'=@objid '  
if (@sobjid<0 and @inssql>'' and @sfld>'') exec sp_executesql @clearsql, N'@objid bigint', @objid  
if (@sfld>'')
set @rtnsql = ' * , 0 targetresult from '+@ttablename +' where '+@tkeyfld+'=@objid '  
else set @rtnsql = ' *, 1 targetresult  from '+@stablename +',#objids where '+@skeyfld+'=objid__ '  
end else 
if (@ttype='doc') 
begin
if (@inssql>'') set @inssql=@inssql +' mps_document old , ' 
set @tkeyfld='doc_id'
set @ttablename = 'mps_document' 
set @clearsql=@clearsql +' '+@ttablename+' old where  '+@tkeyfld+'=@objid '  
if (@sobjid<0 and @inssql>'' and @sfld>'') exec sp_executesql @clearsql, N'@objid bigint', @objid  
if (@sfld>'')
set @rtnsql = ' *, 0 targetresult  from '+@ttablename +' where '+@tkeyfld+'=@objid '  
else set @rtnsql = ' *, 1 targetresult  from '+@stablename +',#objids where '+@skeyfld+'=objid__ '  
end else 
if (@ttype='biz') 
begin
 
exec sys_sql_getTableNamePk @cmpid,'biz',@tobjid,0,@tbname output , @tkeyfld output , @dbname output 
set @tkeyfld='ord_id' 
set @tbname = @dbname+@tbname 

if (@tobjid2>0)  -- 是明细
begin
set @tbname = @tbname+'_'+ ltrim(rtrim(str(@tobjid2))) 
set @tkeyfld='ord2_id'
end  
set @ttablename = @tbname 

if (@inssql>'') set @inssql=@inssql +' '+@ttablename+' old , ' 
set @clearsql=@clearsql +' '+@ttablename+' old where  '+@tkeyfld+'=@objid '  

if (@sobjid<0 and @inssql>'' and @sfld>'') exec sp_executesql @clearsql, N'@objid bigint', @objid  
-- 开始添加新记录，只需要增加，不用赋值
set @i = 1 

if (@i<=@ic and @tobjid2>0 and @objid=0 and @sfld>'')
begin
if (@hasprdserialno=1 and @allowduplicate=1)  -- 如果是一物一码，则不处理下面的
begin
set @dsql = 'update #objids set tobjid__=ord2_id  from '+@ttablename+' where ord2_ordid=@ordid and '+@sfld+'=objid__ '
exec sp_executesql @dsql, N'@ordid bigint', @ordid  
end 
end 
if (@tobjid2>0 and @sfld>'') 
set @sqlnew='insert into '+@ttablename+N'(ord2_ordid, ord2_tag, ord2_group, ord2_updated) values(@ordid, @ord2tag, @ord2group,GETDATE()) 
select top 1 @neword2id = ord2_id from '+@ttablename +' where ord2_ordid=@ordid and ord2_tag=@ord2tag order by ord2_id desc '
 
 
while (@i<=@ic and @tobjid2>0 and @objid=0 and @sfld>'') 
begin
if ((@allowduplicate=0) or (exists (select * from #objids where ord__=@i and tobjid__=0)))
begin
set @newobjid = ISNULL(@newobjid,0) 
exec sp_executesql @sqlnew, N'@ordid bigint,@ord2tag int,@ord2group nvarchar(50),@neword2id bigint out', 
@ordid ,@ord2tag,@ord2group,@newobjid out 
-- set @sqlnew='exec order_'+rtrim(ltrim(str(@tobjid)))+'_'+ltrim(rtrim(str(@tobjid2)))+'_new_formulti @cmpid , @branchid,@userdpid,@useruid,@userotid,@jbid,@ordid,@ord2tag,@ord2id,@ord2group,@newobjid output ' 
-- exec sp_executesql @sqlnew, N'@cmpid int,@branchid int,@userdpid int,@useruid int,@userotid int,@jbid bigint,@ordid bigint,@ord2tag int,@ord2id bigint,@ord2group nvarchar(50),@newobjid bigint out', @cmpid , @branchid,@userdpid,@useruid,@userotid,@jbid,@ordid ,@ord2tag,@ord2id,@ord2group,@newobjid out 
update #objids set objid2__ = @newobjid where ord__=@i 
end
set @i = @i + 1 
end
if (@sfld>'')
begin
if (@tobjid2>0)  -- 是明细
begin
set @rtnsql = ' a.* , 0 targetresult from '+@ttablename +' a ,#objids  where '+@tkeyfld+'=objid2__ '  
end else 
begin
set @rtnsql = ' *, 0 targetresult  from '+@ttablename +' where '+@tkeyfld+'=@objid '  
end 
end else set @rtnsql = ' *, 1 targetresult  from '+@stablename +',#objids where '+@skeyfld+'=objid__ '  
end else 
if (@ttype='cst') 
begin
 
exec sys_sql_getTableNamePk @cmpid,'cst',@tobjid_,0,@tbname output , @tkeyfld output , @dbname output 
set @tkeyfld='cc_id' 
set @keyfld2='cc2_id' 
set @tbname = @dbname+@tbname 

if (@tobjid2>0)  -- 是明细
begin
set @tbname = @tbname+'_'+ ltrim(rtrim(str(@tobjid2))) 
set @tkeyfld='cc2_id'
end  

set @ttablename = @tbname 

if (@inssql>'') set @inssql=@inssql +' '+@ttablename+' old , ' 
set @clearsql=@clearsql +' '+@ttablename+' old where  '+@tkeyfld+'=@objid '  

if (@sobjid<0 and @inssql>'' and @sfld>'') exec sp_executesql @clearsql, N'@objid bigint', @objid  
-- 开始添加新记录，只需要增加，不用赋值
set @i = 1 

if (@i<=@ic and @tobjid2>0 and @objid=0 and @sfld>'')
begin
set @dsql = 'update #objids set tobjid__=cc2_id  from '+@ttablename+' where cc2_ccid=@ordid and '+@sfld+'=objid__ '
exec sp_executesql @dsql, N'@ordid bigint', @ordid  
end 

set @sqlnew='insert into '+@ttablename+N'(cc2_ccid, cc2_tag, cc2_group, cc2_updated) values(@ordid, @ord2tag, @ord2group,GETDATE()) 
set @neword2id = @@Identity '
 
if (@tobjid2>0) 
set @sqlnew='insert into '+@ttablename+N'(cc2_ccid, cc2_tag, cc2_group, cc2_updated) values(@ordid, @ord2tag, @ord2group,GETDATE()) 
select top 1 @neword2id = cc2_id from '+@ttablename +' where cc2_ccid=@ordid and cc2_tag=@ord2tag order by cc2_id desc '

while (@i<=@ic and @tobjid2>0 and @objid=0 and @sfld>'') 
begin
if ((@allowduplicate=0) or (exists (select * from #objids where ord__=@i and tobjid__=0)))
begin
set @newobjid = ISNULL(@newobjid,0) 
exec sp_executesql @sqlnew, N'@ordid bigint,@ord2tag int,@ord2group nvarchar(50),@neword2id bigint out', 
@ordid ,@ord2tag,@ord2group,@newobjid out 
update #objids set objid2__ = @newobjid where ord__=@i 
end
set @i = @i + 1 
end
if (@sfld>'')
begin
if (@tobjid2>0)  -- 是明细
begin
set @rtnsql = ' a.* , 0 targetresult from '+@ttablename +' a ,#objids  where '+@tkeyfld+'=objid2__ '  
end else 
begin
set @rtnsql = ' *, 0 targetresult  from '+@ttablename +' where '+@tkeyfld+'=@objid '  
end 
end else set @rtnsql = ' *, 1 targetresult  from '+@stablename +',#objids where '+@skeyfld+'=objid__ '  
end  else 
if (@ttype='prd') 
begin
 
exec sys_sql_getTableNamePk @cmpid,'prd',@tobjid_,0,@tbname output , @tkeyfld output , @dbname output 
set @tkeyfld='prd_id' 
set @keyfld2='prd2_id' 
set @tbname = @dbname+@tbname 

if (@tobjid2>0)  -- 是明细
begin
set @tbname = @tbname+'_'+ ltrim(rtrim(str(@tobjid2))) 
set @tkeyfld='prd2_id'
end  

set @ttablename = @tbname 

if (@inssql>'') set @inssql=@inssql +' '+@ttablename+' old , ' 
set @clearsql=@clearsql +' '+@ttablename+' old where  '+@tkeyfld+'=@objid '  

if (@sobjid<0 and @inssql>'' and @sfld>'') exec sp_executesql @clearsql, N'@objid bigint', @objid  
-- 开始添加新记录，只需要增加，不用赋值
set @i = 1 

if (@i<=@ic and @tobjid2>0 and @objid=0 and @sfld>'')
begin
set @dsql = 'update #objids set tobjid__=prd2_id  from '+@ttablename+' where prd2_prdid=@ordid and '+@sfld+'=objid__ '
exec sp_executesql @dsql, N'@ordid bigint', @ordid  
end 

set @sqlnew='insert into '+@ttablename+N'(prd2_prdid, prd2_tag, prd2_group, prd2_updated) values(@ordid, @ord2tag, @ord2group,GETDATE()) 
set @neword2id = @@Identity '
 
if (@tobjid2>0) 
set @sqlnew='insert into '+@ttablename+N'(prd2_prdid, prd2_tag, prd2_group, prd2_updated) values(@ordid, @ord2tag, @ord2group,GETDATE()) 
select top 1 @neword2id = prd2_id from '+@ttablename +' where prd2_prdid=@ordid and prd2_tag=@ord2tag order by prd2_id desc '

while (@i<=@ic and @tobjid2>0 and @objid=0 and @sfld>'') 
begin
if ((@allowduplicate=0) or (exists (select * from #objids where ord__=@i and tobjid__=0)))
begin
set @newobjid = ISNULL(@newobjid,0) 
exec sp_executesql @sqlnew, N'@ordid bigint,@ord2tag int,@ord2group nvarchar(50),@neword2id bigint out', 
@ordid ,@ord2tag,@ord2group,@newobjid out 
update #objids set objid2__ = @newobjid where ord__=@i 
end
set @i = @i + 1 
end
if (@sfld>'')
begin
if (@tobjid2>0)  -- 是明细
begin
set @rtnsql = ' a.* , 0 targetresult from '+@ttablename +' a ,#objids  where '+@tkeyfld+'=objid2__ '  
end else 
begin
set @rtnsql = ' *, 0 targetresult  from '+@ttablename +' where '+@tkeyfld+'=@objid '  
end 
end else set @rtnsql = ' *, 1 targetresult  from '+@stablename +',#objids where '+@skeyfld+'=objid__ '  
end   else 
if (@ttype='equ') 
begin
 
exec sys_sql_getTableNamePk @cmpid,'equ',@tobjid_,0,@tbname output , @tkeyfld output , @dbname output 
set @tkeyfld='equ_id' 
set @keyfld2='equ2_id' 
set @tbname = @dbname+@tbname 

if (@tobjid2>0)  -- 是明细
begin
set @tbname = @tbname+'_'+ ltrim(rtrim(str(@tobjid2))) 
set @tkeyfld='equ2_id'
end  

set @ttablename = @tbname 

if (@inssql>'') set @inssql=@inssql +' '+@ttablename+' old , ' 
set @clearsql=@clearsql +' '+@ttablename+' old where  '+@tkeyfld+'=@objid '  

if (@sobjid<0 and @inssql>'' and @sfld>'') exec sp_executesql @clearsql, N'@objid bigint', @objid  
-- 开始添加新记录，只需要增加，不用赋值
set @i = 1 

if (@i<=@ic and @tobjid2>0 and @objid=0 and @sfld>'')
begin
set @dsql = 'update #objids set tobjid__=equ2_id  from '+@ttablename+' where equ2_equid=@ordid and '+@sfld+'=objid__ '
exec sp_executesql @dsql, N'@ordid bigint', @ordid  
end 

set @sqlnew='insert into '+@ttablename+N'(equ2_equid, equ2_tag, equ2_group, equ2_updated) values(@ordid, @ord2tag, @ord2group,GETDATE()) 
set @neword2id = @@Identity '
 
if (@tobjid2>0) 
set @sqlnew='insert into '+@ttablename+N'(equ2_equid, equ2_tag, equ2_group, equ2_updated) values(@ordid, @ord2tag, @ord2group,GETDATE()) 
select top 1 @neword2id = equ2_id from '+@ttablename +' where equ2_equid=@ordid and equ2_tag=@ord2tag order by equ2_id desc '

while (@i<=@ic and @tobjid2>0 and @objid=0 and @sfld>'') 
begin
if ((@allowduplicate=0) or (exists (select * from #objids where ord__=@i and tobjid__=0)))
begin
set @newobjid = ISNULL(@newobjid,0) 
exec sp_executesql @sqlnew, N'@ordid bigint,@ord2tag int,@ord2group nvarchar(50),@neword2id bigint out', 
@ordid ,@ord2tag,@ord2group,@newobjid out 
update #objids set objid2__ = @newobjid where ord__=@i 
end
set @i = @i + 1 
end
if (@sfld>'')
begin
if (@tobjid2>0)  -- 是明细
begin
set @rtnsql = ' a.* , 0 targetresult from '+@ttablename +' a ,#objids  where '+@tkeyfld+'=objid2__ '  
end else 
begin
set @rtnsql = ' * , 0 targetresult from '+@ttablename +' where '+@tkeyfld+'=@objid '  
end 
end else set @rtnsql = ' *, 1 targetresult  from '+@stablename +',#objids where '+@skeyfld+'=objid__ '  
end else
begin
if (@sfld>'') 
set @rtnsql = ' * , 0 targetresult from '+@ttablename +' where '+@tkeyfld+'=@objid '  
else 
begin
set @rtnsql = ' * , 1 targetresult from '+@stablename +',#objids where '+@skeyfld+'=objid__ '  
end
 end 
  
if (@sfld>'') delete #objids where tobjid__>0 

if (@stype='emp') 
begin

if (@inssql>'')   set @inssql=replace(@inssql,'{mykeyfld}','cu_id')
if (@inssqlold>'')   set @inssqlold=replace(@inssqlold,'{mykeyfld}','cu_id')
if (@tobjid2>0 and @objid=0) -- 是添加明细记录
begin
if (@inssql>'')  set @inssql=@inssql +' #objids , mps_employee new  where new.cu_id=objid__ and old.'+@tkeyfld+'=objid2__' 
if (@inssql>'' and @sfld>'') exec sp_executesql @inssql 
end else 
if (@sobjid>0)
begin
set @stype=@stype
if (@inssql>'')  set @inssql=@inssql +' mps_employee new  where new.cu_id=@sobjid and old.'+@tkeyfld+'=@objid ' 
if (@inssql>'' and @sfld>'') exec sp_executesql @inssql, N'@objid bigint,@sobjid bigint', @objid ,@sobjid 
end
  
end  else 
if (@stype='web' or @stype='rfc' or @stype='svc') 
begin

if (@inssql>'')   set @inssql=replace(@inssql,'{mykeyfld}','svc_id')
if (@inssqlold>'')   set @inssqlold=replace(@inssqlold,'{mykeyfld}','svc_id')
if (@tobjid2>0 and @objid=0) -- 是添加明细记录
begin
if (@inssql>'')  set @inssql=@inssql +' #objids , zsvc_temp_'+LTRIM(rtrim(str(@sctgid)))+' new  where new.svc_id=objid__ and old.'+@tkeyfld+'=objid2__' 
if (@inssql>'' and @sfld>'') exec sp_executesql @inssql 
end else 
if (@sobjid>0)
begin
set @stype=@stype
if (@inssql>'')  set @inssql=@inssql +' zsvc_temp_'+LTRIM(rtrim(str(@sctgid)))+' new  where new.svc_id=@sobjid and old.'+@tkeyfld+'=@objid ' 
if (@inssql>'' and @sfld>'') exec sp_executesql @inssql, N'@objid bigint,@sobjid bigint', @objid ,@sobjid 
end
  
end  else 
if (@stype='fee') 
begin
if (@inssql>'')   set @inssql=replace(@inssql,'{mykeyfld}','ai_id')
if (@inssqlold>'')   set @inssqlold=replace(@inssqlold,'{mykeyfld}','ai_id')
if (@tobjid2>0 and @objid=0) -- 是添加明细记录
begin
if (@inssql>'')  set @inssql=@inssql +' #objids , mps_account_item new  where new.ai_id=objid__ and old.'+@tkeyfld+'=objid2__' 
if (@inssql>'' and @sfld>'') exec sp_executesql @inssql 
end else 
if (@sobjid>0)
begin
if (@inssql>'')  set @inssql=@inssql +' mps_account_item new  where new.ai_id=@sobjid and old.'+@tkeyfld+'=@objid ' 
if (@inssql>'' and @sfld>'') exec sp_executesql @inssql, N'@objid bigint,@sobjid bigint', @objid ,@sobjid 
end
end else 
if (@stype='barn') 
begin
if (@inssql>'')   set @inssql=replace(@inssql,'{mykeyfld}','wh_id')
if (@inssqlold>'')   set @inssqlold=replace(@inssqlold,'{mykeyfld}','wh_id')
if (@tobjid2>0 and @objid=0) -- 是添加明细记录
begin
if (@inssql>'')  set @inssql=@inssql +' #objids , mps_warehouse new  where new.wh_id=objid__ and old.'+@tkeyfld+'=objid2__' 
if (@inssql>'' and @sfld>'') exec sp_executesql @inssql 
end else 
if (@sobjid>0)
begin
if (@inssql>'')  set @inssql=@inssql +' mps_warehouse new  where new.wh_id=@sobjid and old.'+@tkeyfld+'=@objid ' 
if (@inssql>'' and @sfld>'') exec sp_executesql @inssql, N'@objid bigint,@sobjid bigint', @objid ,@sobjid 
end
end else 
if (@stype='dpt' or @stype='shp') 
begin
if (@inssql>'')   set @inssql=replace(@inssql,'{mykeyfld}','dp_id')
if (@inssqlold>'')   set @inssqlold=replace(@inssqlold,'{mykeyfld}','dp_id')
if (@tobjid2>0 and @objid=0) -- 是添加明细记录
begin
if (@inssql>'')  set @inssql=@inssql +' #objids , mps_depart new  where new.dp_id=objid__ and old.'+@tkeyfld+'=objid2__' 
if (@inssql>'' and @sfld>'') exec sp_executesql @inssql 
end else 
if (@sobjid>0)
begin
if (@inssql>'')  set @inssql=@inssql +' mps_depart new  where new.dp_id=@sobjid and old.'+@tkeyfld+'=@objid ' 
if (@inssql>'' and @sfld>'') exec sp_executesql @inssql, N'@objid bigint,@sobjid bigint', @objid ,@sobjid 
end
end  else 
if (@stype='private') 
begin
select @extcond = ps_extcond from mps_privatesql where ps_id=@sctgid 
set @extcond=isnull(@extcond,'') 
set @tbname = 'privateview_'+ltrim(rtrim(str(@sctgid))) 

if (@inssql>'')   set @inssql=replace(@inssql,'{mykeyfld}','objid')
if (@inssqlold>'')   set @inssqlold=replace(@inssqlold,'{mykeyfld}','objid')
if (@tobjid2>0 and @objid=0) -- 是添加明细记录
begin
if (@inssql>'')  set @inssql=@inssql +' #objids , '+@tbname+' new  where new.objid=objid__ and old.'+@tkeyfld+'=objid2__' 
if (@inssql>'' and @sfld>'') exec sp_executesql @inssql 
end else 
if (@sobjid>0)
begin
if (@inssql>'')  set @inssql=@inssql +' '+@tbname+' new  where new.objid=@sobjid and old.'+@tkeyfld+'=@objid ' 
if (@inssql>'' and @sfld>'') exec sp_executesql @inssql, N'@objid bigint,@sobjid bigint', @objid ,@sobjid 
end
end else 
if (@stype='brch') 
begin
if (@inssql>'')   set @inssql=replace(@inssql,'{mykeyfld}','bc_id')
if (@inssqlold>'')   set @inssqlold=replace(@inssqlold,'{mykeyfld}','bc_id')
if (@tobjid2>0 and @objid=0) -- 是添加明细记录
begin
if (@inssql>'')  set @inssql=@inssql +' #objids , mps_groupbranch new  where new.bc_cmpid=@cmpid and new.bc_id=objid__ and old.'+@tkeyfld+'=objid2__' 
if (@inssql>'' and @sfld>'') exec sp_executesql @inssql ,N'@cmpid int',@cmpid 
end else 
if (@sobjid>=0)
begin
if (@inssql>'')  set @inssql=@inssql +' mps_groupbranch new  where new.bc_cmpid=@cmpid and new.bc_id=@sobjid and old.'+@tkeyfld+'=@objid ' 
if (@inssql>'' and @sfld>'') exec sp_executesql @inssql, N'@objid bigint,@sobjid bigint,@cmpid int', @objid ,@sobjid ,@cmpid 
end
end  else 
if (@stype='doc') 
begin
if (@inssql>'')   set @inssql=replace(@inssql,'{mykeyfld}','doc_id')
if (@inssqlold>'')   set @inssqlold=replace(@inssqlold,'{mykeyfld}','doc_id')
if (@tobjid2>0 and @objid=0) -- 是添加明细记录
begin
if (@inssql>'') set @inssql=@inssql +' #objids,mps_document new where new.doc_id=objid__ and old.'+@tkeyfld+'=objid2__ ' 
if (@inssql>'' and @sfld>'') exec sp_executesql @inssql 
end else
if (@sobjid>0) 
begin
if (@inssql>'') set @inssql=@inssql +' mps_document new where new.doc_id=@sobjid and old.'+@tkeyfld+'=@objid ' 
if (@inssql>'' and @sfld>'') exec sp_executesql @inssql, N'@objid bigint,@sobjid bigint', @objid ,@sobjid 
end
end  else 
if (@stype='biz') 
begin
exec sys_sql_getTableNamePk @cmpid,'biz',@sctgid,0,@tbname output , @skeyfld output , @dbname output 
set @skeyfld='ord_id' 
set @tbname = @dbname+@tbname 

if (@sctgid2>0) set @tbname = @tbname+'_'+ ltrim(rtrim(str(@sctgid2))) 
set @skeyfld='ord_id' 
if (@sctgid2>0) set @skeyfld='ord2_id' 

if (@inssql>'')   set @inssql=replace(@inssql,'{mykeyfld}',@skeyfld)
if (@inssqlold>'')   set @inssqlold=replace(@inssqlold,'{mykeyfld}',@skeyfld)
if (@tobjid2>0 and @objid=0) -- 是添加明细记录
begin
if (@inssql>'') set @inssql=@inssql +' '+@tbname+' new , #objids where new.'+@skeyfld+'=objid__ and old.'+@tkeyfld+'=objid2__' 
if (@inssql>'' and @sfld>'') exec sp_executesql @inssql 
end else 
if (@sobjid>0)
begin
if (@inssql>'') set @inssql=@inssql +' '+@tbname+' new  where new.'+@skeyfld+'=@sobjid and old.'+@tkeyfld+'=@objid' 
if (@inssql>'' and @sfld>'') exec sp_executesql @inssql, N'@objid bigint,@sobjid bigint', @objid ,@sobjid 
end
end else
if (@stype='cst') 
begin
exec sys_sql_getTableNamePk @cmpid,'cst',@sctgid,0,@tbname output , @skeyfld output , @dbname output 
set @skeyfld='cc_id' 
set @tbname = @dbname+@tbname 

if (@sctgid2>0) set @tbname = @tbname+'_'+ ltrim(rtrim(str(@sctgid2))) 
set @skeyfld='cc_id' 
if (@sctgid2>0) set @skeyfld='cc2_id' 

if (@inssql>'')   set @inssql=replace(@inssql,'{mykeyfld}',@skeyfld)
if (@inssqlold>'')   set @inssqlold=replace(@inssqlold,'{mykeyfld}',@skeyfld)
if (@tobjid2>0 and @objid=0) -- 是添加明细记录
begin
if (@inssql>'') set @inssql=@inssql +' '+@tbname+' new , #objids where new.'+@skeyfld+'=objid__ and old.'+@tkeyfld+'=objid2__' 
if (@inssql>'' and @sfld>'') exec sp_executesql @inssql 
end else 
if (@sobjid>0)
begin
if (@inssql>'') set @inssql=@inssql +' '+@tbname+' new  where new.'+@skeyfld+'=@sobjid and old.'+@tkeyfld+'=@objid' 
if (@inssql>'' and @sfld>'') exec sp_executesql @inssql, N'@objid bigint,@sobjid bigint', @objid ,@sobjid 
end
end else
if (@stype='prd') 
begin
exec sys_sql_getTableNamePk @cmpid,'prd',@sctgid,0,@tbname output , @skeyfld output , @dbname output 
set @skeyfld='prd_id' 
set @tbname = @dbname+@tbname 

if (@sctgid2>0) set @tbname = @tbname+'_'+ ltrim(rtrim(str(@sctgid2))) 
set @skeyfld='prd_id' 
if (@sctgid2>0) set @skeyfld='prd2_id' 

if (@inssql>'')   set @inssql=replace(@inssql,'{mykeyfld}',@skeyfld)
if (@inssqlold>'')   set @inssqlold=replace(@inssqlold,'{mykeyfld}',@skeyfld)
if (@tobjid2>0 and @objid=0) -- 是添加明细记录
begin
if (@inssql>'') set @inssql=@inssql +' '+@tbname+' new , #objids where new.'+@skeyfld+'=objid__ and old.'+@tkeyfld+'=objid2__' 
if (@inssql>'' and @sfld>'') exec sp_executesql @inssql 
end else 
if (@sobjid>0)
begin
if (@inssql>'') set @inssql=@inssql +' '+@tbname+' new  where new.'+@skeyfld+'=@sobjid and old.'+@tkeyfld+'=@objid' 
if (@inssql>'' and @sfld>'') exec sp_executesql @inssql, N'@objid bigint,@sobjid bigint', @objid ,@sobjid 
end
end



  
set @rtnsql = 'select @inssql upfldmap,'+ @rtnsql  

--- 以下开始处理事件
if (@ttype='emp') 
begin
if (@tobjid2>0 and @objid=0) -- 是添加明细记录
begin
set @sql = 'select a.* from '+@ttablename+' a , #objids where '+@tkeyfld+'=objid2__' 
-- exec sp_executesql @sql
exec sp_executesql @rtnsql, N'@inssql nvarchar(4000),@sobjid bigint,@objid bigint,@cmpid int', @inssqlold,@sobjid ,@objid,@cmpid
end else 
if ((@sobjid>0 and @stype<>'brch') or (@sobjid>=0 and @stype='brch'))
begin
exec sys_sql_getTableNamePk @cmpid,@stype,@sctgid,@sctgid2,@tbname output , @skeyfld output , @dbname output 
set @tbname = @dbname+@tbname 
if (@stype='brch') 
begin
set @tbname='mps_groupbranch'
set @sql = 'select @inssql upfldmap,a.* from '+@tbname+' a  where bc_cmpid=@cmpid and '+@skeyfld+'=@sobjid ' 
end else 
begin
set @sql = 'select @inssql upfldmap,a.* from '+@tbname+' a  where '+@skeyfld+'=@sobjid ' 
end 
--exec sp_executesql @sql, N'@inssql nvarchar(4000),@sobjid bigint,@cmpid int', @inssqlold,@sobjid ,@cmpid
exec sp_executesql @rtnsql, N'@inssql nvarchar(4000),@sobjid bigint,@objid bigint,@cmpid int', @inssqlold,@sobjid ,@objid,@cmpid
end
end  else 
if (@ttype='web' or @ttype='svc' or @ttype='rfc') 
begin
if (@tobjid2>0 and @objid=0) -- 是添加明细记录
begin
set @sql = 'select a.* from '+@ttablename+' a , #objids where '+@tkeyfld+'=objid2__' 
-- exec sp_executesql @sql
exec sp_executesql @rtnsql, N'@inssql nvarchar(4000),@sobjid bigint,@objid bigint,@cmpid int', @inssqlold,@sobjid ,@objid,@cmpid
end else 
if ((@sobjid>0 and @stype<>'brch') or (@sobjid>=0 and @stype='brch'))
begin
exec sys_sql_getTableNamePk @cmpid,@stype,@sctgid,@sctgid2,@tbname output , @skeyfld output , @dbname output 
set @tbname = @dbname+@tbname 
if (@stype='brch') 
begin
set @tbname='mps_groupbranch'
set @sql = 'select @inssql upfldmap,a.* from '+@tbname+' a  where bc_cmpid=@cmpid and '+@skeyfld+'=@sobjid ' 
end else 
begin
set @sql = 'select @inssql upfldmap,a.* from '+@tbname+' a  where '+@skeyfld+'=@sobjid ' 
end 
--exec sp_executesql @sql, N'@inssql nvarchar(4000),@sobjid bigint,@cmpid int', @inssqlold,@sobjid ,@cmpid
exec sp_executesql @rtnsql, N'@inssql nvarchar(4000),@sobjid bigint,@objid bigint,@cmpid int', @inssqlold,@sobjid ,@objid,@cmpid
end
end  else 
if (@ttype='dpt' or @ttype='shp') 
begin
if (@inssql>'')   set @inssql=replace(@inssql,'{mykeyfld}','dp_id')
if (@inssqlold>'')   set @inssqlold=replace(@inssqlold,'{mykeyfld}','dp_id')
if (@tobjid2>0 and @objid=0) -- 是添加明细记录
begin
set @sql = 'select a.* from '+@ttablename+' a , #objids where '+@tkeyfld+'=objid2__' 
--exec sp_executesql @sql
exec sp_executesql @rtnsql, N'@inssql nvarchar(4000),@sobjid bigint,@objid bigint,@cmpid int', @inssqlold,@sobjid ,@objid,@cmpid
end else 
if ((@sobjid>0 and @stype<>'brch') or (@sobjid>=0 and @stype='brch'))
begin
exec sys_sql_getTableNamePk @cmpid,@stype,@sctgid,@sctgid2,@tbname output , @skeyfld output , @dbname output 
set @tbname = @dbname+@tbname 
if (@stype='brch') 
begin
set @tbname='mps_groupbranch'
set @sql = 'select @inssql upfldmap,a.* from '+@tbname+' a  where bc_cmpid=@cmpid and '+@skeyfld+'=@sobjid ' 
end else 
begin
set @sql = 'select @inssql upfldmap,a.* from '+@tbname+' a  where '+@skeyfld+'=@sobjid ' 
end 
--exec sp_executesql @sql, N'@inssql nvarchar(4000),@sobjid bigint,@cmpid int', @inssqlold,@sobjid ,@cmpid
exec sp_executesql @rtnsql, N'@inssql nvarchar(4000),@sobjid bigint,@objid bigint,@cmpid int', @inssqlold,@sobjid ,@objid,@cmpid
end
end  else 
if (@ttype='private') 
begin
select @extcond = ps_extcond from mps_privatesql where ps_id=@sctgid 
set @extcond=isnull(@extcond,'') 
set @tbname = 'privateview_'+ltrim(rtrim(str(@sctgid))) 

if (@inssql>'')   set @inssql=replace(@inssql,'{mykeyfld}','objid')
if (@inssqlold>'')   set @inssqlold=replace(@inssqlold,'{mykeyfld}','objid')
if (@tobjid2>0 and @objid=0) -- 是添加明细记录
begin
set @sql = 'select a.* from '+@ttablename+' a , #objids where '+@tkeyfld+'=objid2__' 
--exec sp_executesql @sql
exec sp_executesql @rtnsql, N'@inssql nvarchar(4000),@sobjid bigint,@objid bigint,@cmpid int', @inssqlold,@sobjid ,@objid,@cmpid
end else 
if ((@sobjid>0 and @stype<>'brch') or (@sobjid>=0 and @stype='brch'))
begin
exec sys_sql_getTableNamePk @cmpid,@stype,@sctgid,@sctgid2,@tbname output , @skeyfld output , @dbname output 
set @tbname = @dbname+@tbname 
if (@stype='brch') 
begin
set @tbname='mps_groupbranch'
set @sql = 'select @inssql upfldmap,a.* from '+@tbname+' a  where bc_cmpid=@cmpid and '+@skeyfld+'=@sobjid ' 
end else 
begin
set @sql = 'select @inssql upfldmap,a.* from '+@tbname+' a  where '+@skeyfld+'=@sobjid ' 
end 
--exec sp_executesql @sql, N'@inssql nvarchar(4000),@sobjid bigint,@cmpid int', @inssqlold,@sobjid ,@cmpid
exec sp_executesql @rtnsql, N'@inssql nvarchar(4000),@sobjid bigint,@objid bigint,@cmpid int', @inssqlold,@sobjid ,@objid,@cmpid
end

end else 
if (@ttype='brch') 
begin
if (@inssql>'')   set @inssql=replace(@inssql,'{mykeyfld}','bc_id')
if (@inssqlold>'')   set @inssqlold=replace(@inssqlold,'{mykeyfld}','bc_id')
if (@tobjid2>0 and @objid=0) -- 是添加明细记录
begin
set @sql = 'select a.* from '+@ttablename+' a , #objids where '+@tkeyfld+'=objid2__' 
--exec sp_executesql @sql
exec sp_executesql @rtnsql, N'@inssql nvarchar(4000),@sobjid bigint,@objid bigint,@cmpid int', @inssqlold,@sobjid ,@objid,@cmpid
end else 
if ((@sobjid>0 and @stype<>'brch') or (@sobjid>=0 and @stype='brch'))
begin
  exec sys_sql_getTableNamePk @cmpid,@stype,@sctgid,@sctgid2,@tbname output , @skeyfld output , @dbname output 
set @tbname = @dbname+@tbname 
if (@stype='brch') 
begin
set @tbname='mps_groupbranch'
set @sql = 'select @inssql upfldmap,a.* from '+@tbname+' a  where bc_cmpid=@cmpid and '+@skeyfld+'=@sobjid ' 
end else 
begin
set @sql = 'select @inssql upfldmap,a.* from '+@tbname+' a  where '+@skeyfld+'=@sobjid ' 
end 
--exec sp_executesql @sql, N'@inssql nvarchar(4000),@sobjid bigint,@cmpid int', @inssqlold,@sobjid ,@cmpid
exec sp_executesql @rtnsql, N'@inssql nvarchar(4000),@sobjid bigint,@objid bigint,@cmpid int', @inssqlold,@sobjid ,@objid,@cmpid
end
end  else 
if (@ttype='doc') 
begin
if (@inssql>'')   set @inssql=replace(@inssql,'{mykeyfld}','doc_id')
if (@inssqlold>'')   set @inssqlold=replace(@inssqlold,'{mykeyfld}','doc_id')
if (@tobjid2>0 and @objid=0) -- 是添加明细记录
begin
set @sql = 'select a.* from '+@ttablename+' a , #objids where '+@tkeyfld+'=objid2__' 
--exec sp_executesql @sql
exec sp_executesql @rtnsql, N'@inssql nvarchar(4000),@sobjid bigint,@objid bigint,@cmpid int', @inssqlold,@sobjid ,@objid,@cmpid
end else
if ((@sobjid>0 and @stype<>'brch') or (@sobjid>=0 and @stype='brch'))
begin
  exec sys_sql_getTableNamePk @cmpid,@stype,@sctgid,@sctgid2,@tbname output , @skeyfld output , @dbname output 
set @tbname = @dbname+@tbname 
if (@stype='brch') 
begin
set @tbname='mps_groupbranch'
set @sql = 'select @inssql upfldmap,a.* from '+@tbname+' a  where bc_cmpid=@cmpid and '+@skeyfld+'=@sobjid ' 
end else 
begin
set @sql = 'select @inssql upfldmap,a.* from '+@tbname+' a  where '+@skeyfld+'=@sobjid ' 
end 
--exec sp_executesql @sql, N'@inssql nvarchar(4000),@sobjid bigint,@cmpid int', @inssqlold,@sobjid ,@cmpid
 
exec sp_executesql @rtnsql, N'@inssql nvarchar(4000),@sobjid bigint,@objid bigint,@cmpid int', @inssqlold,@sobjid ,@objid,@cmpid
end
end  else 
if (@ttype='biz') 
begin
 
exec sys_sql_getTableNamePk @cmpid,'biz',@tobjid,0,@tbname output , @skeyfld output , @dbname output 
set @skeyfld='ord_id' 
set @tbname = @dbname+@tbname 
 
if (@tobjid2>0) set @tbname = @tbname+'_'+ ltrim(rtrim(str(@tobjid2))) 
set @skeyfld='ord_id' 
if (@tobjid2>0) set @skeyfld='ord2_id' 
 
if (@tobjid2>0 and @objid=0) -- 是添加明细记录
begin
-- 数据已经生成 准备执行事件 
if (@sfld>'')
begin
SET @sql=N'
declare @operation varchar(50)
declare @errorinfo nvarchar(4000)
set @operation =''continue'' 
set @errorinfo ='''' 

' 
if (dbo.mps_sys_checkkeywords('crm.beforeneword2,crm.afterneword2',@tobjid ,0,'#old','')=0) 
begin
set @sql = @sql +N'exec drop_temp_table ''#old'' '+char(13)+
'select a.* into #old from '+@tbname+' a,#objids b where a.ord2_id=b.objid2__ '+char(13) 
end 
if (dbo.mps_sys_checkkeywords('crm.beforeneword2,crm.afterneword2',@tobjid ,0,'#new','')=0) 
begin
set @sql = @sql +N'exec drop_temp_table ''#new'' '+char(13)+
'select a.* , '''+@objidname+'''   sourcefield , null recid into #new from '+@tbname+' a,#objids b where a.ord2_id=b.objid2__ '+char(13) 
end 

if (dbo.mps_sys_checkkeywords('crm.beforeneword2,crm.afterneword2',@tobjid ,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',@tobjid,0,'#new','insert,update')=0)
begin
set @sql = @sql+N'update #new set recid=ord2_id '+CHAR(13) 
end

if (dbo.mps_sys_checkkeywords('crm.beforeneword2',@tobjid ,0,'','')=0) 
begin
set @sql = @sql +N'exec order_'+convert(varchar(10),@tobjid)+'_'+convert(varchar(10),@tobjid2)+
'_newdetail_before '+convert(varchar(10),@cmpid)+','+convert(varchar(10),@branchid)+','+
convert(varchar(10),@userdpid)+','+convert(varchar(10),@useruid)+','+convert(varchar(10),@userotid)+
','+convert(varchar(10),@jbid)+','+convert(varchar(10),@ordid)+' ,'+convert(varchar(10),@ord2tag)+','''+
@ord2group+''' ,'+convert(varchar(10),@newobjid)+',@operation output,@errorinfo output  '+char(13) 
end 
  
  set @sql = @sql +N'
  if (@operation=''continue'')
  begin
  set @operation=@operation 
  '
  if (dbo.mps_sys_checkkeywords('crm.afterneword2',@tobjid ,0,'','')=0) 
begin
set @sql = @sql +N'exec order_'+convert(varchar(10),@tobjid)+'_'+convert(varchar(10),@tobjid2)+
'_newdetail_after '+convert(varchar(10),@cmpid)+','+convert(varchar(10),@branchid)+','+
convert(varchar(10),@userdpid)+','+convert(varchar(10),@useruid)+','+convert(varchar(10),@userotid)+
','+convert(varchar(10),@jbid)+','+convert(varchar(10),@ordid)+' ,'+convert(varchar(10),@ord2tag)+','''+
@ord2group+''' ,'+convert(varchar(10),@newobjid)+' '+char(13) 
end 
  
set @sql = @sql +N'
  end
  '
  
  
  if (dbo.mps_sys_checkkeywords('crm.beforeneword2,crm.afterneword2',@tobjid ,0,'#old','')=0) 
begin
set @sql = @sql +N'exec drop_temp_table ''#old'' '+char(13) 
end 
if (dbo.mps_sys_checkkeywords('crm.beforeneword2,crm.afterneword2',@tobjid ,0,'#new','')=0) 
begin
set @sql = @sql +N'exec drop_temp_table ''#new'' '+char(13) 
end 
if (dbo.mps_sys_checkkeywords('crm.beforeneword2,crm.afterneword2',@tobjid ,0,'#account','')=0) 
begin
set @sql = @sql +N'exec drop_temp_table ''#account'' '+char(13) 
end
 
execute(@sql) 
end 

set @sql = 'select a.* from '+@ttablename+' a , #objids where '+@tkeyfld+'=objid2__' 
--exec sp_executesql @sql
 exec sp_executesql @rtnsql, N'@inssql nvarchar(4000),@sobjid bigint,@objid bigint,@cmpid int', @inssqlold,@sobjid ,@objid,@cmpid
end else 
if ((@sobjid>0 and @stype<>'brch') or (@sobjid>=0 and @stype='brch'))
begin
-- 这里有二种情况 ， 一是改主表，二是改明细，都要支持事件
-- 只支持修改以后
if (@sfld>'')
begin
if (@tobjid2>0) -- 改明细表
begin
if (dbo.mps_sys_checkkeywords('crm.aftermodifyord2',@tobjid ,0,'','')=0)
SET @sql=N'exec order_'+convert(varchar(10),@tobjid)+'_modify_'+convert(varchar(10),@tobjid2)+
' @ord2id , @cmpid , @branchid , @userdpid , @userotid , @useruid  , @jbid , @ordid , @ord2tag ,@sourcefield '
exec sp_executesql @sql, N'@ord2id bigint, @cmpid int, @branchid bigint, @userdpid bigint, @userotid bigint, @useruid int, @jbid bigint, @ordid bigint, @ord2tag int,@sourcefield varchar(50) ', 
@objid , @cmpid , @branchid , @userdpid , @userotid , @useruid  , @jbid , @ordid , @tobjid2 , @sfld 
end else  --  改主表 
begin
if (dbo.mps_sys_checkkeywords('crm.aftermodify',@tobjid ,0,'','')=0)
SET @sql=N'exec order_'+convert(varchar(10),@tobjid)+'_modify'+
' @cmpid ,@branchid ,@userdpid ,@useruid ,@userotid ,@jbid ,@ordid ,@sourcefield '
exec sp_executesql @sql, N'@cmpid int,@branchid int,@userdpid int,@useruid int,@userotid int,@jbid bigint,@ordid bigint , @sourcefield varchar(50) ', 
@cmpid ,@branchid ,@userdpid ,@useruid ,@userotid ,@jbid ,@objid   , @sfld  
end 
end 
--exec sp_executesql @sql, N'@inssql nvarchar(4000),@sobjid bigint,@cmpid int', @inssqlold,@sobjid ,@cmpid
exec sp_executesql @rtnsql, N'@inssql nvarchar(4000),@sobjid bigint,@objid bigint,@cmpid int', @inssqlold,@sobjid ,@objid,@cmpid
--print @sql
end
end else
if (@ttype='cst') 
begin
 
exec sys_sql_getTableNamePk @cmpid,'cst',@tobjid,0,@tbname output , @skeyfld output , @dbname output 
set @skeyfld='cc_id' 
set @tbname = @dbname+@tbname 
 
if (@tobjid2>0) set @tbname = @tbname+'_'+ ltrim(rtrim(str(@tobjid2))) 
set @skeyfld='cc_id' 
if (@tobjid2>0) set @skeyfld='cc2_id' 
 
if (@tobjid2>0 and @objid=0) -- 是添加明细记录
begin

-- 数据已经生成 准备执行事件 
if (@sfld>'')
begin
SET @sql=N'
declare @operation varchar(50)
declare @errorinfo nvarchar(4000)
set @operation =''continue'' 
set @errorinfo ='''' 

' 
  
  set @sql = @sql +N'
  if (@operation=''continue'')
  begin
  set @operation=@operation 
  '
set @sql = @sql +N'
  end
  '
  
  
execute(@sql) 
end 

--exec sp_executesql @sql
exec sp_executesql @rtnsql, N'@inssql nvarchar(4000),@sobjid bigint,@objid bigint,@cmpid int', @inssqlold,@sobjid ,@objid,@cmpid
 
end else 
if ((@sobjid>0 and @stype<>'brch') or (@sobjid>=0 and @stype='brch'))
begin
exec sp_executesql @rtnsql, N'@inssql nvarchar(4000),@sobjid bigint,@objid bigint,@cmpid int', @inssqlold,@sobjid ,@objid,@cmpid
--print @sql
end
end   else
if (@ttype='prd') 
begin
 
exec sys_sql_getTableNamePk @cmpid,'prd',@tobjid,0,@tbname output , @skeyfld output , @dbname output 
set @skeyfld='prd_id' 
set @tbname = @dbname+@tbname 
 
if (@tobjid2>0) set @tbname = @tbname+'_'+ ltrim(rtrim(str(@tobjid2))) 
set @skeyfld='prd_id' 
if (@tobjid2>0) set @skeyfld='prd2_id' 
 
if (@tobjid2>0 and @objid=0) -- 是添加明细记录
begin

-- 数据已经生成 准备执行事件 
if (@sfld>'')
begin
SET @sql=N'
declare @operation varchar(50)
declare @errorinfo nvarchar(4000)
set @operation =''continue'' 
set @errorinfo ='''' 

' 
  
  set @sql = @sql +N'
  if (@operation=''continue'')
  begin
  set @operation=@operation 
  '
set @sql = @sql +N'
  end
  '
  
  
execute(@sql) 
end 

--exec sp_executesql @sql
exec sp_executesql @rtnsql, N'@inssql nvarchar(4000),@sobjid bigint,@objid bigint,@cmpid int', @inssqlold,@sobjid ,@objid,@cmpid
 
end else 
if ((@sobjid>0 and @stype<>'brch') or (@sobjid>=0 and @stype='brch'))
begin
 
exec sp_executesql @rtnsql, N'@inssql nvarchar(4000),@sobjid bigint,@objid bigint,@cmpid int', @inssqlold,@sobjid ,@objid,@cmpid
end
end   else
if (@ttype='equ') 
begin
 
exec sys_sql_getTableNamePk @cmpid,'equ',@tobjid,0,@tbname output , @skeyfld output , @dbname output 
set @skeyfld='equ_id' 
set @tbname = @dbname+@tbname 
 
if (@tobjid2>0) set @tbname = @tbname+'_'+ ltrim(rtrim(str(@tobjid2))) 
set @skeyfld='equ_id' 
if (@tobjid2>0) set @skeyfld='equ2_id' 
 
if (@tobjid2>0 and @objid=0) -- 是添加明细记录
begin

-- 数据已经生成 准备执行事件 
if (@sfld>'')
begin
SET @sql=N'
declare @operation varchar(50)
declare @errorinfo nvarchar(4000)
set @operation =''continue'' 
set @errorinfo ='''' 

' 
  
  set @sql = @sql +N'
  if (@operation=''continue'')
  begin
  set @operation=@operation 
  '
set @sql = @sql +N'
  end
  '
  
  
execute(@sql) 
end 

--exec sp_executesql @sql
exec sp_executesql @rtnsql, N'@inssql nvarchar(4000),@sobjid bigint,@objid bigint,@cmpid int', @inssqlold,@sobjid ,@objid,@cmpid
 
end else 
if ((@sobjid>0 and @stype<>'brch') or (@sobjid>=0 and @stype='brch'))
begin
 
exec sp_executesql @rtnsql, N'@inssql nvarchar(4000),@sobjid bigint,@objid bigint,@cmpid int', @inssqlold,@sobjid ,@objid,@cmpid
end
end else
begin
exec sp_executesql @rtnsql, N'@inssql nvarchar(4000),@sobjid bigint,@objid bigint,@cmpid int', @inssqlold,@sobjid ,@objid,@cmpid
end 
print @sql
exec drop_temp_table '#objids' 
set nocount off  
end