IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[sys_gen_joblink_updatedata]') AND type = 'P')
DROP PROCEDURE [dbo].[sys_gen_joblink_updatedata]
GO
 

CREATE procedure [dbo].[sys_gen_joblink_updatedata](
@cmpid int,
@bizid int ,
@wsid int ,
@procname varchar(100) output 
)
as
begin
set nocount on
declare @i int 
declare @s nvarchar(4000) 
declare @s1 nvarchar(500) 
declare @s2 nvarchar(500) 
declare @s3 nvarchar(500) 
declare @sql nvarchar(MAX) 
declare @sqlins1 nvarchar(max) 
declare @sqlins2 nvarchar(max) 
declare @sqlback nvarchar(max)


declare @tbname varchar(50)
declare @dbname varchar(50)
declare @fkey   varchar(50) 
exec sys_sql_getTableNamePk @cmpid,'biz',@bizid,0,@tbname output,@fkey output ,@dbname output 

declare @sqlprev nvarchar(max) -- 保存一些基础代码

declare @codei int 
set @codei = 0 
declare @sqlcenter nvarchar(max)
set @sqlcenter ='set @operation = ''continue'' '+char(13)+char(10)+
  'set @errorinfo='''' '+char(13)+char(10)+
  'set @newobjid = 0 '+char(13)+char(10)+
  'set @ident=convert(varchar(100),convert(decimal(18,11),CURRENT_TIMESTAMP)) '+char(13)+char(10)+
  'set @newsql = '''' '+char(13)+char(10)+
  'set @authority = '''' '+char(13)+char(10)+
  'set @condition = '''' '+char(13)+char(10) 
   
  
declare @sqlrear nvarchar(max) -- 清理临时数据
set @sqlrear = '' 

set @sqlprev = 'declare @operation varchar(50) '+char(13)+char(10)+
   'declare @errorinfo nvarchar(4000)  '+char(13)+char(10)+
   'declare @newobjid bigint '+char(13)+char(10)+
   'declare @ident varchar(40) '+char(13)+char(10)+
   'declare @newsql nvarchar(max) '+char(13)+char(10)+
   'declare @condition nvarchar(4000) '+char(13)+char(10) +
   'declare @authority varchar(200) '+char(13)+char(10) +
   'create table #mptypes(mpt varchar(20)) '+char(13)+char(10) +
   'declare @bcid int '+char(13)+char(10) +
   'set @bcid=@ordbcid '+char(13)+char(10)+ 
   'declare @i int,@curInd int,@strlen int,@tmps varchar(100) ,@srcs varchar(2000) '+CHAR(13)+char(10)   
   
  if (exists (select 1 from mps_flowworkstatus_data  where wsd_wsid=@wsid and wsd_use=0 and wsd_updatesql>'' and wsd_sql like '%#master%' ))
  begin
  set @sqlprev = @sqlprev + 'exec drop_temp_table ''#master'' '+char(13)+char(10)+
'select * into #master from '+@dbname+@tbname+N' where ord_id=@ordid '+char(13)+char(10) 
set @sqlrear=@sqlrear +'exec drop_temp_table ''#master'' '+char(13)+char(10) 
  end 
  if (exists (select 1 from mps_flowworkstatus_data  where wsd_wsid=@wsid and wsd_use=0 and wsd_updatesql>'' and wsd_sql like '%#romaster%' ))
  begin
  set @sqlprev = @sqlprev + 'exec drop_temp_table ''#romaster'' '+char(13)+char(10)+
'select * into #romaster from '+@dbname+@tbname+N' where ord_id=@ordid '+char(13)+char(10) 
set @sqlrear=@sqlrear +'exec drop_temp_table ''#romaster'' '+char(13)+char(10) 
  end

declare c1 cursor LOCAL FORWARD_ONLY READ_ONLY for
select distinct biz2_tag  from mps_biztype2  where biz2_bizid=@bizid 
open c1

