IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_g_flowwork]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_g_flowwork]
GO
CREATE procedure [dbo].[mps_g_flowwork] 
@crmid int,
@flwid int,
@wrkid int,
@shape varchar(30),
@nodetext nvarchar(100) 
  as
begin
set nocount on
declare @wrkid1 int 
select @wrkid1 = wrk_id from mps_flowwork where wrk_bizid=@crmid and wrk_flwid=@flwid and wrk_id=@wrkid 
set @wrkid1 = isnull(@wrkid1,0) 
if (@wrkid1<=0) 
begin
insert into mps_flowwork(wrk_bizid,wrk_flwid,wrk_shape,wrk_title) values(@crmid,@flwid,@shape,@nodetext) 
select top 1 @wrkid1 = wrk_id from mps_flowwork where wrk_bizid=@crmid and wrk_flwid=@flwid and wrk_shape=@shape and wrk_title=@nodetext order by wrk_id desc  
end  
-- 调整节点状态
if (@wrkid<0) 
begin
update mps_flowwork_status set ws_swrkid=@wrkid1 where ws_swrkid=@wrkid 
update mps_flowwork_status set ws_twrkid=@wrkid1 where ws_twrkid=@wrkid 
end 
select * from mps_flowwork where wrk_id=@wrkid1 

-- 
declare @t table(wrkid int)
insert into @t(wrkid) 
select distinct ws_swrkid from mps_flowworkstatus where ws_twrkid=@wrkid1 
select a.* from  mps_flowwork a , @t where wrk_id=wrkid

set nocount off
end