IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[userfunc_568_wwcorders]') AND type = 'P')
DROP PROCEDURE [dbo].[userfunc_568_wwcorders]
GO
CREATE PROCEDURE userfunc_568_wwcorders(@cmpid int,   @branchid int,  @userdpid int,  @useruid int,   @userotid int,  @usersecurity int,@ctgid int,@ctgid2 int,@objid bigint,@qry_ordid bigint,@qry_cuid int)
as
begin



      set nocount on
      delete from order_240_1 where ord2_ordid=@qry_ordid
      
      insert into order_240_1(ord2_ordid,ord2_tag,ord2_jbid,ord2_jbcrmid,ord2_jbordid,
                            ord2_ordno,ord2_ordhm,ord2_ordkhxm,ord2_ordstatus,ord2_ordwhzt,
                            ord2_ordddzt,ord2_ordsfcj,ord2_sfbg)
      select @qry_ordid,1,j.jb_id,j.jb_crmid,j.jb_ordid,
             o.ord_no ,o.ord_hm ,o.ord_khxm ,o.ord_status ,o.ord_whzt ,
             o.ord_ddzt,o.ord_sfcj,'变更'
      from mps_job as j inner join order_239 as o
      on j.jb_ordid = o.ord_id and j.jb_crmid = 239 and j.jb_wsid = 0
      where o.ord_status != '已完成' and  o.ord_status > '' 
      and j.jb_cuid = @qry_cuid 
      
      select * from order_240_1 where ord2_ordid=@qry_ordid
      set nocount off 


end