declare @detail varchar(50) 
declare @order2 varchar(50)
declare @ord2tag2 int 
FETCH NEXT FROM c1 INTO @ord2tag2 
WHILE (@@FETCH_STATUS = 0)
BEGIN
set @detail = '#detail'+convert(varchar(10),@ord2tag2) 
set @order2 = @dbname+@tbname+'_'+convert(varchar(10),@ord2tag2) 
if (exists (select 1 from mps_flowworkstatus_data  where wsd_wsid=@wsid and wsd_use=0 and wsd_updatesql>'' and wsd_sql like '%#'+@detail+'%' ))
  begin
  set @sqlprev = @sqlprev + 'exec drop_temp_table '''+@detail+''' '+char(13)+char(10)+
'select * into '+@detail+' from '+@order2+N' where ord2_ordid=@ordid '+char(13)+char(10) 
set @sqlrear=@sqlrear +'exec drop_temp_table '''+@detail+''' '+char(13)+char(10) 
  end
  set @detail = '#rodetail'+convert(varchar(10),@ord2tag2) 
  if (exists (select 1 from mps_flowworkstatus_data  where wsd_wsid=@wsid and wsd_use=0 and wsd_updatesql>'' and wsd_sql like '%#'+@detail+'%' ))
  begin
  set @sqlprev = @sqlprev + 'exec drop_temp_table '''+@detail+''' '+char(13)+char(10)+
'select * into '+@detail+' from '+@order2+N' where ord2_ordid=@ordid '+char(13)+char(10) 
set @sqlrear=@sqlrear +'exec drop_temp_table '''+@detail+''' '+char(13)+char(10) 
  end
 FETCH NEXT FROM c1 INTO @ord2tag2 
END
CLOSE c1
DEALLOCATE c1 


set @sql = N'IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(''[dbo].[order_'+ltrim(rtrim(str(@bizid)))+
'_'++N'dolinkdata_'+ltrim(rtrim(str(@wsid)))+N']'') AND type in (''P'', ''PC''))
DROP PROCEDURE [dbo].[order_'+ltrim(rtrim(str(@bizid)))+'_'+N'dolinkdata_'+ltrim(rtrim(str(@wsid)))+'] ' 
exec(@sql)

declare @f table(biztype varchar(10),bizid int, ord2tag int) 
insert into @f(biztype,bizid,ord2tag ) 
select bs_stype , bs_sbizid , bs_sord2tag 
from mps_biztype_comefrom
where bs_bizid=@bizid and bs_ord2tag=0 and 
(exists (select * from mps_biztype_comefrom_fieldmap where bsf_bsid=bs_id and bsf_use=0 ))


declare @t table(biztype varchar(10),bizid int, ord2tag int , level int , mybizid int) 
declare @tt table(bid int) 
-- 寻找所有的上级，包括根级
insert into @t(biztype,bizid,ord2tag , level ,mybizid) 
select bs_stype , bs_sbizid , bs_sord2tag , 0 , -1 
from mps_biztype_comefrom
where bs_bizid=@bizid and bs_ord2tag=0 and 
(exists (select * from mps_biztype_comefrom_fieldmap where bsf_bsid=bs_id and bsf_use=0 ))

-- 寻找父级的上级
insert into @t(biztype,bizid,ord2tag,level,mybizid) 
select bs_stype , bs_sbizid , bs_sord2tag , 1 , bizid 
from mps_biztype_comefrom , @t 
where bs_bizid=bizid and bs_ord2tag=ord2tag and biztype='biz' and level=0 and 
(exists (select * from mps_biztype_comefrom_fieldmap where bsf_bsid=bs_id and bsf_use=0 )) 

delete @tt 
insert into @tt(bid) select mybizid from @t where level=1 
 
-- 寻找父级的上级
insert into @t(biztype,bizid,ord2tag,level,mybizid) 
select bs_stype , bs_sbizid , bs_sord2tag , 2 , bizid 
from mps_biztype_comefrom , @t 
where bs_bizid=bizid and bs_ord2tag=ord2tag and biztype='biz' and level=1 and 
(exists (select * from mps_biztype_comefrom_fieldmap where bsf_bsid=bs_id and bsf_use=0 )) 

delete @tt 
insert into @tt(bid) select mybizid from @t where level=2 
 

-- 寻找父级的上级
insert into @t(biztype,bizid,ord2tag,level,mybizid) 
select bs_stype , bs_sbizid , bs_sord2tag , 3 , bizid 
from mps_biztype_comefrom , @t 
where bs_bizid=bizid and bs_ord2tag=ord2tag and biztype='biz' and level=2 and 
(exists (select * from mps_biztype_comefrom_fieldmap where bsf_bsid=bs_id and bsf_use=0 )) 

delete @tt 
insert into @tt(bid) select mybizid from @t where level=3 
 
-- 寻找父级的上级
insert into @t(biztype,bizid,ord2tag,level,mybizid) 
select bs_stype , bs_sbizid , bs_sord2tag , 4 , bizid 
from mps_biztype_comefrom , @t 
where bs_bizid=bizid and bs_ord2tag=ord2tag and biztype='biz' and level=3 and 
(exists (select * from mps_biztype_comefrom_fieldmap where bsf_bsid=bs_id and bsf_use=0 )) 

delete @tt 
insert into @tt(bid) select mybizid from @t where level=4 

-- @f 表中，是当前业务，所有可能的上级或根级
declare @r table(biztype varchar(10),bizid int, ord2tag int ) 
insert into @r(biztype,bizid, ord2tag)
select distinct biztype,bizid, ord2tag from @t where mybizid>=0 
  

declare @targ table(ttype varchar(20),tbizid int,tord2tag int,sord2tag int,updatesql nvarchar(4000),sflag varchar(10),extsql nvarchar(4000),keyfld varchar(50) , sqlins1 nvarchar(4000) , sqlins2 nvarchar(4000),sqlback nvarchar(4000)) 

 

declare status1 cursor LOCAL FORWARD_ONLY READ_ONLY for
select wsd_tobjtype,wsd_tobjid,wsd_tobjid2,wsd_sord2tag,wsd_updatesql,wsd_sql,wsd_keyfield  
from mps_flowworkstatus_data  where wsd_wsid=@wsid and wsd_use=0
open status1
declare @wsd_tobjtype varchar(20)
declare @wsd_tobjid int 
declare @wsd_tobjid2 int  
declare @wsd_sord2tag int
declare @wsd_updatesql nvarchar(max)  

declare @wsd_sql nvarchar(max)  
declare @wsd_keyfield varchar(50)  
FETCH NEXT FROM status1 INTO @wsd_tobjtype,@wsd_tobjid,@wsd_tobjid2,@wsd_sord2tag,@wsd_updatesql,@wsd_sql,@wsd_keyfield 
WHILE (@@FETCH_STATUS = 0)
BEGIN
-- 生成修改串 ord_cbid=0=new.ord_cbid|||ord_cmpid=0=new.公司ID|||ord_createcuid=0=new.ord_createcuid|||ord_createdepart=0=new.ord_createdepart
set @s = @wsd_updatesql
-- 在这里完善一下 
if (CHARINDEX('ord_updated=',@s)<=0 and (CHARINDEX('ord_',@s)=1 or CHARINDEX('|ord_',@s)>0)) 
begin
set @s = 'ord_updated=0=getdate()|||'+@s 
end 
if (CHARINDEX('cc_activetime=',@s)<=0 and (CHARINDEX('cc_',@s)=1 or CHARINDEX('|cc_',@s)>0)) 
begin
set @s = 'cc_activetime=0=getdate()|||'+@s 
end 
if (CHARINDEX('cc2_activetime=',@s)<=0 and (CHARINDEX('cc2_',@s)=1 or CHARINDEX('|cc2_',@s)>0)) 
begin
set @s = 'cc2_activetime=0=getdate()|||'+@s 
end 
if (CHARINDEX('ord2_updated=',@s)<=0 and (CHARINDEX('ord2_',@s)=1 or CHARINDEX('|ord2_',@s)>0)) 
begin
set @s = 'ord2_updated=0=getdate()|||'+@s 
end 
if (CHARINDEX('prd_updated=',@s)<=0 and (CHARINDEX('prd_',@s)=1 or CHARINDEX('|prd_',@s)>0)) 
begin
set @s = 'prd_updated=0=getdate()|||'+@s 
end 
if (CHARINDEX('prd2_updated=',@s)<=0 and (CHARINDEX('prd2_',@s)=1 or CHARINDEX('|prd2_',@s)>0)) 
begin
set @s = 'prd2_updated=0=getdate()|||'+@s 
end 
set @sql ='' 
set @sqlins1 = '' 
set @sqlins2 = '' 
set @sqlback = '' 
-- @tord2tag=999999999
while (@s>'') 
begin
set @i = charindex('|||',@s) 
if (@i>0) 
begin
set @s1 = substring(@s,1,@i-1) 
set @s =substring(@s,@i+3,len(@s)-@i-2) 
end else 
begin
set @s1 = @s 
set @s = '' 
end
set @i = charindex('=',@s1) 
if (@i>0) 
begin
set @s2 = substring(@s1,1,@i-1) 
set @s1 = substring(@s1 , @i+1 , len(@s1)-@i) 
set @i = charindex('=',@s1) 
if (@i>0) 
begin
set @s3 = substring(@s1,1,@i-1) 
set @s1 = substring(@s1 , @i+1 , len(@s1)-@i) 
set @s3=isnull(@s3,'0') 
if (@s3='') set @s3='0' 

if (@s1='当前时间') set @s1='GETDATE()' 
if (@s1='当前日期') set @s1='GETDATE()' 
if (@s1='now') set @s1='GETDATE()' 
if (@s1='today') set @s1='GETDATE()' 


if (@wsd_tobjid2=999999999)  -- 是新建
begin
if (@s2='cu_id' or @s2='dp_id' or @s2='cc_id' or @s2='cc_ctid' or @s2='cc_tag' or 
@s2='prd_id' or @s2='prd_pcid'  or @s2='prd_tag' or 
@s2='equ_id' or @s2='equ_pcid'  or @s2='equ_tag' or 
@s2='doc_id' or @s2='doc_dcid') 
begin
if (CHARINDEX('ord_',@s1)>0 or CHARINDEX('ord2_',@s1)>0) 
begin
if (@sqlback>'') set @sqlback = @sqlback +' , ' 
set @sqlback = @sqlback+' '+@s1+'=old.'+@s2 
end 
if (@sqlins1>'') set @sqlins1 = @sqlins1 +' , ' 
if (@sqlins2>'') set @sqlins2 = @sqlins2 +' , ' 
if (@sql>'') set @sql = @sql +' , ' 
if (@s3='0') set @sql = @sql +' old.'+@s2+'='+@s1 
if (@s3='1') set @sql = @sql +' old.'+@s2+'=old.'+@s2+'+'+@s1 
if (@s3='-1') set @sql = @sql +' old.'+@s2+'=old.'+@s2+'-'+@s1 
set @sqlins1 = @sqlins1 +' '+@s2 
set @sqlins2 = @sqlins2 +' '+@s1 
end else
begin
if (@sqlins1>'') set @sqlins1 = @sqlins1 +' , ' 
if (@sqlins2>'') set @sqlins2 = @sqlins2 +' , ' 
if (@sql>'') set @sql = @sql +' , ' 
if (@s3='0') set @sql = @sql +' old.'+@s2+'='+@s1 
if (@s3='1') set @sql = @sql +' old.'+@s2+'=old.'+@s2+'+'+@s1 
if (@s3='-1') set @sql = @sql +' old.'+@s2+'=old.'+@s2+'-'+@s1 
set @sqlins1 = @sqlins1 +' '+@s2 
set @sqlins2 = @sqlins2 +' '+@s1 
end 
end else 
if (@wsd_tobjid2=0 )
begin
if (@sqlins1>'') set @sqlins1 = @sqlins1 +' , ' 
if (@sqlins2>'') set @sqlins2 = @sqlins2 +' , ' 
if (@sql>'') set @sql = @sql +' , ' 
if (@s3='0') set @sql = @sql +' old.'+@s2+'='+@s1 
if (@s3='1') set @sql = @sql +' old.'+@s2+'=old.'+@s2+'+'+@s1 
if (@s3='-1') set @sql = @sql +' old.'+@s2+'=old.'+@s2+'-'+@s1 
set @sqlins1 = @sqlins1 +' '+@s2 
set @sqlins2 = @sqlins2 +' '+@s1 
end else 
begin
if (@sqlins1>'') set @sqlins1 = @sqlins1 +' , ' 
if (@sqlins2>'') set @sqlins2 = @sqlins2 +' , ' 
if (@sql>'') set @sql = @sql +' , ' 
if (@s3='0') set @sql = @sql +' old2.'+@s2+'='+@s1 
if (@s3='1') set @sql = @sql +' old2.'+@s2+'=old2.'+@s2+'+'+@s1 
if (@s3='-1') set @sql = @sql +' old2.'+@s2+'=old2.'+@s2+'-'+@s1 
set @sqlins1 = @sqlins1 +' '+@s2 
set @sqlins2 = @sqlins2 +' '+@s1 
end
end 
end
end
if (@sql>'') 
begin
 if (@wsd_tobjtype='biz' and @wsd_tobjid=-1) -- father 
 begin
 insert into @targ(ttype,tbizid,tord2tag,sord2tag,updatesql,sflag,extsql,keyfld,sqlins1,sqlins2,sqlback)
 select biztype,bizid, ord2tag ,0, @sql , 'f' , @wsd_sql , '' ,@sqlins1,@sqlins2,@sqlback
 from @f 
 end else 
 if (@wsd_tobjtype='biz' and @wsd_tobjid=-3) -- root
 begin
 insert into @targ(ttype,tbizid,tord2tag,sord2tag,updatesql,sflag,extsql,keyfld,sqlins1,sqlins2,sqlback)
 select biztype,bizid, ord2tag ,0, @sql , 'r' , @wsd_sql ,'' ,@sqlins1,@sqlins2,@sqlback
 from @r 
 end else 
 if (@wsd_tobjid>=0) -- 其它源
 begin
 insert into @targ(ttype,tbizid,tord2tag,sord2tag,updatesql,sflag,extsql,keyfld,sqlins1,sqlins2,sqlback)
 select @wsd_tobjtype,@wsd_tobjid,@wsd_tobjid2 ,@wsd_sord2tag, @sql , '' , @wsd_sql ,@wsd_keyfield ,@sqlins1,@sqlins2,@sqlback
 end
end
 FETCH NEXT FROM status1 INTO @wsd_tobjtype,@wsd_tobjid,@wsd_tobjid2,@wsd_sord2tag,@wsd_updatesql,@wsd_sql,@wsd_keyfield 
END
CLOSE status1
DEALLOCATE status1 

-- 纠正一下father root 
update @targ set sflag='f' 
from @f where sflag='' and sord2tag=ord2tag and tbizid=bizid and biztype=ttype 
update @targ set sflag='r' 
from @r where sflag='' and sord2tag=ord2tag and tbizid=bizid and biztype=ttype 

 
-- 开始生成代码
declare @mystbname varchar(50) , @mysdbname varchar(50), @myskeyfld varchar(50) 
declare @myttbname varchar(50) , @mytdbname varchar(50), @mytkeyfld varchar(50) 
declare @myttb2name varchar(50) , @mytdb2name varchar(50), @mytkey2fld varchar(50) 

update @targ  set sflag='' where keyfld>'' 

declare status11 cursor LOCAL FORWARD_ONLY READ_ONLY for
select ttype,tbizid,tord2tag,sord2tag,updatesql,sflag,extsql,keyfld,sqlins1,sqlins2,sqlback   from @targ  
open status11


 

declare @ttype varchar(20),@tbizid int,@tord2tag int,@sord2tag int,@updatesql nvarchar(4000),@sflag varchar(10),@extsql nvarchar(4000),@keyfld varchar(50)

-- select * from @targ 

set @codei = 0 

declare @nsql nvarchar(MAX) 
set @nsql = @sqlprev   
FETCH NEXT FROM status11 INTO @ttype,@tbizid,@tord2tag,@sord2tag,@updatesql,@sflag,@extsql,@keyfld,@sqlins1,@sqlins2 ,@sqlback
WHILE (@@FETCH_STATUS = 0)
BEGIN
exec sys_sql_getTableNamePk @cmpid , 'biz',@bizid,0,@mystbname output,@myskeyfld output,@mysdbname output
   if (@mysdbname>'') set @mystbname = @mysdbname+@mystbname 
   
   exec sys_sql_getTableNamePk @cmpid , @ttype,@tbizid,0,@myttbname output,@mytkeyfld output,@mytdbname output
   
   exec sys_sql_getTableNamePk @cmpid , @ttype,@tbizid,@tord2tag,@myttb2name output,@mytkey2fld output,@mytdb2name output
   
   if (@mytdbname>'') set @myttbname = @mytdbname+@myttbname 
   
   
   if (LEN(@extsql)>5) set @codei = @codei + 1 
set @sql = CHAR(13)+char(10)
-- 在执行以下代码时，要先执行扩展代码
set @sql = @sql + @sqlcenter + 
'-- 以下是用户扩展的程序  ---- '+CHAR(13)+char(10)  + 
replace(@extsql,'#target',@myttbname)+CHAR(13)+char(10)  + 
'-- 以上是用户扩展的程序  ---- '+CHAR(13)+char(10)  + 
' '+CHAR(13)+char(10)  + 

'set @srcs = @authority  '+CHAR(13)+char(10)   
if (@ttype='emp') 
begin
set @sql = @sql +
'if (@srcs='''') set @srcs=''myempview'' '+CHAR(13)+CHAR(10) 
end 
if (@ttype='doc') 
begin
set @sql = @sql +
'if (@srcs='''') set @srcs=''mydocview'' '+CHAR(13)+CHAR(10) 
end 
if (@ttype='prd') 
begin
set @sql = @sql +
'if (@srcs='''') set @srcs=''myprdview'' '+CHAR(13)+CHAR(10) 
end 
if (@ttype='equ') 
begin
set @sql = @sql +
'if (@srcs='''') set @srcs=''myequview'' '+CHAR(13)+CHAR(10) 
end 
if (@ttype='biz') 
begin
set @sql = @sql +
'if (@srcs='''') set @srcs=''mybizview'' '+CHAR(13)+CHAR(10) 
end 
if (@ttype='dpt') 
begin
set @sql = @sql +
'if (@srcs='''') set @srcs=''mydptview'' '+CHAR(13)+CHAR(10) 
end 
if (@ttype='shp') 
begin
set @sql = @sql +
'if (@srcs='''') set @srcs=''myshpview'' '+CHAR(13)+CHAR(10) 
end 
set @sql = @sql +
'set @authority=@srcs  '+CHAR(13)+char(10)  + 
'select @curInd=1,@strlen=LEN(@srcs)  '+CHAR(13)+char(10)  + 
'while (@curInd<=@strlen)  '+CHAR(13)+char(10)  + 
'begin  '+CHAR(13)+char(10)  + 
'select @i=charindex('','',@srcs,@curInd)  '+CHAR(13)+char(10)  + 
'if (@i>0)  '+CHAR(13)+char(10)  + 
'begin  '+CHAR(13)+char(10)  + 
'select @tmps=SUBSTRING(@srcs,@curInd,@i-@curInd)  '+CHAR(13)+char(10)  + 
'set @curInd=@i+1  '+CHAR(13)+char(10)  + 
'end else if (@curInd<=@strlen) '+CHAR(13)+char(10)  +  
'begin  '+CHAR(13)+char(10)  + 
'select @tmps=SUBSTRING(@srcs,@curInd,@strlen-@curInd+1)  '+CHAR(13)+char(10)  + 
'set @curInd=@strlen+1  '+CHAR(13)+char(10)  + 
'end  '+CHAR(13)+char(10)  + 
'if (@tmps>'''') insert into #mptypes(mpt) values(@tmps)  '+CHAR(13)+char(10)  + 
'end '+CHAR(13)+char(10)  + 

'if (@operation = ''continue'') '+CHAR(13)+char(10)  + 
'begin '+CHAR(13)+char(10)    

if (@sflag='f')
begin
set @sql = @sql +CHAR(13)+char(10)+ N'
if (@ftype='''+@ttype+''' and @fbizid='+ltrim(rtrim(str(@tbizid)))+
' and @ford2tag='+ltrim(rtrim(str(@tord2tag)))+') ' 
end 
if (@sflag='r')
begin
set @sql = @sql +CHAR(13)+char(10)+ N'
if (@rtype='''+@ttype+''' and @rbizid='+ltrim(rtrim(str(@tbizid)))+
' and @rord2tag='+ltrim(rtrim(str(@tord2tag)))+') ' 
end 
set @sql = @sql +N'
'

set @sql = @sql+'set @newsql=''' 

