IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[sys_gen_newbizbysrc]') AND type = 'P')
DROP PROCEDURE [dbo].[sys_gen_newbizbysrc]
GO
 
CREATE procedure [dbo].[sys_gen_newbizbysrc] (
    @cmpid int  ,
   @bizid int
 )
as 
BEGIN
declare @sql nvarchar(max) 
set @sql = N'
create procedure order_'+ltrim(rtrim(str(@bizid)))+N'_newbizbysrc (
    @cmpid int , 
    @bcid int ,
    @branch nvarchar(50),
    @dpid int ,
    @depart nvarchar(50),
    @createcuid int ,
    @cuname nvarchar(50),
    @jbid bigint ,
    @bizstyle int,
    @biztitle nvarchar(100),  
    @bizcode nvarchar(100),
    @stype varchar(10) ,
    @sbizid int ,
    @sordid bigint ,
    @sord2tag int ,
    @sord2id bigint, 
    @newordids varchar(2000) output 
   ) 
  as 
  begin 
set @jbid = isnull(@jbid,0) 
if (@jbid=-999999999) set @jbid=0
declare @ordno varchar(50)
  declare @maxordid bigint 
  declare @ord table(ordid bigint , ordno varchar(50)) 
  declare @newcount int 
  set @newcount = 0 
  declare @newordid bigint
  ' 
   --  生成主表数据
   
   declare @ttablename varchar(100) 
   declare @tkeyfld varchar(100) 
   declare @tdatabase varchar(100) 
   
   declare @stablename varchar(100) 
   declare @skeyfld varchar(100) 
   declare @skeyfld2 varchar(100) 
   declare @skeyfld22 varchar(100) 
   declare @sdatabase varchar(100) 
   declare @sord2tagfld varchar(30) 
   
   declare @flds nvarchar(2000) 
   declare @flds2 nvarchar(2000) 
   declare @vals nvarchar(4000) 
   
   declare @bsid int
   declare @bsstype varchar(20)
   declare @bssbizid int 
   declare @bssord2tag int 
   declare @bsneword2 int
   declare @bssql nvarchar(4000) 
   declare @bsord2tag int 
   declare @bsprevsql nvarchar(max) 
   declare @bstables nvarchar(1000) 
   declare @bswhere nvarchar(4000) 
   
   declare @newordid bigint 
   
    
   
   exec sys_sql_getTableNamePk @cmpid , 'biz',@bizid,0,@ttablename output,@tkeyfld output,@tdatabase output
   if (@tdatabase>'') set @ttablename = @tdatabase+@ttablename 
   
   set @sql=@sql+N'
select @maxordid = max(ord_id) from '+@ttablename+N'  
set @maxordid = isnull(@maxordid , 0 ) 
'
  
   declare @nodetails int 
   set @nodetails = 1 
    
   select top 1 @bsid=bs_id 
   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))
   select @bsid=isnull(@bsid,0) 
   -- 如果有生成员的项
if (@bsid>0)   
begin  -- 需要引入主表数据 
declare status1 cursor LOCAL FORWARD_ONLY READ_ONLY for
  select bs_id , bs_stype ,bs_sbizid,bs_sord2tag,bs_newviaord2,bs_sql , bs_prevsql,bs_tables,bs_where 
   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 and bsf_tfldname like 'ord_%'))
  open status1
  FETCH NEXT FROM status1 INTO @bsid,@bsstype ,@bssbizid,@bssord2tag,@bsneword2 ,@bssql , @bsprevsql ,  @bstables, @bswhere 
  WHILE (@@FETCH_STATUS = 0)
  BEGIN
  set @bsprevsql = ISNULL(@bsprevsql,'') 
  set @bssql = ISNULL(@bssql,'') 
  set @bstables = ISNULL(@bstables,'') 
  set @bswhere = ISNULL(@bswhere,'') 
  
  if (@bsprevsql>'') 
  begin
  set @sql = @sql+CHAR(13)+CHAR(10)+@bsprevsql+CHAR(13)+CHAR(10)  
  end 
  
  exec sys_sql_getTableNamePk @cmpid , @bsstype,@bssbizid,0,@stablename output,@skeyfld output,@sdatabase output
   if (@sdatabase>'') set @stablename = @sdatabase+@stablename 
   
   set @skeyfld2 = case 
   when @bsstype='biz' then 'ord2_id' 
   when @bsstype='cst' then 'cc2_id' 
   when @bsstype='prd' then 'prd2_id' 
   when @bsstype='equ' then 'equ2_id' 
   else @skeyfld 
    end
   set @skeyfld22 = case 
   when @bsstype='biz' then 'ord2_ordid' 
   when @bsstype='cst' then 'cc2_ccid' 
   when @bsstype='prd' then 'prd2_prdid' 
   when @bsstype='equ' then 'equ2_equid' 
   else @skeyfld 
    end
   set @sord2tagfld = case 
   when @bsstype='biz' then 'ord2_tag' 
   when @bsstype='cst' then 'cc2_tag' 
   when @bsstype='prd' then 'prd2_tag' 
   when @bsstype='equ' then 'equ2_tag' 
   else @skeyfld 
    end
  set @flds=null 
