IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_load_line_code]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_load_line_code]
GO
create procedure [dbo].[mps_load_line_code](
@twsid int,
@swsid int
) as
begin
set nocount on
-- 保存现在的数据
--if (s1=="" || s1.length<=2 || s1=="工作状态" ||  s1=="流程状态" || s1=="无状态") 
declare @s nvarchar(100) 
select @s=ws_caption from mps_flowworkstatus where ws_id=@twsid 
set @s = ISNULL(@s,'') 
declare @ok int 
set @ok = 0 
if (@s='' or LEN(@s)<=2 or @s='工作状态' or  @s='流程状态' or @s='无状态') set @ok = 1 
if (@ok=0) 
begin
delete mps_flowworkstatus_history where ws_id=@twsid 

insert into mps_flowworkstatus_history(
ws_id,
ws_swrkid,
ws_twrkid,
ws_caption,
ws_condition,
ws_pickvalues,
ws_status,
ws_status_f,
ws_status_ff,
ws_status_r,
ws_key,
ws_key_f,
ws_key_ff,
ws_key_r,
ws_sql,
ws_prevwrkids,
ws_fjbstatus,
ws_bizrelation,
ws_accsource,
ws_acctype,
ws_accdate,
ws_accobjtype,
ws_accobj,
ws_accfid,
ws_accmethod,
ws_accvalue,
ws_accsuspend,
ws_accsql,
ws_tosubflow,
ws_bizid,
ws_title )
select 
ws_id,
ws_swrkid,
ws_twrkid,
ws_caption,
ws_condition,
ws_pickvalues,
ws_status,
ws_status_f,
ws_status_ff,
ws_status_r,
ws_key,
ws_key_f,
ws_key_ff,
ws_key_r,
ws_sql,
ws_prevwrkids,
ws_fjbstatus,
ws_bizrelation,
ws_accsource,
ws_acctype,
ws_accdate,
ws_accobjtype,
ws_accobj,
ws_accfid,
ws_accmethod,
ws_accvalue,
ws_accsuspend,
ws_accsql,
ws_tosubflow ,
wrk_bizid,
flw_title+'/'+wrk_title+'/'+ws_caption 
from mps_flowworkstatus , mps_flowwork , mps_bizflow 
where ws_id=@twsid and ws_swrkid=wrk_id and wrk_flwid=flw_id 
end 

if (@ok=0) 
begin
delete mps_extsqljs_history where cd_objid=@twsid and cd_name in ('wrk.afterlinkapply','wrk.beforelinkapply') 
insert into mps_extsqljs_history(
cd_objid,
cd_objid2,
cd_name,
cd_text)
select 
cd_objid,
cd_objid2,
cd_name,
cd_text
from mps_extsqljs,mps_flowworkstatus 
where 
  ws_id=@twsid and cd_objid=ws_id and cd_name in ('wrk.afterlinkapply','wrk.beforelinkapply')
end 


delete mps_flowworkstatus_data_history 
where wsd_wsid=@twsid  

insert into mps_flowworkstatus_data_history(
wsd_wsid,
wsd_tobjtype,
wsd_tobjid,
wsd_tobjid2,
wsd_sord2tag,
wsd_updatesql,
wsd_sql,
wsd_use ,
wsd_keyfield )
select 
wsd_wsid,
wsd_tobjtype,
wsd_tobjid,
wsd_tobjid2,
wsd_sord2tag,
wsd_updatesql,
wsd_sql,
wsd_use ,
wsd_keyfield 
from mps_flowworkstatus_data   where   wsd_wsid=@twsid 


-- 删除mps_flowworkstatus_data  
delete mps_flowworkstatus_data where   wsd_wsid=@twsid 

-- COPY主数据
update a set 
a.ws_caption = b.ws_caption,
a.ws_condition = b.ws_condition,
a.ws_pickvalues = b.ws_pickvalues,
a.ws_status = b.ws_status,
a.ws_status_f = b.ws_status_f,
a.ws_status_ff = b.ws_status_ff,
a.ws_status_r = b.ws_status_r,
a.ws_key = b.ws_key,
a.ws_key_f = b.ws_key_f,
a.ws_key_ff = b.ws_key_ff,
a.ws_key_r = b.ws_key_r,
a.ws_sql = b.ws_sql,
a.ws_prevwrkids = b.ws_prevwrkids,
a.ws_fjbstatus = b.ws_fjbstatus,
a.ws_bizrelation = b.ws_bizrelation,
a.ws_accsource = b.ws_accsource,
a.ws_acctype = b.ws_acctype,
a.ws_accdate = b.ws_accdate,
a.ws_accobjtype = b.ws_accobjtype,
a.ws_accobj = b.ws_accobj,
a.ws_accfid = b.ws_accfid,
a.ws_accmethod = b.ws_accmethod,
a.ws_accvalue = b.ws_accvalue,
a.ws_accsuspend = b.ws_accsuspend,
a.ws_accsql = b.ws_accsql,
a.ws_tosubflow  = b.ws_tosubflow
from mps_flowworkstatus a , mps_flowworkstatus_history b 
where b.ws_id=@swsid and a.ws_id=@twsid 

-- 处理mps_flowworkstatus_data
insert into mps_flowworkstatus_data(
wsd_wsid,
wsd_tobjtype,
wsd_tobjid,
wsd_tobjid2,
wsd_sord2tag,
wsd_updatesql,
wsd_sql,
wsd_use ,
wsd_keyfield) 
select 
@twsid,
wsd_tobjtype,
wsd_tobjid,
wsd_tobjid2,
wsd_sord2tag,
wsd_updatesql,
wsd_sql,
wsd_use ,
wsd_keyfield
from mps_flowworkstatus_data_history b
where b.wsd_wsid=@swsid 

 
delete mps_extsqljs 
where cd_objid=@twsid and cd_name in ('wrk.afterlinkapply','wrk.beforelinkapply')

insert into mps_extsqljs(
cd_objid,
cd_objid2,
cd_name,
cd_text)
select 
@twsid,
cd_objid2,
cd_name,
cd_text
from mps_extsqljs_history
where 
  cd_objid=@swsid and cd_name in ('wrk.afterlinkapply','wrk.beforelinkapply')


delete mps_extsqljs_history 
where cd_objid=@swsid and cd_name in ('wrk.afterlinkapply','wrk.beforelinkapply')

delete mps_flowworkstatus_data_history 
where wsd_wsid=@swsid 

delete mps_flowworkstatus_history
where ws_id=@swsid 

select * from mps_flowworkstatus where ws_id=@twsid 
set nocount off
end