if (@tord2tag=999999999) 
begin
 -- 这里要写插入语句，根据插入的结果来执行修改语句 
 if (@ttype='emp')  -- dpt shp emp cst prd equ doc  etc user 
 begin
set @codei = @codei + 1
set @sql = @sql + CHAR(13)+char(10) +
'if (@newobjid<=0) '+CHAR(13)+char(10) +
'begin '+CHAR(13)+char(10) +
'insert into mps_employee(cu_cmpid,cu_bcid,cu_branch,cu_dpid,cu_depart,cu_code,cu_name) '+CHAR(13)+char(10) +
'select @cmpid,@ordbcid,bc_company,0,''''未设置'''',@ident,''''新员工'''' from mps_branch where bc_id=@ordbcid '+CHAR(13)+char(10) +
'select top 1 @newobjid = dp_id from mps_employee where cu_cmpid=@cmpid and cu_bcid=@ordbcid and cu_depart=''''未设置'''' and cu_code=@ident '+CHAR(13)+char(10) +
'set @newobjid = ISNULL(@newobjid,0) '+CHAR(13)+char(10)  
if (@sqlback>'') 
begin

if (@sord2tag=0) 
 begin
 set @sql = @sql +
 'update new set '+@sqlback+' from '+@mystbname+' new , mps_employee old where cu_id=@newobjid and ord_id=@ordid '+CHAR(13)+char(10) 
