IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[sys_gen_joblink_updatedata_all]') AND type = 'P')
DROP PROCEDURE [dbo].[sys_gen_joblink_updatedata_all]
GO

CREATE procedure [dbo].[sys_gen_joblink_updatedata_all](
@cmpid int,
@bizid int 
)
as
begin
set nocount on
declare status1 cursor LOCAL FORWARD_ONLY READ_ONLY for
select ws_id  from mps_flowworkstatus,mps_flowwork  
where wrk_bizid=@bizid and wrk_id=ws_swrkid 
open status1
declare @wsid int 
declare @procname varchar(100) 
declare @sql nvarchar(MAX) 

set @sql = N'IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(''[dbo].[order_'+ltrim(rtrim(str(@bizid)))+
'_dolinkdata]'') AND type in (''P'', ''PC''))
DROP PROCEDURE [dbo].[order_'+ltrim(rtrim(str(@bizid)))+'_dolinkdata] ' 
exec(@sql)

set @sql='' 
FETCH NEXT FROM status1 INTO @wsid 
WHILE (@@FETCH_STATUS = 0)
BEGIN
set @procname = '' 
exec sys_gen_joblink_updatedata @cmpid,@bizid,@wsid,@procname output 
if (@procname>'') 
begin
set @sql = @sql+N'
if (@wsid='+ltrim(rtrim(str(@wsid)))+') exec '+@procname+' @cmpid,@useruid,@bizid,@ordbcid,@ordid,@jbid,@ftype,@fbizid,@fordid,@ford2tag,@ford2id,@rtype,@rbizid,@rordid,@rord2tag,@rord2id '
end
 FETCH NEXT FROM status1 INTO @wsid 
END
CLOSE status1
DEALLOCATE status1 
 
set @sql = 'create procedure order_'+ltrim(rtrim(str(@bizid)))+'_dolinkdata(
@cmpid int ,
@useruid int ,
@bizid int ,
@wsid int,
@ordbcid int , 
@ordid bigint,
@jbid bigint,
@ftype varchar(10),
@fbizid int,
@fordid bigint,
@ford2tag int ,
@ford2id bigint,
@rtype varchar(10),
@rbizid int,
@rordid bigint,
@rord2tag int ,
@rord2id bigint 
) 
as 
begin
set nocount on
'+@sql+N'
set nocount off
end
'
execute(@sql)
 
print @sql 
set nocount off
end