select @flds=ISNULL(@flds+',','')+bsf_tfldname from mps_biztype_comefrom_fieldmap 
where bsf_bsid=@bsid and bsf_tfldname like 'ord_%' and bsf_use=0 order by bsf_tfldname,bsf_expression
set @flds=isnull(@flds,'') 
set @flds2 =','+@flds+',' 

set @vals=null 
select @vals=ISNULL(@vals+',','')+bsf_expression from mps_biztype_comefrom_fieldmap 
where bsf_bsid=@bsid  and bsf_tfldname like 'ord_%' and bsf_use=0 order by bsf_tfldname ,bsf_expression
set @vals=isnull(@vals,'') 

-- 纠正字段
if (charindex(',ord_cmpid,',@flds2)<=0) 
begin 
set @flds = @flds+',ord_cmpid' 
set @vals=@vals+',@cmpid ' 
end 
if (charindex(',ord_finished,',@flds2)<=0) 
begin 
set @flds = @flds+',ord_finished' 
set @vals=@vals+',1 ' 
end 
if (charindex(',ord_fjbid,',@flds2)<=0) 
begin 
set @flds = @flds+',ord_fjbid' 
set @vals=@vals+',@jbid ' 
end 
if (charindex(',ord_type,',@flds2)<=0) 
begin 
set @flds = @flds+',ord_type' 
set @vals=@vals+N',@biztitle ' 
end 
if (charindex(',ord_style,',@flds2)<=0) 
begin 
set @flds = @flds+',ord_style' 
set @vals=@vals+N',@bizstyle ' 
end 
if (charindex(',ord_date,',@flds2)<=0) 
begin 
set @flds = @flds+',ord_date' 
set @vals=@vals+',getdate()' 
end 
if (charindex(',ord_updated,',@flds2)<=0) 
begin 
set @flds = @flds+',ord_updated' 
set @vals=@vals+',getdate()' 
end 
if (charindex(',ord_bizid,',@flds2)<=0) 
begin 
set @flds = @flds+',ord_bizid' 
set @vals=@vals+','+ltrim(rtrim(str(@bizid)))  
end 
if (charindex(',ord_bcid,',@flds2)<=0) 
begin 
set @flds = @flds+',ord_bcid' 
set @vals=@vals+',@bcid ' 
end 
if (charindex(',ord_branch,',@flds2)<=0) 
begin 
set @flds = @flds+',ord_branch' 
set @vals=@vals+',@branch ' 
end 
if (charindex(',ord_createdpid,',@flds2)<=0) 
begin 
set @flds = @flds+',ord_createdpid' 
set @vals=@vals+',@dpid ' 
end 
if (charindex(',ord_dpid,',@flds2)<=0) 
begin 
set @flds = @flds+',ord_dpid' 
set @vals=@vals+',@dpid ' 
end 
if (charindex(',ord_createcuid,',@flds2)<=0) 
begin 
set @flds = @flds+',ord_createcuid' 
set @vals=@vals+',@createcuid ' 
end 
if (charindex(',ord_cuid,',@flds2)<=0) 
begin 
set @flds = @flds+',ord_cuid' 
set @vals=@vals+',@createcuid ' 
end 
if (charindex(',ord_createdepart,',@flds2)<=0) 
begin 
set @flds = @flds+',ord_createdepart' 
set @vals=@vals+',@depart ' 
end 
if (charindex(',ord_depart,',@flds2)<=0) 
begin 
set @flds = @flds+',ord_depart' 
set @vals=@vals+',@depart ' 
end 
if (charindex(',ord_creator,',@flds2)<=0) 
begin 
set @flds = @flds+',ord_creator' 
set @vals=@vals+',@cuname ' 
end 
if (charindex(',ord_empname,',@flds2)<=0) 
begin 
set @flds = @flds+',ord_empname' 
set @vals=@vals+',@cuname ' 
end 
if (charindex(',ord_ftype,',@flds2)<=0) 
begin 
set @flds = @flds+',ord_ftype' 
set @vals=@vals+','''+@bsstype+'''' 
end 
if (charindex(',ord_fbizid,',@flds2)<=0) 
begin 
set @flds = @flds+',ord_fbizid' 
if (@bsstype='biz') set @vals=@vals+',new.ord_bizid ' 
if (@bsstype='cst') set @vals=@vals+',new.cc_tag ' 
if (@bsstype='prd') set @vals=@vals+',new.prd_tag ' 
if (@bsstype='equ') set @vals=@vals+',new.equ_tag ' 
if (@bsstype='doc') set @vals=@vals+',new.doc_tag ' 
end 
if (charindex(',ord_fordid,',@flds2)<=0) 
begin 
set @flds = @flds+',ord_fordid' 
if (@bsstype='biz') set @vals=@vals+',new.ord_id ' 
if (@bsstype='cst') set @vals=@vals+',new.cc_id ' 
if (@bsstype='prd') set @vals=@vals+',new.prd_id ' 
if (@bsstype='equ') set @vals=@vals+',new.equ_id ' 
if (@bsstype='doc') set @vals=@vals+',new.doc_id ' 
end 
if (charindex(',ord_ford2tag,',@flds2)<=0) 
begin 
set @flds = @flds+',ord_ford2tag' 
set @vals=@vals+','+ltrim(rtrim(str(@bssord2tag)))  
end 
if (charindex(',ord_ford2id,',@flds2)<=0) 
begin 
set @flds = @flds+',ord_ford2id' 
if (@bssord2tag=0) set @vals=@vals+',0 ' 
else 
begin
if (@bsstype='biz') set @vals=@vals+',new2.ord2_tag '  
if (@bsstype='cst') set @vals=@vals+',new2.cc2_tag '  
if (@bsstype='prd') set @vals=@vals+',new2.prd2_tag '  
if (@bsstype='equ') set @vals=@vals+',new2.equ2_tag '  
end
end 
if (charindex(',ord_rtype,',@flds2)<=0) 
begin 
  set @flds = @flds+',ord_rtype' 
  if (@bsstype='biz')   -- only for biz
  begin
  if (@bssord2tag>0) set @vals=@vals+',case when new2.ord2_fordid=0 then '''+@bsstype+''' when new2.ord2_rordid>0 then new2.ord2_rtype else new2.ord2_ftype end ' 
  else set @vals=@vals+',case when new.ord_fordid=0 then '''+@bsstype+''' when new.ord_rordid>0 then new.ord_rtype  else new.ord_ftype end ' 
  end else 
  begin
  set @vals=@vals+','''' ' 
  end 