end else 
begin
set @sql = @sql +
 'update new set '+@sqlback+' from '+@mystbname+'_'+ltrim(rtrim(str(@sord2tag)))+' new , mps_employee old where cu_id=@newobjid and ord2_ordid=@ordid '+CHAR(13)+char(10) 
end
end 
set @sql = @sql + 
'update mps_employee set cu_depart=dp_depart ,cu_bcid=dp_bcid  from mps_depart where cu_id=@newobjid and dp_id=cu_dpid '+CHAR(13)+char(10)+
'update mps_employee set cu_branch=bc_company from mps_groupbranch where cu_id=@newobjid and bc_id=cu_bcid '+CHAR(13)+char(10)+
'end '+CHAR(13)+char(10) 
 end 
 if (@ttype='shp')  -- dpt shp emp cst prd equ doc  etc user 
 begin
set @codei = @codei + 1
set @sql = @sql + CHAR(13)+char(10) +
'if (@newobjid<=0) '+CHAR(13)+char(10) +
'begin '+CHAR(13)+char(10) +
'insert into mps_depart(dp_cmpid,dp_bcid,dp_code,dp_depart,dp_fullname,dp_order,dp_dpstdid,dp_display,dp_isdepart) '+CHAR(13)+char(10) +
'select @cmpid,@ordbcid,@ident,''''新门店'''',''''新门店'''',9999,0,''''新门店'''',1  '+CHAR(13)+char(10) +
'select top 1 @newobjid = dp_id from mps_depart where dp_cmpid=@cmpid and dp_bcid=@ordbcid and dp_code=@ident and dp_isdepart=1 '+CHAR(13)+char(10) +
'set @newobjid = ISNULL(@newobjid,0) '+CHAR(13)+char(10) 
if (@sqlback>'') 
begin

