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

create procedure order_271_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
  
select @maxordid = max(ord_id) from order_271 with(nolock)  
set @maxordid = isnull(@maxordid , 0 ) 

if (  @stype='biz' and @sbizid=260 and ((@sord2tag>0 and @sord2tag=0) or (@sord2tag=0)))  
begin 
 insert into order_271(ord_bxlx,ord_cuid,ord_cylx,ord_depart,ord_dpid,ord_dqmdgcsid,ord_empname,ord_fcbzls,ord_fordid,ord_gjrq,ord_gzfl,ord_gzsm,ord_hyxm,ord_imei,ord_jjlx,ord_jjmd,ord_jjrq,ord_lxdh,ord_sfzh,ord_sn,ord_ssfgs,ord_sxfj,ord_wgqk,ord_ys,ord_zdpp,ord_zdxh,ord_cmpid,ord_finished,ord_fjbid,ord_type,ord_style,ord_date,ord_updated,ord_bizid,ord_bcid,ord_branch,ord_createdpid,ord_createcuid,ord_createdepart,ord_creator,ord_ftype,ord_fbizid,ord_ford2tag,ord_ford2id,ord_rtype,ord_rbizid,ord_rordid,ord_rord2tag,ord_rord2id) 
select new.ord_fwlx,new.ord_createcuid,new.ord_wgzk,new.ord_depart,new.ord_dpid,new.ord_dqmdgcsid,new.ord_empname,new.ord_fcbz,new.ord_id,new.ord_gjrq,new.ord_gzfl1j,new.ord_gzms,new.ord_xm,new.ord_imei,new.ord_zdlx,new.ord_createdepart,new.ord_date,new.ord_sjh,new.ord_sfzh,new.ord_sn,new.ord_branch,new.ord_sxfj,new.ord_wgzk,new.ord_ys,new.ord_zdpp,new.ord_zdxh,@cmpid ,1 ,@jbid ,@biztitle ,@bizstyle ,getdate(),getdate(),271,@bcid ,@branch ,@dpid ,@createcuid ,@depart ,@cuname ,'biz',new.ord_bizid ,0,0 ,case when new.ord_fordid=0 then 'biz' when new.ord_rordid>0 then new.ord_rtype  else new.ord_ftype end ,case when new.ord_fordid=0 then new.ord_bizid when new.ord_rordid>0 then new.ord_rordid else new.ord_fbizid end ,case when new.ord_fordid=0 then new.ord_id when new.ord_rordid>0 then new.ord_rordid else new.ord_fordid end ,case when new.ord_fordid=0 then 0 when new.ord_rordid>0 then new.ord_rord2tag  else new.ord_ford2tag end ,case when new.ord_fordid=0 then 0 when new.ord_rordid>0 then new.ord_rord2id else new.ord_ford2id end 
  from order_260 new  with(nolock)  where  ord_id=@sordid 
end

insert into @ord(ordid,ordno) select ord_id,'' from order_271 with(nolock) 
where ord_cmpid=@cmpid and ord_id>@maxordid and ord_bizid=271 and ord_no='' and ord_finished=1    and ord_fordid=@sordid 
declare status11 cursor LOCAL FORWARD_ONLY READ_ONLY for
select ordid from   @ord  order by ordid 
open status11
FETCH NEXT FROM status11 INTO @newordid 
WHILE (@@FETCH_STATUS = 0)
BEGIN
set @newcount = @newcount + 1
exec mps_bm_getorset @cmpid,'biz',271,@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 order_271 with(rowlock) 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) 
set @newordids=null 
select @newordids=ISNULL(@newordids+',','')+convert(varchar(16),ordid) from @ord 
set @newordids = isnull(@newordids,'')  
end