end 
if (charindex(',ord_rbizid,',@flds2)<=0) 
begin 
set @flds = @flds+',ord_rbizid' 
if (@bsstype='biz') 
begin
if (@bssord2tag>0) set @vals=@vals+',case when new2.ord2_fordid=0 then new.ord_bizid when new2.ord2_rordid>0 then new2.ord2_rbizid  else new2.ord2_fbizid end ' 
  else set @vals=@vals+',case when new.ord_fordid=0 then new.ord_bizid when new.ord_rordid>0 then new.ord_rordid else new.ord_fbizid end ' 
  end else  
  begin
  set @vals=@vals+',0 ' 
  end
end 
if (charindex(',ord_rordid,',@flds2)<=0) 
begin 
set @flds = @flds+',ord_rordid' 
if (@bsstype='biz') 
  begin
if (@bssord2tag>0) set @vals=@vals+',case when new2.ord2_fordid=0 then new.ord_id when new2.ord2_rordid>0 then new2.ord2_rordid else new2.ord2_fordid end ' 
   else set @vals=@vals+',case when new.ord_fordid=0 then new.ord_id when new.ord_rordid>0 then new.ord_rordid else new.ord_fordid end ' 
  endelse 
  begin
  set @vals=@vals+',0 ' 
  end
end 
if (charindex(',ord_rord2tag,',@flds2)<=0) 
begin 
set @flds = @flds+',ord_rord2tag' 
if (@bsstype='biz') 
  begin