if (@sord2tag=0) 
 begin
 set @sql = @sql +
 'update new set '+@sqlback+' from '+@mystbname+' new , mps_depart old where dp_id=@newobjid and ord_id=@ordid '+CHAR(13)+char(10) 
end else 
begin
set @sql = @sql +
 'update new set '+@sqlback+' from '+@mystbname+'_'+ltrim(rtrim(str(@sord2tag)))+' new , mps_depart old where dp_id=@newobjid and ord2_ordid=@ordid '+CHAR(13)+char(10) 
end
end 
set @sql = @sql + 
'end '+CHAR(13)+char(10)

 end 
 if (@ttype='dpt')  -- dpt shp emp cst prd equ doc  etc user 
 begin
set @codei = @codei + 1
set @sql = @sql + CHAR(13)+char(10) +
'if (@newobjid<=0) '+CHAR(13)+char(10) +
'begin '+CHAR(13)+char(10) +
'insert into mps_depart(dp_cmpid,dp_bcid,dp_code,dp_depart,dp_fullname,dp_order,dp_dpstdid,dp_display,dp_isdepart) '+CHAR(13)+char(10) +
'select @cmpid,@ordbcid,@ident,''''新门店'''',''''新门店'''',9999,0,''''新门店'''',1  '+CHAR(13)+char(10) +
'select top 1 @newobjid = dp_id from mps_depart where dp_cmpid=@cmpid and dp_bcid=@ordbcid and dp_code=@ident and dp_isdepart=0 '+CHAR(13)+char(10) +
'set @newobjid = ISNULL(@newobjid,0) '+CHAR(13)+char(10) 
if (@sqlback>'') 
begin

if (@sord2tag=0) 
 begin
 set @sql = @sql +
 'update new set '+@sqlback+' from '+@mystbname+' new ,mps_depart old where dp_id=@newobjid and ord_id=@ordid '+CHAR(13)+char(10) 
end else 
begin
set @sql = @sql +
 'update new set '+@sqlback+' from '+@mystbname+'_'+ltrim(rtrim(str(@sord2tag)))+' new,mps_depart old where dp_id=@newobjid and ord2_ordid=@ordid '+CHAR(13)+char(10) 
end
end 
set @sql = @sql + 
'end '+CHAR(13)+char(10)

 end
 if (@ttype='cst')  -- dpt shp emp cst prd equ doc  etc user 
 begin
set @codei = @codei + 1
set @sql = @sql + CHAR(13)+char(10) +
'if (@newobjid<=0) '+CHAR(13)+char(10) +
'begin '+CHAR(13)+char(10) +
'insert into '+@myttbname+'(cc_cmpid,cc_bcid,cc_branch,cc_dpid,cc_depart,cc_tag,cc_caption,cc_ctid,cc_type,cc_code,'+
'cc_createcuid,cc_creator,cc_cuid,cc_cuname,cc_name) '+CHAR(13)+char(10) +
'select distinct @cmpid,cu_bcid,cu_branch,cu_dpid,cu_depart,'+convert(varchar(10),@tbizid)+
',ct_caption,0,'''''''',@ident,cu_id,cu_callname,cu_id,cu_callname,''''未录入'''' from mps_login,mps_csttype where ct_cmpid=@cmpid and ct_tag='+convert(varchar(10),@tbizid)+' and  cu_id=@useruid '+CHAR(13)+char(10) +
'select top 1 @newobjid = cc_id from '+@myttbname+' where cc_cmpid=@cmpid and cc_code=@ident order by cc_id desc '+CHAR(13)+char(10) +
'set @newobjid = ISNULL(@newobjid,0) '+CHAR(13)+char(10)  +
'delete '+@myttbname+'_users where ccp_ccid=@newobjid '+CHAR(13)+char(10)  +
'if (not exists (select 1  from '+@myttbname+'_users  where ccp_type=''''cstview'''' and ccp_ccid=@newobjid and ccp_cuid=@useruid)) insert into '+@myttbname+'_users(ccp_cuid,ccp_ccid,ccp_type,ccp_otid) values(@useruid,@newobjid,''''cstview'''',-1) '+CHAR(13)+char(10)  +
'if (not exists (select 1 from '+@myttbname+'_users   where ccp_type=''''cstedit'''' and ccp_ccid=@newobjid and ccp_cuid=@useruid)) insert into '+@myttbname+'_users(ccp_cuid,ccp_ccid,ccp_type,ccp_otid) values(@useruid,@newobjid,''''cstedit'''',-1) '+CHAR(13)+char(10)  +
'end '+CHAR(13)+char(10) +
'if (@authority>'''''''') '+CHAR(13)+char(10) +
'begin '+CHAR(13)+char(10) +
'set @authority = '''',''''+@authority+'''',''''  '+CHAR(13)+char(10) +
'insert into '+@myttbname+'_users(ccp_cuid,ccp_ccid,ccp_otid,ccp_type) '+CHAR(13)+char(10) +
'select distinct ordp_cuid,@newobjid,0,''''cstview'''' '+CHAR(13)+char(10) +
'from '+@mystbname+'_users  '+CHAR(13)+char(10) +
'where ordp_ordid=@ordid and  @authority like ''''%,''''+ordp_type +'''',%'''' and  '+CHAR(13)+char(10) +
'not exists (select 1 from '+@myttbname+'_users where ccp_cuid=ordp_cuid and ccp_ccid=@newobjid and ccp_type=''''cstview'''') '+CHAR(13)+char(10)  
if (@sqlback>'') 
begin

if (@sord2tag=0) 
 begin
 set @sql = @sql +
 'update new set '+@sqlback+' from '+@mystbname+' new ,'+@myttbname+' old where cc_id=@newobjid and ord_id=@ordid '+CHAR(13)+char(10) 
