IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_do_execute_job_batch]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_do_execute_job_batch]
GO
create procedure [dbo].[mps_do_execute_job_batch](
@cmpid int ,
@branchid int ,
@userdpid int,
@useruid int ,
@userotid int , 
@bizid int ,
@wrkid int,
@ordids varchar(8000)
)
as 
begin
set nocount on

declare @s1 varchar(8000)  
declare @s2 varchar(100) 
declare @i int 
declare @ordid bigint
set @s1 = @ordids 
declare @ord table(ordid bigint)
while (@s1>'') 
begin
set @i = CHARINDEX(',',@s1) 
if (@i>0) 
begin
set @s2 = SUBSTRING(@s1,1,@i-1) 
set @s1 = SUBSTRING(@s1,@i+1,LEN(@s1)-@i) 
end else 
begin
set @s2 = @s1 
set @s1 = ''
end 
if (@s2>'') 
begin
set @ordid = CONVERT(bigint,@s2) 
-- 判断当前ordid可不可以被提交
if (exists (select 1 from mps_job where jb_cmpid=@cmpid and jb_crmid=@bizid and jb_ordid=@ordid and jb_wsid=0 and jb_cuid=@useruid))
begin
if (not exists (select 1 from @ord where ordid=@ordid)) 
begin
insert into @ord(ordid) values(@ordid) 
end 
end 
end
end 

declare @wsid int 
declare @status nvarchar(50) 
set @wsid = 0 
select top 1 @wsid=ws_id , @status=ws_status 
from mps_flowworkstatus 
where ws_swrkid=@wrkid and (ws_tshape!='top' or ws_tshape!='abolish' or ws_tshape!='back' 
or ws_tshape!='end' or ws_tshape!='job' or ws_tshape!='subflow' or ws_tshape!='backany') and 
ws_sql='' 

if (@wsid=0) 
select top 1 @wsid=ws_id , @status=ws_status 
from mps_flowworkstatus 
where ws_swrkid=@wrkid and (ws_tshape!='top' or ws_tshape!='abolish' or ws_tshape!='back' 
or  ws_tshape!='job' or ws_tshape!='subflow' or ws_tshape!='backany') and 
ws_sql='' 

if (@wsid=0) 
select top 1 @wsid=ws_id , @status=ws_status 
from mps_flowworkstatus 
where ws_swrkid=@wrkid and (ws_tshape!='top' or  ws_tshape!='back' 
or  ws_tshape!='job' or ws_tshape!='subflow' or ws_tshape!='backany') and 
ws_sql='' 

if (@wsid=0) return 

declare @ordtype nvarchar(50) 
declare @ordno varchar(50) 
declare @ordtitle nvarchar(100) 
declare @orddate datetime  
declare @ordcreator nvarchar(50) 
declare @ordbcid int 
declare @jbid bigint 

declare @wsids varchar(30)
set @wsids = CONVERT(varchar(10),@wsid) 
declare c1 cursor LOCAL FORWARD_ONLY READ_ONLY for
select distinct jb_bcid,jb_ordtype,jb_ordno,jb_ordtitle,jb_orddate,jb_ordcreator,jb_id  
from mps_job , @ord  
where jb_cmpid=@cmpid and jb_crmid=@bizid and jb_ordid=ordid and 
jb_wrkid=@wrkid and jb_wsid=0 and jb_cuid=@useruid 
open c1
FETCH NEXT FROM c1 INTO @ordbcid,@ordtype,@ordno,@ordtitle,@orddate,@ordcreator,@jbid
WHILE (@@FETCH_STATUS = 0)
BEGIN
exec mps_do_execute_job 
@cmpid  ,
@branchid  ,
@userdpid ,
@useruid  ,
@userotid  , 
@bizid  ,
@ordbcid ,
@ordid ,
@ordtype ,
@ordno ,
@ordtitle ,
@orddate  ,
@ordcreator ,
'' ,
0 ,
0 ,
0 ,
0,
'',
0,
0,
0 ,
0,
@wrkid,
@jbid,
@wsids  , 
@status,
'批量提交',
0 ,
'',
'' 
 FETCH NEXT FROM c1 INTO @ordbcid,@ordtype,@ordno,@ordtitle,@orddate,@ordcreator,@jbid
END
CLOSE c1
DEALLOCATE c1 
set nocount off
end