if (@bssord2tag>0) set @vals=@vals+',case when new2.ord2_fordid=0 then '+ltrim(rtrim(str(@bssord2tag)))+' when new2.ord2_rordid>0 then new2.ord2_rord2tag else new2.ord2_ford2tag end ' 
   else set @vals=@vals+',case when new.ord_fordid=0 then 0 when new.ord_rordid>0 then new.ord_rord2tag  else new.ord_ford2tag end ' 
  endelse 
  begin
  set @vals=@vals+',0 ' 
  end
end 
if (charindex(',ord_rord2id,',@flds2)<=0) 
begin 
set @flds = @flds+',ord_rord2id' 
if (@bsstype='biz') 
  begin
if (@bssord2tag>0) set @vals=@vals+',case when new2.ord2_fordid=0 then new2.ord2_id when new2.ord2_rordid>0 then new2.ord2_rord2id else new2.ord2_ford2id end ' 
else set @vals=@vals+',case when new.ord_fordid=0 then 0 when new.ord_rordid>0 then new.ord_rord2id else new.ord_ford2id end ' 
endelse 
  begin
  set @vals=@vals+',0 ' 
  end
end 
 
  set @sql = @sql +N'
if (  @stype='''+@bsstype+''' and @sbizid='+ltrim(rtrim(str(@bssbizid)))+
N' and ((@sord2tag>0 and @sord2tag='+ltrim(rtrim(str(@bssord2tag)))+N') or (@sord2tag=0)))  
begin
 insert into '+@ttablename+N'('+@flds+N') 
select '+@vals  

if (@bssord2tag=0) -- 从主表中来
begin

set @sql=@sql+N'
  from '+@stablename+N' new  with(nolock) ' 
-- 将一些补充表加进来
if (CHARINDEX('dp_',@vals)>0 and charindex('dp_id',@bswhere)>0) 
begin
if (charindex('mps_depart',@bstables)<=0) 
set @sql = @sql +' , mps_depart with(nolock) '  -- 条件部分需要人为写
end 

if (CHARINDEX('cu_',@vals)>0 and (charindex('cu_id',@bswhere)>0 or  charindex('cu_ccid',@bswhere)>0)) 
begin
if (charindex('mps_login',@bstables)<=0) 
set @sql = @sql +' , mps_login with(nolock) '  -- 条件部分需要人为写
end 

if (CHARINDEX('ordp_cuid',@vals)>0) 
begin
if (charindex(@stablename+'_users',@bstables)<=0) 
set @sql = @sql +' , '+@stablename+'_users with(nolock) '  -- 条件部分需要人为写
end 

if (CHARINDEX('ccp_cuid',@vals)>0) 
begin
if (charindex(@stablename+'_users',@bstables)<=0) 
set @sql = @sql +' , '+@stablename+'_users with(nolock) '  -- 条件部分需要人为写
end 

if (CHARINDEX('prdp_cuid',@vals)>0) 
begin
if (charindex(@stablename+'_users',@bstables)<=0) 
set @sql = @sql +' , '+@stablename+'_users with(nolock) '  -- 条件部分需要人为写
end 

if (CHARINDEX('equp_cuid',@vals)>0) 
begin
if (charindex(@stablename+'_users',@bstables)<=0) 
set @sql = @sql +' , '+@stablename+'_users with(nolock) '  -- 条件部分需要人为写
end 

if (@bstables>'') 
begin
set @sql = @sql +' , '+@bstables   -- 条件部分需要人为写
end 

set @sql = @sql +' where  '+@skeyfld+N'=@sordid '  -- 条件部分需要人为写

if (@bswhere>'') 
begin
set @sql = @sql + ' and ('+@bswhere +') ' 
end 

end else 
begin
set @sql=@sql+N'
  from '+@stablename+' new with(nolock), '+@stablename+'_'+ltrim(rtrim(str(@bssord2tag)))+N' new2 with(nolock)' 

if (CHARINDEX('dp_',@vals)>0 and charindex('dp_id',@bswhere)>0) 
begin
if (charindex('mps_depart',@bstables)<=0) 
set @sql = @sql +' , mps_depart with(nolock) '  -- 条件部分需要人为写
end 


if (CHARINDEX('cu_',@vals)>0 and (charindex('cu_id',@bswhere)>0 or  charindex('cu_ccid',@bswhere)>0)) 
begin
if (charindex('mps_login',@bstables)<=0) 
set @sql = @sql +' , mps_login with(nolock) '  -- 条件部分需要人为写
end 

if (CHARINDEX('ordp_cuid',@vals)>0) 
begin
if (charindex(@stablename+'_users',@bstables)<=0) 
set @sql = @sql +' , '+@stablename+'_users with(nolock) '  -- 条件部分需要人为写
end 

if (CHARINDEX('ccp_cuid',@vals)>0) 
begin
if (charindex(@stablename+'_users',@bstables)<=0) 
set @sql = @sql +' , '+@stablename+'_users with(nolock) '  -- 条件部分需要人为写
end

if (CHARINDEX('prdp_cuid',@vals)>0) 
begin
if (charindex(@stablename+'_users',@bstables)<=0) 
set @sql = @sql +' , '+@stablename+'_users with(nolock) '  -- 条件部分需要人为写
end 

if (CHARINDEX('equp_cuid',@vals)>0) 
begin
if (charindex(@stablename+'_users',@bstables)<=0) 
set @sql = @sql +' , '+@stablename+'_users with(nolock) '  -- 条件部分需要人为写
end 

if (@bstables>'') 
begin
set  @sql=@sql +' , '+@bstables 
end 

 

set @sql = @sql +' where new.'+@skeyfld+N'=@sordid and new.'+@skeyfld+'=new2.'+@skeyfld22 + 
' and new2.'+@sord2tagfld+'='+CONVERT(varchar(10),@bssord2tag)+' and ((@sord2id>0 and new2.'+@skeyfld2+'=@sord2id)'+
' or (@sord2id=0)) '

if (@bswhere>'') set @sql = @sql+N' and (
'+@bswhere+N'
)' 
if (@bsstype='biz') set @sql=@sql+' order by ord2_tag ,ord2_order , ord2_id ' 
if (@bsstype='cst') set @sql=@sql+' order by cc2_tag ,cc2_id ' 
if (@bsstype='prd') set @sql=@sql+' order by prd2_tag ,prd2_id ' 
if (@bsstype='equ') set @sql=@sql+' order by equ2_tag ,equ2_id ' 
set @sql = @sql + N'
 ' 