end else 
begin
set @sql = @sql +
 'update new set '+@sqlback+' from '+@mystbname+'_'+ltrim(rtrim(str(@sord2tag)))+' new ,'+@myttbname+' old where cc_id=@newobjid and ord2_ordid=@ordid '+CHAR(13)+char(10) 
end
end 
set @sql = @sql + 
'update  '+@myttbname+' set cc_tag=ct_tag,cc_caption=ct_caption,cc_type=ct_type from mps_csttype where cc_id=@newobjid and cc_ctid=ct_id '+CHAR(13)+char(10)+
'update  '+@myttbname+' set cc_depart=dp_depart,cc_bcid=dp_bcid  from mps_depart where cc_id=@newobjid and cc_dpid=dp_id '+CHAR(13)+char(10)+
'update  '+@myttbname+' set cc_branch=bc_company  from mps_groupbranch where cc_id=@newobjid and cc_bcid=bc_id '+CHAR(13)+char(10)+
'update  '+@myttbname+' set cc_creator=cu_name  from mps_employee where cc_id=@newobjid and cc_createcuid=cu_id '+CHAR(13)+char(10)+
'update  '+@myttbname+' set cc_cuname=cu_name  from mps_employee where cc_id=@newobjid and cc_cuid=cu_id '+CHAR(13)+char(10)+
'end '+CHAR(13)+char(10)
 end 
 
 if (@ttype='doc')  -- dpt shp emp cst prd equ doc  etc user 
 begin
