IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_save_link_data]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_save_link_data]
GO
CREATE procedure [dbo].[mps_save_link_data](
@wsd_wsidint,
@wsd_tobjtypevarchar(20),
@wsd_tobjidint,
@wsd_tobjid2int,
@wsd_sord2tagint,
@wsd_updatesqlnvarchar(max),
@wsd_sqlnvarchar(max) ,
@wsd_keyfield varchar(50)
) as
begin

declare @newid int 
if (@wsd_tobjid<0) 
begin
select top 1 @newid=wsd_id from  mps_flowworkstatus_data where wsd_wsid=@wsd_wsid and wsd_tobjid=@wsd_tobjid 
end else 
begin
select top 1 @newid=wsd_id from  mps_flowworkstatus_data 
where wsd_wsid=@wsd_wsid and wsd_tobjid=@wsd_tobjid and wsd_tobjtype=@wsd_tobjtype and wsd_tobjid2=@wsd_tobjid2 
end
set @newid=isnull(@newid,0) 

if (@wsd_sord2tag<0 and @newid>0) 
begin
update mps_flowworkstatus_data set wsd_use=1 where wsd_id=@newid 
end else 
if (@newid>0 and @wsd_sord2tag>=0)  -- 修改
begin
update mps_flowworkstatus_data set 
wsd_sord2tag = @wsd_sord2tag  ,
wsd_updatesql = @wsd_updatesql ,
wsd_sql = @wsd_sql,
wsd_use = 0  
where wsd_id=@newid 
end else -- 新增
if (@wsd_sord2tag>=0) 
begin
insert into mps_flowworkstatus_data(wsd_wsid,wsd_tobjtype,wsd_tobjid,wsd_tobjid2,wsd_sord2tag,wsd_updatesql,wsd_sql,wsd_use,wsd_keyfield) 
values(@wsd_wsid,@wsd_tobjtype,@wsd_tobjid,@wsd_tobjid2,@wsd_sord2tag,@wsd_updatesql,@wsd_sql,0,@wsd_keyfield)
end  
declare @cmpid int 
declare @bizid int 
select @cmpid = biz_cmpid , @bizid=biz_id 
from mps_flowworkstatus , mps_flowwork , mps_biztype 
where ws_id=@wsd_wsid and ws_swrkid=wrk_id  and wrk_bizid=biz_id 
exec sys_gen_joblink_updatedata_all @cmpid ,@bizid  
end