end
set @sql = @sql +N'
end
'
  FETCH NEXT FROM status1 INTO @bsid,@bsstype ,@bssbizid,@bssord2tag,@bsneword2 ,@bssql , @bsprevsql ,  @bstables, @bswhere 
  END
  CLOSE status1
DEALLOCATE status1 
set @sql = @sql+N'
insert into @ord(ordid,ordno) select ord_id,'''' from '+@ttablename +N' with(nolock) 
where ord_cmpid=@cmpid and ord_id>@maxordid and ord_bizid='+ltrim(rtrim(str(@bizid)))+
N' and ord_no=''''  and ord_fordid=@sordid 
declare status11 cursor LOCAL FORWARD_ONLY READ_ONLY for
select ord_id from '+@ttablename+N' with(nolock) , @ord where ord_id=ordid  
open status11
FETCH NEXT FROM status11 INTO @newordid 
WHILE (@@FETCH_STATUS = 0)
BEGIN
set @newcount = @newcount + 1
exec mps_bm_getorset @cmpid,''biz'','+ltrim(rtrim(str(@bizid)))+N',@newordid,@createcuid,@bizcode,2,@ordno output,1 
update @ord set ordno=@ordno where ordid=@newordid 
FETCH NEXT FROM status11 INTO @newordid 
END
CLOSE status11
DEALLOCATE status11   
update '+@ttablename+N'  set ord_no=ordno , ord_finished=1 from @ord where ord_id=ordid 
select top 1 @newordid=ordid from @ord  
set @newordid = isnull(@newordid,0) '
end else 
begin  -- 没有可以引入的数据，由系统自动新建一条。  
set @sql = @sql+N'
insert into '+@ttablename+'(ord_cmpid,ord_style,ord_bcid,ord_branch,ord_bizid,ord_type,ord_date,ord_updated, 
ord_createdpid,ord_createcuid,ord_createdepart,ord_creator,ord_finished,ord_fjbid)  
select @cmpid,@bizstyle,@bcid,@branch,'+ltrim(rtrim(str(@bizid)))+N',@biztitle,getdate(),getdate(), @dpid,@createcuid,@depart,@cuname ,1 , @jbid  
set @newordid=@@Identity  
set @newordid=isnull(@newordid,0) 
if (@newordid>0) exec mps_bm_getorset @cmpid,''biz'','+ltrim(rtrim(str(@bizid)))+N',@newordid,@createcuid,@bizcode,2,@ordno output,1 
if (@newordid>0) 
begin
set @newcount = @newcount + 1
insert into @ord(ordid,ordno) values(@newordid,@ordno)  
update '+@ttablename+N' set ord_no=@ordno , ord_finished=1 where ord_id=@newordid 
end' 
end
-- 只有一种情况 ，才可以生成明细表数据 ，就是只有一条主要数据  