set @codei = @codei + 1
set @sql = @sql + CHAR(13)+char(10) +
'if (@newobjid<=0) '+CHAR(13)+char(10) +
'begin '+CHAR(13)+char(10) +
'insert into '+@myttbname+'(doc_cmpid,doc_bcid,doc_branch,doc_dpid,doc_depart,doc_tag,doc_cuid,doc_creator,doc_no,doc_title,doc_dcid,doc_security,doc_distroy,doc_deliver,doc_archive) '+CHAR(13)+char(10) +
'select distinct  @cmpid,cu_bcid,cu_branch,cu_dpid,cu_depart,'+convert(varchar(10),@tbizid)+
',cu_id,cu_callname,@ident,''''未录入'''',0 ,5,getdate()+3000,0,0 from mps_login where  cu_id=@useruid '+CHAR(13)+char(10) +
'select top 1 @newobjid = doc_id from '+@myttbname+' where doc_cmpid=@cmpid and doc_no=@ident order by doc_id desc '+CHAR(13)+char(10) +
'set @newobjid = ISNULL(@newobjid,0) '+CHAR(13)+char(10)  +
'end '+CHAR(13)+char(10) + 
'insert into mps_document_text(docd_docid,docd_text)
select @newobjid , ord3_text from '+@mystbname+'_0 with(nolock) where ord3_ordid=@ordid and ord3_idname=''''context'''' '+CHAR(13)+char(10)  + 
'insert into mps_attach(da_cmpid,da_bcid,da_dpid,da_type,da_objid,da_objid2,da_categoryid,da_category,da_security,
da_filename,da_size,da_ext,da_date,da_cuid,da_uploader,da_realname,da_md5,da_device,da_destory,
da_icopath,da_originname,da_uploaduuid,da_currentchunks,da_success,da_fixpath,da_preview) 
select da_cmpid,da_bcid,da_dpid,''''doc'''',@newobjid,0,da_categoryid,da_category,da_security,
da_filename,da_size,da_ext,da_date,da_cuid,da_uploader,da_realname,da_md5,da_device,da_destory,
da_icopath,da_originname,da_uploaduuid,da_currentchunks,da_success,da_fixpath,da_preview 
from mps_attach with(nolock) where da_cmpid=@cmpid and da_type=''''biz''''+convert(varchar(10),@bizid) and da_objid=@ordid '+char(13)+char(10)+
'insert into mps_document_permission(docp_docid,docp_type,docp_ppid,docp_objid1,docp_objid2) '+CHAR(13)+char(10) + 
'select distinct @newobjid,''''doc'+convert(varchar(10),@bizid)+'''''+case when charindex(''''>'''',mp_type)>0 then substring(mp_type,3,charindex(''''>'''',mp_type)-1) else substring(mp_type,3,20) end,mp_ppid,mp_objid1,mp_objid2 '+CHAR(13)+char(10) + 
'from mps_permission with(nolock) , #mptypes  where mp_cmpid=@cmpid and mp_type=mpt+''''>'+convert(varchar(10),@bizid)+''''' and mp_ctgid=@ordid  '+CHAR(13)+char(10) + 
'update '+@mystbname+' with(rowlock) set ord_sobjid=@newobjid where ord_id=@ordid '+CHAR(13)+char(10) +
'if (@authority>'''''''') '+CHAR(13)+char(10) +
'begin '+CHAR(13)+char(10) +
'insert into '+@myttbname+'_users(docp_cuid,docp_docid,docp_otid,docp_type) '+CHAR(13)+char(10) +
'select distinct ordp_cuid,@newobjid,-1,substring(mpt,3,20) '+CHAR(13)+char(10) +
'from '+@mystbname+'_users , #mptypes '+CHAR(13)+char(10) +
'where ordp_ordid=@ordid and  ordp_type=mpt and  '+CHAR(13)+char(10) +
'not exists (select 1 from '+@myttbname+'_users where docp_cuid=ordp_cuid and docp_docid=@newobjid and docp_type=substring(mpt,3,20)) '+CHAR(13)+char(10)  
if (@sqlback>'') 
begin

if (@sord2tag=0) 
 begin
 set @sql = @sql +
 'update new set '+@sqlback+' from '+@mystbname+' new,'+@myttbname+' old where doc_id=@newobjid and ord_id=@ordid '+CHAR(13)+char(10) 
end else 
begin
set @sql = @sql +
 'update new set '+@sqlback+' from '+@mystbname+'_'+ltrim(rtrim(str(@sord2tag)))+' new,'+@myttbname+' old where doc_id=@newobjid and ord2_ordid=@ordid '+CHAR(13)+char(10) 
end
end 
set @sql = @sql + 
'update  '+@myttbname+' set doc_depart=dp_depart,doc_bcid=dp_bcid  from mps_depart where doc_id=@newobjid and doc_dpid=dp_id '+CHAR(13)+char(10)+
'update  '+@myttbname+' set doc_branch=bc_company  from mps_groupbranch where doc_id=@newobjid and doc_bcid=bc_id '+CHAR(13)+char(10)+
'update  '+@myttbname+' set doc_creator=cu_name  from mps_employee where doc_id=@newobjid and doc_cuid=cu_id '+CHAR(13)+char(10)+
'end '+CHAR(13)+char(10)
 end 
  
end 

if (LEN(@updatesql)>5) set @codei = @codei + 1
set @sql = @sql + CHAR(13)+char(10) 
 
if (@tord2tag=0 or @tord2tag=999999999)  set @sql = @sql + 
'update old set ' + replace(@updatesql,'''','''''')  +N'
from '  
else set @sql = @sql + N'
update old2 set ' + replace(@updatesql,'''','''''')  +N'
from '  
  
 
   -- 源，实际上就是当前表 order  new 
 if (@sord2tag=0) 
 begin
 set @sql = @sql +@mystbname +' new '
end else 
begin
set @sql = @sql +@mystbname +' new , '+@mystbname+'_'+ltrim(rtrim(str(@sord2tag)))+' new2 ' 
end


if (@tord2tag=0 or @tord2tag=999999999) 
 begin
 set @sql = @sql +','+@myttbname +' old '
end else 
begin
set @sql = @sql +','+@myttbname+'_'+ltrim(rtrim(str(@tord2tag)))+' old2 ' 
end

set @sql = @sql +N'
where '

if (@tord2tag=999999999)
begin
set @sql = @sql +' '+@mytkeyfld+'=@newobjid and '
end 
if (@sord2tag=0) 
 begin
 set @sql = @sql +' new.ord_id=@ordid '
end else 
begin
set @sql = @sql +' new.ord_id=@ordid and new2.ord2_ordid=@ordid ' 
end

set @sql = @sql +N'
'
  
if (@sflag='f') 
begin
-- 现在系统 对于 f r 的支持，只限于主表 
if (@tord2tag=0) 
begin
if (@sord2tag=0) 
begin
if (@keyfld>'') 
set @sql = @sql +' and old.'+@mytkeyfld+'=new.'+@keyfld
else 
set @sql = @sql +' and old.'+@mytkeyfld+'=new.ord_fordid'
end else
begin
if (@keyfld>'') 
set @sql = @sql +' and old.'+@mytkeyfld+'=new2.'+@keyfld
else 
set @sql = @sql +' and old.'+@mytkeyfld+'=new2.ord_ford2id'
end 
end 
else 
begin
if (@sord2tag=0) 
begin
if (@keyfld>'') 
set @sql = @sql +' and old2.'+@mytkey2fld+'=new.'+@keyfld 
else 
set @sql = @sql +' and old2.'+@mytkey2fld+'=new.ord_fordid'
end else 
begin
if (@keyfld>'') 
set @sql = @sql +' and old2.'+@mytkey2fld+'=new2.'+@keyfld
else 
set @sql = @sql +' and old2.'+@mytkey2fld+'=new2.ord2_ford2id'
end
end 
 
end else 
if (@sflag='r') 
begin
if (@tord2tag=0) 
begin
if (@sord2tag=0) 
begin
if (@keyfld>'') 
set @sql = @sql +' and old.'+@mytkeyfld+'=new.'+@keyfld
else 
set @sql = @sql +' and old.'+@mytkeyfld+'=new.ord_rordid'
end else
begin
if (@keyfld>'') 
set @sql = @sql +' and old.'+@mytkeyfld+'=new2.'+@keyfld
else 
set @sql = @sql +' and old.'+@mytkeyfld+'=new2.ord_rord2id'
end 
end 
else 
begin
if (@sord2tag=0) 
begin
if (@keyfld>'') 
set @sql = @sql +' and old2.'+@mytkey2fld+'=new.'+@keyfld 
else 
set @sql = @sql +' and old2.'+@mytkey2fld+'=new.ord_rordid'
end else 
begin
if (@keyfld>'') 
set @sql = @sql +' and old2.'+@mytkey2fld+'=new2.'+@keyfld
else 
set @sql = @sql +' and old2.'+@mytkey2fld+'=new2.ord2_rord2id'
end
end 
end else 
begin
if (@keyfld>'') 
begin
if (@tord2tag=0) 
begin
if (@sord2tag=0) 
set @sql = @sql +' and old.'+@mytkeyfld+'=new.'+@keyfld 
else 
set @sql = @sql +' and old.'+@mytkeyfld+'=new2.'+@keyfld 
end 
else 
begin
if (@sord2tag=0) 
set @sql = @sql +' and old2.'+@mytkey2fld+'=new.'+@keyfld 
else
set @sql = @sql +' and old2.'+@mytkey2fld+'=new2.'+@keyfld 
end 
end else  -- 如果没有值，说明有可能就是f或r
begin
if (exists (select 1 from @targ where ttype=@ttype and tbizid=@tbizid and sflag='f'))  -- 当前单是f
begin
if (@tord2tag=0) 
begin
if (@sord2tag=0) 
set @sql = @sql +' and old.'+@mytkeyfld+'=new.ord_fordid'
else
set @sql = @sql +' and old.'+@mytkeyfld+'=new2.ord2_ford2id'
end 
else 
begin
if (@sord2tag=0) 
set @sql = @sql +' and old2.'+@mytkey2fld+'=new.ord_ford2id'
else 
set @sql = @sql +' and old2.'+@mytkey2fld+'=new2.ord2_ford2id' 
end  
end else 
if (exists (select 1 from @targ where ttype=@ttype and tbizid=@tbizid and sflag='r'))  -- 当前单是r
begin
if (@tord2tag=0) 
begin
if (@sord2tag=0) 
set @sql = @sql +' and old.'+@mytkeyfld+'=new.ord_rordid'
else
set @sql = @sql +' and old.'+@mytkeyfld+'=new2.ord2_rord2id'
end 
else 
begin
if (@sord2tag=0) 
set @sql = @sql +' and old2.'+@mytkey2fld+'=new.ord_rord2id'
else 
set @sql = @sql +' and old2.'+@mytkey2fld+'=new2.ord2_rord2id' 
end  
end 
end 
end

set @sql = @sql +N'
'
set @sql = @sql +N'''
if (@condition>'''') set @newsql=@newsql+'' and (''+@condition+'') '' ' +char(13)+CHAR(10) +'
exec sp_executesql @newsql, N''@cmpid int,@useruid int,@bizid int,@ordbcid int,'+
'@ordid bigint,@jbid bigint,@ftype varchar(10),@fbizid int,@fordid bigint,@ford2tag int ,'+
'@ford2id bigint,@rtype varchar(10),@rbizid int,@rordid bigint,@rord2tag int ,@rord2id bigint,'+
'@newobjid bigint,@ident varchar(40),@authority varchar(300),@bcid int '', @cmpid,@useruid,@bizid,'+
'@ordbcid,@ordid,@jbid,@ftype,@fbizid,@fordid,@ford2tag,@ford2id,@rtype,@rbizid,@rordid,@rord2tag,'+
'@rord2id,@newobjid,@ident,@authority,@bcid '+char(13)+CHAR(10) 
set @sql = @sql+'end '+char(13)+CHAR(10) 
set @nsql = @nsql+N'

' +@sql 
 FETCH NEXT FROM status11 INTO @ttype,@tbizid,@tord2tag,@sord2tag,@updatesql,@sflag,@extsql,@keyfld,@sqlins1,@sqlins2 ,@sqlback
END
CLOSE status11
DEALLOCATE status11 

set @nsql = @nsql+@sqlrear 

set @nsql = @nsql+N'
--- 下面这段程序，是完成帐务处理------
'
declare @ts9 nvarchar(2000)
if (exists (select * from mps_flowworkstatus where ws_id=@wsid and ws_accsource>=0 and 
ws_acctype>'' and ws_accobj>'' and (ws_accvalue+ws_accsuspend)>''))  
begin
 set @codei = @codei + 1
exec sys_sql_getTableNamePk @cmpid , 'biz',@bizid,0,@mystbname output,@myskeyfld output,@mysdbname output
   if (@mysdbname>'') set @mystbname = @mysdbname+@mystbname 
select @ts9= 'insert into mps_account_operation(accop_cmpid,accop_type,accop_category,accop_date,accop_objid,accop_fiid,'+
'accop_opdate,accop_operation,accop_value,accop_valuesuspend,accop_bizid,accop_no,accop_ordid,accop_jbid,accop_back) ' +
'select '+convert(varchar(10),@cmpid)+','''+ws_acctype+''','''+ws_accobjtype+''','+
case when ws_accdate='' then '''''' else ws_accdate end +','+ws_accobj+','+
case when ws_accfid='' then '0' else ws_accfid end +','+
'getdate(),'+convert(varchar(10),ws_accmethod)+','+
case when ws_accvalue='' then '0' else  ws_accvalue end +'*100,'+
case when ws_accsuspend='' then '0' else ws_accsuspend end +'*100,ord_bizid,ord_no,ord_id,@jbid,1 '+
'from '+@mystbname+
case when ws_accsource>0 then ','+@mystbname+'_'+convert(varchar(10),ws_accsource) 
else '' end +' where ord_id=@ordid '+
case when ws_accsource>0 then ' and ord2_ordid=ord_id ' 
else '' end +
case when ws_accsql>'' then ' and ('+ws_accsql+') '
else '' end
from mps_flowworkstatus 
where ws_id=@wsid and ws_accsource>=0 and ws_acctype>'' and ws_accobj>'' and (ws_accvalue+ws_accsuspend)>''
set @ts9 = ISNULL(@ts9,'') 
if (@ts9>'') 
set @nsql = @nsql+CHAR(13)+@ts9+CHAR(13) 
end

set @nsql = @nsql+N'
--- 帐务处理完毕 ------

--- 下面这段程序，是完成物资帐务处理------
'

 

if (exists (select * from mps_flowworkstatus where ws_id=@wsid and ws_whsource>=0 and 
ws_whprd>'' and  ws_whbarn1>''  and  (ws_whvalue+ws_whprev)>''))  
begin
 
set @codei = @codei + 1
exec sys_sql_getTableNamePk @cmpid , 'biz',@bizid,0,@mystbname output,@myskeyfld output,@mysdbname output
   if (@mysdbname>'') set @mystbname = @mysdbname+@mystbname 
select @ts9= 'insert into mps_warehouse_operation(whop_cmpid,whop_tag,whop_prdid,whop_whid,whop_whid2,whop_opdate,'+
'whop_operation,whop_value,whop_valuesuspend,whop_type,whop_branch,whop_depart,whop_creator,whop_bizid,whop_no,whop_ordid,whop_jbid,whop_back,
whop_unit,whop_batch,whop_price,whop_expire,whop_note,whop_location) ' +
'select '+convert(varchar(10),@cmpid)+','+convert(varchar(10),ws_whprdtag)+
','+
case when ws_whprd='' then '0' else ws_whprd end +','+
case when ws_whbarn1='' then '0' else ws_whbarn1 end +','+
case when ws_whbarn2='' then '0' else ws_whbarn2 end +','+
'getdate(),'+convert(varchar(10),ws_whmethod)+','+
case when ws_whvalue='' then '0' else  ws_whvalue end +' ,'+
case when ws_whprev='' then '0' else ws_whprev end +',ord_type,ord_branch,ord_createdepart,ord_creator,ord_bizid,ord_no,ord_id,@jbid,1'+
','+
case when ws_whunit='' then '''''' else ws_whunit end +','+
case when ws_whbatch='' then '''''' else ws_whbatch end +','+
case when ws_whprice='' then '0' else  ws_whprice end +' ,'+
case when ws_whexpire='' then '''1900-01-01''' else  ws_whexpire end +'  ,'+
case when ws_whnote='' then '''''' else ws_whnote end +','+
case when ws_whlocation='' then '''''' else ws_whlocation end +' '+
'from '+@mystbname+
case when ws_whsource>0 then ','+@mystbname+'_'+convert(varchar(10),ws_whsource) 
else '' end +' where ord_id=@ordid '+
case when ws_whsource>0 then ' and ord2_ordid=ord_id ' 
else '' end +
case when ws_whsql>'' then ' and ('+ws_whsql+') '
else '' end
from mps_flowworkstatus 
where ws_id=@wsid and ws_whsource>=0 and ws_whprd>'' and  ws_whbarn1>'' and   (ws_whvalue+ws_whprev)>''  
set @ts9 = ISNULL(@ts9,'') 
if (@ts9>'') 
set @nsql = @nsql+CHAR(13)+@ts9+CHAR(13) 
end

set @nsql = @nsql+N'
--- 物资帐务处理完毕 ------

drop table #mptypes 

'
set @procname = ''
declare @old varchar(50) 
 
if (len(@nsql)>130 and @codei>0) 
begin
set @procname = 'order_'+ltrim(rtrim(str(@bizid)))+'_dolinkdata_'+ltrim(rtrim(str(@wsid))) 
set @old = @procname 
set @nsql = 'create procedure order_'+ltrim(rtrim(str(@bizid)))+
N'_dolinkdata_'+ltrim(rtrim(str(@wsid)))+N'(
@cmpid int ,
@useruid int ,
@bizid int ,
@ordbcid int,
@ordid bigint,
@jbid bigint,
@ftype varchar(10),
@fbizid int,
@fordid bigint,
@ford2tag int ,
@ford2id bigint,
@rtype varchar(10),
@rbizid int,
@rordid bigint,
@rord2tag int ,
@rord2id bigint 
) 
as 
begin
'+@nsql+N'
end
'
set @procname = ''
execute(@nsql)

set @procname = @old 
end

exec myprint @nsql 
set nocount off
end