IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_g_work_status]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_g_work_status]
GO
CREATE procedure [dbo].[mps_g_work_status] 
@wsid int ,
@swrkid int,
@twrkid int as
begin
set nocount on
declare @wsid1 int 
declare @wrkid int 
set @wsid1=@wsid 
if (@wsid1<=0) 
select top 1 @wsid1=ws_id , @wrkid = ws_swrkid from mps_flowworkstatus where ws_id=@wsid and ws_swrkid=@swrkid and ws_twrkid=@twrkid 
set @wsid1 = isnull(@wsid1,0) 
if (@wsid1=0) select top 1 @wsid1=ws_id , @wrkid = ws_swrkid from mps_flowworkstatus where  ws_swrkid=@swrkid and ws_twrkid=@twrkid 
set @wsid1 = isnull(@wsid1,0) 
if (@wsid1<=0) 
begin
insert into mps_flowworkstatus(ws_swrkid,ws_twrkid,ws_caption) values(@swrkid,@twrkid,'无状态') 
select top 1 @wsid1=ws_id from mps_flowworkstatus where ws_swrkid=@swrkid and ws_twrkid=@twrkid and ws_caption='无状态'   order by ws_id desc 
set @wrkid=@swrkid 
end
select * from mps_flowworkstatus where ws_id=@wsid1  
-- 
declare @t table(wrkid int)
insert into @t(wrkid) 
select distinct ws_swrkid from mps_flowworkstatus where ws_twrkid=@wrkid 
select a.* from  mps_flowwork a , @t where wrk_id=wrkid 
set nocount off
end