declare status111 cursor LOCAL FORWARD_ONLY READ_ONLY for
select bs_id , bs_ord2tag,bs_stype ,bs_sbizid,bs_sord2tag,bs_newviaord2,bs_sql  , bs_prevsql , bs_tables , bs_where 
   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 and bsf_tfldname like 'ord2_%' ))
  open status111
  FETCH NEXT FROM status111 INTO @bsid,@bsord2tag,@bsstype ,@bssbizid,@bssord2tag,@bsneword2 ,@bssql , @bsprevsql , @bstables, @bswhere 
  WHILE (@@FETCH_STATUS = 0)
  BEGIN
  
set @bsprevsql = ISNULL(@bsprevsql,'') 
  set @bssql = ISNULL(@bssql,'') 
  set @bstables = ISNULL(@bstables,'') 
  set @bswhere = ISNULL(@bswhere,'') 
  
  if (@bsprevsql>'') 
  begin
  set @sql = @sql+CHAR(13)+CHAR(10)+@bsprevsql+CHAR(13)+CHAR(10)  
  end 
  
  exec sys_sql_getTableNamePk @cmpid , @bsstype,@bssbizid,0,@stablename output,@skeyfld output,@sdatabase output
if (@sdatabase>'') set @stablename = @sdatabase+@stablename 
set @skeyfld2 = case 
when @bsstype='biz' then 'ord2_id' 
when @bsstype='cst' then 'cc2_id' 
when @bsstype='prd' then 'prd2_id' 
when @bsstype='equ' then 'equ2_id' 
else @skeyfld 
 end
set @skeyfld22 = case 
when @bsstype='biz' then 'ord2_ordid' 
when @bsstype='cst' then 'cc2_ccid' 
when @bsstype='prd' then 'prd2_prdid' 
when @bsstype='equ' then 'equ2_equid' 
else @skeyfld 
 end
set @sord2tagfld = case 
when @bsstype='biz' then 'ord2_tag' 
when @bsstype='cst' then 'cc2_tag' 
when @bsstype='prd' then 'prd2_tag' 
when @bsstype='equ' then 'equ2_tag' 
else @skeyfld 
 end
  set @flds=null 
select @flds=ISNULL(@flds+',','')+bsf_tfldname from mps_biztype_comefrom_fieldmap 
where bsf_bsid=@bsid and bsf_tfldname like 'ord2_%' and bsf_use=0 order by bsf_tfldname,bsf_expression 
set @flds=isnull(@flds,'') 
set @flds2 =','+@flds+',' 

set @vals=null 
select @vals=ISNULL(@vals+',','')+bsf_expression from mps_biztype_comefrom_fieldmap 
where bsf_bsid=@bsid  and bsf_tfldname like 'ord2_%' and bsf_use=0 order by bsf_tfldname,bsf_expression 
set @vals=isnull(@vals,'') 

if (charindex(',ord2_ordid,',@flds2)<=0) 
begin 
set @flds = @flds+',ord2_ordid' 
set @vals=@vals+',@newordid ' 
end 
if (charindex(',ord2_date,',@flds2)<=0) 
begin 
set @flds = @flds+',ord2_date' 
set @vals=@vals+',getdate() ' 
end 
if (charindex(',ord2_group,',@flds2)<=0) 
begin 
set @flds = @flds+',ord2_group' 
if (@bsstype='biz')  set @vals=@vals+',new2.ord2_group ' 
else  set @vals=@vals+','''' ' 
end 
if (charindex(',ord2_order,',@flds2)<=0) 
begin 
set @flds = @flds+',ord2_order' 
if (@bsstype='biz')  set @vals=@vals+',new2.ord2_order ' 
else  set @vals=@vals+','''' ' 
end 
if (charindex(',ord2_tags,',@flds2)<=0) 
begin 
set @flds = @flds+',ord2_tags' 
if (@bsstype='biz') set @vals=@vals+',new2.ord2_tags ' 
else  set @vals=@vals+','''' ' 
end 
if (charindex(',ord2_tag,',@flds2)<=0) 
begin 
set @flds = @flds+',ord2_tag' 
set @vals=@vals+','+ltrim(rtrim(str(@bsord2tag)))  
end 
if (charindex(',ord2_ftype,',@flds2)<=0) 
begin 
set @flds = @flds+',ord2_ftype' 
set @vals=@vals+','''+@bsstype+'''' 
end 
if (charindex(',ord2_fbizid,',@flds2)<=0) 
begin 
set @flds = @flds+',ord2_fbizid' 
if (@bsstype='biz') set @vals=@vals+',new.ord_bizid ' 
if (@bsstype='cst') set @vals=@vals+',new.cc_tag ' 
if (@bsstype='prd') set @vals=@vals+',new.prd_tag ' 
if (@bsstype='equ') set @vals=@vals+',new.equ_tag ' 
end 
if (charindex(',ord2_fordid,',@flds2)<=0) 
begin 
set @flds = @flds+',ord2_fordid' 
if (@bsstype='biz') set @vals=@vals+',new.ord_id ' 
if (@bsstype='cst') set @vals=@vals+',new.cc_id ' 
if (@bsstype='prd') set @vals=@vals+',new.prd_id ' 
if (@bsstype='equ') set @vals=@vals+',new.equ_id ' 
end 
if (charindex(',ord2_ford2tag,',@flds2)<=0) 
begin 
set @flds = @flds+',ord2_ford2tag'  -- 不能是源的tag ,因为有可能会转换
set @vals=@vals+','+ltrim(rtrim(str(@bssord2tag)))  
end 
if (charindex(',ord2_ford2id,',@flds2)<=0) 
begin 
set @flds = @flds+',ord2_ford2id' 
if (@bsstype='biz') set @vals=@vals+',new2.ord2_id ' 
if (@bsstype='cst') set @vals=@vals+',new2.cc2_id ' 
if (@bsstype='prd') set @vals=@vals+',new2.prd2_id '  
if (@bsstype='equ') set @vals=@vals+',new2.equ2_id ' 
end 

if (charindex(',ord2_rtype,',@flds2)<=0) 
begin 
set @flds = @flds+',ord2_rtype' 
if (@bsstype='biz') set @vals=@vals+',case when new2.ord2_fordid=0 then '''+@bsstype+''' when new2.ord2_rordid>0 then new2.ord2_rtype else new2.ord2_ftype end ' 
else set @vals=@vals+','''' ' 
end 
if (charindex(',ord2_rbizid,',@flds2)<=0) 
begin 
set @flds = @flds+',ord2_rbizid' 
if (@bsstype='biz') set @vals=@vals+',case when new2.ord2_fordid=0 then new2.ord2_fbizid when new2.ord2_rordid>0 then new2.ord2_rbizid else new2.ord2_fbizid end ' 
else set @vals=@vals+',0 ' 
end 
if (charindex(',ord2_rordid,',@flds2)<=0) 
begin 
set @flds = @flds+',ord2_rordid' 
if (@bsstype='biz') set @vals=@vals+',case when new2.ord2_fordid=0 then new2.ord2_fordid when new2.ord2_rordid>0 then new2.ord2_rordid else new2.ord2_fordid end ' 
else set @vals=@vals+',0 ' 
end 
if (charindex(',ord2_rord2tag,',@flds2)<=0) 
begin 
set @flds = @flds+',ord2_rord2tag' 
if (@bsstype='biz') set @vals=@vals+',case when new2.ord2_fordid=0 then new2.ord2_tag when new2.ord2_rordid>0 then new2.ord2_rord2tag else new2.ord2_ford2tag end ' 
else set @vals=@vals+',0 ' 
end 
if (charindex(',ord2_rord2id,',@flds2)<=0) 
begin 
set @flds = @flds+',ord2_rord2id' 
if (@bsstype='biz') set @vals=@vals+',case when new2.ord2_fordid=0 then new2.ord2_ordid when new2.ord2_rordid>0 then new2.ord2_rordid else new2.ord2_ford2id end ' 
else set @vals=@vals+',0 ' 
end 
  set @sql=@sql+N'
  if (@newcount=1 and  @stype='''+@bsstype+''' and @sbizid='+ltrim(rtrim(str(@bssbizid)))+N')  
begin
insert into '+@ttablename+'_'+ltrim(rtrim(str(@bsord2tag))) +
  N'('+@flds+N') 
select '+@vals +N'
from '+@stablename+' new , '+@stablename+'_'+ltrim(rtrim(str(@bssord2tag)))+N' new2 '


-- 将一些补充表加进来
if (CHARINDEX('dp_',@vals)>0 and charindex('dp_id',@bswhere)>0) 
begin
if (charindex('mps_depart',@bstables)<=0) 
set @sql = @sql +' , mps_depart with(nolock) '  -- 条件部分需要人为写
end 

if (CHARINDEX('cu_',@vals)>0 and (charindex('cu_id',@bswhere)>0 or  charindex('cu_ccid',@bswhere)>0)) 
begin
if (charindex('mps_login',@bstables)<=0) 
set @sql = @sql +' , mps_login with(nolock) '  -- 条件部分需要人为写
end 

if (CHARINDEX('ordp_cuid',@vals)>0) 
begin
if (charindex(@stablename+'_users',@bstables)<=0) 
set @sql = @sql +' , '+@stablename+'_users with(nolock) '  -- 条件部分需要人为写
end 

if (CHARINDEX('ccp_cuid',@vals)>0) 
begin
if (charindex(@stablename+'_users',@bstables)<=0) 
set @sql = @sql +' , '+@stablename+'_users with(nolock) '  -- 条件部分需要人为写
end 

if (CHARINDEX('prdp_cuid',@vals)>0) 
begin
if (charindex(@stablename+'_users',@bstables)<=0) 
set @sql = @sql +' , '+@stablename+'_users with(nolock) '  -- 条件部分需要人为写
end 

if (CHARINDEX('equp_cuid',@vals)>0) 
begin
if (charindex(@stablename+'_users',@bstables)<=0) 
set @sql = @sql +' , '+@stablename+'_users with(nolock) '  -- 条件部分需要人为写
end 

if (@bstables>'') 
begin
set @sql = @sql +' , '+@bstables   -- 条件部分需要人为写
end 


set @sql = @sql+'
where new.'+@skeyfld+'=@sordid and new.'+@skeyfld+'=new2.'+@skeyfld22+' and new2.'+@sord2tagfld+'='+ltrim(rtrim(str(@bssord2tag))) 
  if (@bswhere>'') 
  set @sql=@sql+N' and (
  '+@bswhere+N'
  ) '
  set @sql = @sql+N'
end'
   FETCH NEXT FROM status111 INTO @bsid,@bsord2tag,@bsstype ,@bssbizid,@bssord2tag,@bsneword2 ,@bssql , @bsprevsql , @bstables, @bswhere 
  END
  CLOSE status111
DEALLOCATE status111 
set @sql = @sql + N'
set @newordids=null 
select @newordids=ISNULL(@newordids+'','','''')+convert(varchar(16),ordid) from @ord 
set @newordids = isnull(@newordids,'''')  
end ' 
exec myprint @sql 
set @flds = N'IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(''[dbo].[order_'+ltrim(rtrim(str(@bizid)))+N'_newbizbysrc]'') AND type in (''P'', ''PC''))
DROP PROCEDURE [dbo].[order_'+ltrim(rtrim(str(@bizid)))+N'_newbizbysrc] ' 
exec(@flds)
exec(@sql)
END