IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_gen_wrk_message_end]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_gen_wrk_message_end]
GO
create procedure [dbo].[mps_gen_wrk_message_end](
@cmpid int,
@bizid int,
@wrkid int
)
as
begin
set nocount on
declare @s1 nvarchar(100) 
declare @i int 
declare @tbname varchar(50)
declare @dbname varchar(50)
declare @fkey   varchar(50) 
exec sys_sql_getTableNamePk @cmpid,'biz',@bizid,0,@tbname output,@fkey output ,@dbname output 
declare @wrk varchar(10) 
set @wrk = convert(varchar(10),@wrkid) 

declare @sql nvarchar(MAX) 


set @sql = 'drop procedure '+@tbname+'_endmessage_'+@wrk 
if (dbo.checkprocedure(@tbname+'_endmessage_'+@wrk)=0) execute(@sql) 

set @sql = N'
-- 生成时间:'+convert(varchar(20),getdate(),120)+N' by ebs-core  
'
if (dbo.checkprocedure(@tbname+'_endmessage_'+@wrk)=0) set @sql=@sql+'alter ' else set @sql=@sql+'create '
set @sql = @sql+' procedure '+@tbname+N'_endmessage_'+@wrk+'(
     @cmpid int, 
     @ordid bigint,
     @jbid bigint
  ) 
  as  
  begin  
set nocount on  
'
declare @icc int 
set @icc = 0 
declare @msgcount int , @msgfld varchar(50) , @datasource int,@csttag int
declare @mpname nvarchar(100) 
declare @mpcaption nvarchar(100) 
declare @mpid int , @emp varchar(20) , @mb nvarchar(4000) , @wxtype varchar(10) , @wxcode varchar(50)  , @wxlen varchar(10) 

set @sql=@sql+char(13)+char(10)+'------ 给执行人发消息  ----------  '+char(13)+char(10)

--- 以下处理 工作开始 给执行人发消息 
set @msgcount = 0 
set @mpid=0 ; set @emp='' ; set @mb='' ; set @wxtype='' ; set @wxcode=''  ; set @wxlen='0' 
select @mpid=wrk_exec_end_msgid , @emp=mp_scope , @mb=mp_gzhtext , @wxtype=wx_type , @wxcode=wx_code ,@mpname=mp_name,@mpcaption=mp_caption  
from mps_flowwork , ebs_kernel.dbo.pub_messagetemplate, ebs_kernel.dbo.pub_weixin 
where wrk_id=@wrkid and wrk_exec_end_msgid>0 and mp_id=wrk_exec_end_msgid and len(mp_gzhtext)>10 and mp_gzhid=wx_id 
set @wxlen = convert(varchar(10),len(@wxcode)+2)  
if (@mpid>0) 
begin
set @icc = @icc + 1
set @msgcount = @msgcount+1
set @sql=@sql+'
insert into ebs_kernel.dbo.pub_message(msg_cmpid,msg_mpid,msg_text,msg_senddate,msg_type,msg_target,msg_cuid)  
select @cmpid,'+convert(varchar(10),@mpid)+','+@mb+',getdate(),'''+@wxtype+''',
case when cu_gzh1 like '''+@wxcode+'%''  then substring(cu_gzh1,'+@wxlen+',50) 
when cu_gzh2 like '''+@wxcode+'%''  then substring(cu_gzh2,'+@wxlen+',50) 
when cu_gzh3 like '''+@wxcode+'%''  then substring(cu_gzh3,'+@wxlen+',50) 
when cu_gzh4 like '''+@wxcode+'%''  then substring(cu_gzh4,'+@wxlen+',50) 
when cu_gzh5 like '''+@wxcode+'%''  then substring(cu_gzh5,'+@wxlen+',50) end , cu_id 
from ' 
if (CHARINDEX('ord_',@mb)>0) set @sql=@sql+@dbname+@tbname +','
if (CHARINDEX('jb_',@mb)>0) set @sql=@sql+'mps_job,' 
set @sql=@sql+'mps_login,mps_job_users 
where '
if (CHARINDEX('ord_',@mb)>0) set @sql=@sql+' ord_id=@ordid and '
if (CHARINDEX('jb_',@mb)>0) set @sql=@sql+' jb_id=@jbid and ' 
set @sql=@sql+' jp_type=''wrkexecutor'' and jp_jbid=@jbid and jp_cuid=cu_id and 
cu_type=0 and (cu_gzh1 like '''+@wxcode+'%'' or cu_gzh2 like '''+@wxcode+'%'' or '+
'cu_gzh3 like '''+@wxcode+'%'' or cu_gzh4 like '''+@wxcode+'%'' or cu_gzh5 like '''+@wxcode+'%'') ' +char(13)+char(10)
end 

set @mpid=0 ; set @emp='' ; set @mb='' ; set @wxtype='' ; set @wxcode=''  ; set @wxlen='0' 
select @mpid=wrk_exec_end_msgid , @emp=mp_scope , @mb=mp_qyhtext , @wxtype=wx_type , @wxcode=wx_code ,@mpname=mp_name,@mpcaption=mp_caption  
from mps_flowwork , ebs_kernel.dbo.pub_messagetemplate, ebs_kernel.dbo.pub_weixin 
where wrk_id=@wrkid and wrk_exec_end_msgid>0 and mp_id=wrk_exec_end_msgid and len(mp_qyhtext)>10 and mp_qyhid=wx_id 
set @wxlen = convert(varchar(10),len(@wxcode)+2)  
if (@mpid>0) 
begin
set @icc = @icc + 1
set @msgcount = @msgcount+1
set @sql=@sql+'
insert into ebs_kernel.dbo.pub_message(msg_cmpid,msg_mpid,msg_text,msg_senddate,msg_type,msg_target,msg_cuid)  
select @cmpid,'+convert(varchar(10),@mpid)+','+@mb+',getdate(),'''+@wxtype+''',substring(cu_qyh , '+@wxlen+',50)    , cu_id 
from ' 
if (CHARINDEX('ord_',@mb)>0) set @sql=@sql+@dbname+@tbname +','
if (CHARINDEX('jb_',@mb)>0) set @sql=@sql+'mps_job,' 
set @sql=@sql+'mps_login,mps_job_users 
where '
if (CHARINDEX('ord_',@mb)>0) set @sql=@sql+' ord_id=@ordid and '
if (CHARINDEX('jb_',@mb)>0) set @sql=@sql+' jb_id=@jbid and ' 
set @sql=@sql+' jp_type=''wrkexecutor'' and jp_jbid=@jbid and jp_cuid=cu_id and 
cu_type=0 and cu_qyh>'''' ' +char(13)+char(10) 
end

set @mpid=0 ; set @emp='' ; set @mb='' ; set @wxtype='' ; set @wxcode=''  ; set @wxlen='0' 
select @mpid=wrk_exec_end_msgid , @emp=mp_scope , @mb=mp_dingtext , @wxtype=wx_type , @wxcode=wx_code ,@mpname=mp_name,@mpcaption=mp_caption  
from mps_flowwork , ebs_kernel.dbo.pub_messagetemplate, ebs_kernel.dbo.pub_weixin 
where wrk_id=@wrkid and wrk_exec_end_msgid>0 and mp_id=wrk_exec_end_msgid and len(mp_dingtext)>10 and mp_dingid=wx_id 
set @wxlen = convert(varchar(10),len(@wxcode)+2)  
if (@mpid>0)  
begin
set @icc = @icc + 1
set @msgcount = @msgcount+1
set @sql=@sql+'
insert into ebs_kernel.dbo.pub_message(msg_cmpid,msg_mpid,msg_text,msg_senddate,msg_type,msg_target,msg_cuid)  
select @cmpid,'+convert(varchar(10),@mpid)+','+@mb+',getdate(),'''+@wxtype+''',substring(cu_ding , '+@wxlen+',50)    , cu_id 
from ' 
if (CHARINDEX('ord_',@mb)>0) set @sql=@sql+@dbname+@tbname +','
if (CHARINDEX('jb_',@mb)>0) set @sql=@sql+'mps_job,' 
set @sql=@sql+'mps_login,mps_job_users 
where '
if (CHARINDEX('ord_',@mb)>0) set @sql=@sql+' ord_id=@ordid and '
if (CHARINDEX('jb_',@mb)>0) set @sql=@sql+' jb_id=@jbid and ' 
set @sql=@sql+' jp_type=''wrkexecutor'' and jp_jbid=@jbid and jp_cuid=cu_id and 
cu_type=0 and cu_ding>'''' ' +char(13)+char(10) 
end

set @mpid=0 ; set @emp='' ; set @mb='' ; set @wxtype='' ; set @wxcode=''  ; set @wxlen='0' 
select @mpid=wrk_exec_end_msgid , @emp=mp_scope , @mb=mp_dayutext , @wxtype=wx_type , @wxcode=wx_code ,@mpname=mp_name,@mpcaption=mp_caption  
from mps_flowwork , ebs_kernel.dbo.pub_messagetemplate, ebs_kernel.dbo.pub_weixin 
where wrk_id=@wrkid and wrk_exec_end_msgid>0 and mp_id=wrk_exec_end_msgid and len(mp_dayutext)>10 and mp_dayuid=wx_id 
set @wxlen = convert(varchar(10),len(@wxcode)+2)  
if (@mpid>0)  
begin
set @icc = @icc + 1
set @sql=@sql+'
insert into ebs_kernel.dbo.pub_message(msg_cmpid,msg_mpid,msg_text,msg_senddate,msg_type,msg_target,msg_cuid)  
select @cmpid,'+convert(varchar(10),@mpid)+','+@mb+',getdate(),'''+@wxtype+''',cu_mobile  , cu_id 
from ' 
if (CHARINDEX('ord_',@mb)>0) set @sql=@sql+@dbname+@tbname +','
if (CHARINDEX('jb_',@mb)>0) set @sql=@sql+'mps_job,' 
set @sql=@sql+'mps_login,mps_job_users 
where '
if (CHARINDEX('ord_',@mb)>0) set @sql=@sql+' ord_id=@ordid and '
if (CHARINDEX('jb_',@mb)>0) set @sql=@sql+' jb_id=@jbid and ' 
set @sql=@sql+' jp_type=''wrkexecutor'' and jp_jbid=@jbid and jp_cuid=cu_id and cu_type=0 and len(cu_mobile)>=11 '+char(13)+char(10) 
if (@msgcount>0) 
set @sql=@sql+'and (cu_ding+cu_qyh+cu_gzh1+cu_gzh2+cu_gzh3+cu_gzh4+cu_gzh5)='''' ' +char(13)+char(10)  
end 


set @mpid=0 ; set @emp='' ; set @mb='' ; set @wxtype='' ; set @wxcode=''  ; set @wxlen='0' 
select @mpid=wrk_exec_end_msgid , @emp=mp_scope , @mb=mp_pctext  , @s1=mp_caption ,@mpname=mp_name,@mpcaption=mp_caption  
from mps_flowwork , ebs_kernel.dbo.pub_messagetemplate 
where wrk_id=@wrkid and wrk_exec_end_msgid>0 and mp_id=wrk_exec_end_msgid and len(mp_pctext)>10  
if (@mpid>0) 
begin
set @icc = @icc + 1
set @sql=@sql+'
insert into mps_alarm_message(
 almmsg_cmpid,almmsg_almid,almmsg_identkey,almmsg_type,almmsg_title,almmsg_category,almmsg_cuid,almmsg_text,almmsg_objid,almmsg_jbid,almmsg_objtype) 
select @cmpid,'+convert(varchar(10),@mpid)+','','',''message'','''+@mpcaption+''','''+@mpname+''',cu_id,'+@mb+' , @ordid,@jbid ,''biz'' 
from ' 
if (CHARINDEX('ord_',@mb)>0) set @sql=@sql+@dbname+@tbname +','
if (CHARINDEX('jb_',@mb)>0) set @sql=@sql+'mps_job,' 
set @sql=@sql+'mps_login,mps_job_users 
where '
if (CHARINDEX('ord_',@mb)>0) set @sql=@sql+' ord_id=@ordid and '
if (CHARINDEX('jb_',@mb)>0) set @sql=@sql+' jb_id=@jbid and ' 
set @sql=@sql+' jp_type=''wrkexecutor'' and jp_jbid=@jbid and jp_cuid=cu_id and cu_type=0  '+char(13)+char(10) 
end 

set @sql=@sql+char(13)+char(10)+'------ 给监督人发消息  ----------  '+char(13)+char(10)
--- 以下处理 工作开始 给监督人发消息 
set @msgcount = 0 
set @mpid=0 ; set @emp='' ; set @mb='' ; set @wxtype='' ; set @wxcode=''  ; set @wxlen='0' 
select @mpid=wrk_audit_end_msgid , @emp=mp_scope , @mb=mp_gzhtext , @wxtype=wx_type , @wxcode=wx_code ,@mpname=mp_name,@mpcaption=mp_caption  
from mps_flowwork , ebs_kernel.dbo.pub_messagetemplate, ebs_kernel.dbo.pub_weixin 
where wrk_id=@wrkid and wrk_audit_end_msgid>0 and mp_id=wrk_audit_end_msgid and len(mp_gzhtext)>10 and mp_gzhid=wx_id 
set @wxlen = convert(varchar(10),len(@wxcode)+2)  
if (@mpid>0) 
begin
set @icc = @icc + 1
set @msgcount = @msgcount+1
set @sql=@sql+'
insert into ebs_kernel.dbo.pub_message(msg_cmpid,msg_mpid,msg_text,msg_senddate,msg_type,msg_target,msg_cuid)  
select @cmpid,'+convert(varchar(10),@mpid)+','+@mb+',getdate(),'''+@wxtype+''',
case when cu_gzh1 like '''+@wxcode+'%''  then substring(cu_gzh1,'+@wxlen+',50) 
when cu_gzh2 like '''+@wxcode+'%''  then substring(cu_gzh2,'+@wxlen+',50) 
when cu_gzh3 like '''+@wxcode+'%''  then substring(cu_gzh3,'+@wxlen+',50) 
when cu_gzh4 like '''+@wxcode+'%''  then substring(cu_gzh4,'+@wxlen+',50) 
when cu_gzh5 like '''+@wxcode+'%''  then substring(cu_gzh5,'+@wxlen+',50) end , cu_id 
from ' 
if (CHARINDEX('ord_',@mb)>0) set @sql=@sql+@dbname+@tbname +','
if (CHARINDEX('jb_',@mb)>0) set @sql=@sql+'mps_job,' 
set @sql=@sql+'mps_login,mps_job_users 
where '
if (CHARINDEX('ord_',@mb)>0) set @sql=@sql+' ord_id=@ordid and '
if (CHARINDEX('jb_',@mb)>0) set @sql=@sql+' jb_id=@jbid and ' 
set @sql=@sql+' jp_type=''wrkaudit'' and jp_jbid=@jbid and jp_cuid=cu_id and 
cu_type=0 and (cu_gzh1 like '''+@wxcode+'%'' or cu_gzh2 like '''+@wxcode+'%'' or '+
'cu_gzh3 like '''+@wxcode+'%'' or cu_gzh4 like '''+@wxcode+'%'' or cu_gzh5 like '''+@wxcode+'%'') ' +char(13)+char(10)
end 

set @mpid=0 ; set @emp='' ; set @mb='' ; set @wxtype='' ; set @wxcode=''  ; set @wxlen='0' 
select @mpid=wrk_audit_end_msgid , @emp=mp_scope , @mb=mp_qyhtext , @wxtype=wx_type , @wxcode=wx_code ,@mpname=mp_name,@mpcaption=mp_caption  
from mps_flowwork , ebs_kernel.dbo.pub_messagetemplate, ebs_kernel.dbo.pub_weixin 
where wrk_id=@wrkid and wrk_audit_end_msgid>0 and mp_id=wrk_audit_end_msgid and len(mp_qyhtext)>10 and mp_qyhid=wx_id 
set @wxlen = convert(varchar(10),len(@wxcode)+2)  
if (@mpid>0)  
begin
set @icc = @icc + 1
set @msgcount = @msgcount+1
set @sql=@sql+'
insert into ebs_kernel.dbo.pub_message(msg_cmpid,msg_mpid,msg_text,msg_senddate,msg_type,msg_target,msg_cuid)  
select @cmpid,'+convert(varchar(10),@mpid)+','+@mb+',getdate(),'''+@wxtype+''',substring(cu_qyh , '+@wxlen+',50)    , cu_id 
from ' 
if (CHARINDEX('ord_',@mb)>0) set @sql=@sql+@dbname+@tbname +','
if (CHARINDEX('jb_',@mb)>0) set @sql=@sql+'mps_job,' 
set @sql=@sql+'mps_login,mps_job_users 
where '
if (CHARINDEX('ord_',@mb)>0) set @sql=@sql+' ord_id=@ordid and '
if (CHARINDEX('jb_',@mb)>0) set @sql=@sql+' jb_id=@jbid and ' 
set @sql=@sql+' jp_type=''wrkaudit'' and jp_jbid=@jbid and jp_cuid=cu_id and 
cu_type=0 and cu_qyh>'''' ' +char(13)+char(10) 
end

set @mpid=0 ; set @emp='' ; set @mb='' ; set @wxtype='' ; set @wxcode=''  ; set @wxlen='0' 
select @mpid=wrk_audit_end_msgid , @emp=mp_scope , @mb=mp_dingtext , @wxtype=wx_type , @wxcode=wx_code ,@mpname=mp_name,@mpcaption=mp_caption  
from mps_flowwork , ebs_kernel.dbo.pub_messagetemplate, ebs_kernel.dbo.pub_weixin 
where wrk_id=@wrkid and wrk_audit_end_msgid>0 and mp_id=wrk_audit_end_msgid and len(mp_dingtext)>10 and mp_dingid=wx_id 
set @wxlen = convert(varchar(10),len(@wxcode)+2)  
if (@mpid>0)  
begin
set @icc = @icc + 1
set @msgcount = @msgcount+1
set @sql=@sql+'
insert into ebs_kernel.dbo.pub_message(msg_cmpid,msg_mpid,msg_text,msg_senddate,msg_type,msg_target,msg_cuid)  
select @cmpid,'+convert(varchar(10),@mpid)+','+@mb+',getdate(),'''+@wxtype+''',substring(cu_ding , '+@wxlen+',50)    , cu_id 
from ' 
if (CHARINDEX('ord_',@mb)>0) set @sql=@sql+@dbname+@tbname +','
if (CHARINDEX('jb_',@mb)>0) set @sql=@sql+'mps_job,' 
set @sql=@sql+'mps_login,mps_job_users 
where '
if (CHARINDEX('ord_',@mb)>0) set @sql=@sql+' ord_id=@ordid and '
if (CHARINDEX('jb_',@mb)>0) set @sql=@sql+' jb_id=@jbid and ' 
set @sql=@sql+' jp_type=''wrkaudit'' and jp_jbid=@jbid and jp_cuid=cu_id and 
cu_type=0 and cu_ding>'''' ' +char(13)+char(10) 
end

set @mpid=0 ; set @emp='' ; set @mb='' ; set @wxtype='' ; set @wxcode=''  ; set @wxlen='0' 
select @mpid=wrk_audit_end_msgid , @emp=mp_scope , @mb=mp_dayutext , @wxtype=wx_type , @wxcode=wx_code ,@mpname=mp_name,@mpcaption=mp_caption  
from mps_flowwork , ebs_kernel.dbo.pub_messagetemplate, ebs_kernel.dbo.pub_weixin 
where wrk_id=@wrkid and wrk_audit_end_msgid>0 and mp_id=wrk_audit_end_msgid and len(mp_dayutext)>10 and mp_dayuid=wx_id 
set @wxlen = convert(varchar(10),len(@wxcode)+2)  
if (@mpid>0)  
begin
set @icc = @icc + 1
set @sql=@sql+'
insert into ebs_kernel.dbo.pub_message(msg_cmpid,msg_mpid,msg_text,msg_senddate,msg_type,msg_target,msg_cuid)  
select @cmpid,'+convert(varchar(10),@mpid)+','+@mb+',getdate(),'''+@wxtype+''',cu_mobile  , cu_id 
from ' 
if (CHARINDEX('ord_',@mb)>0) set @sql=@sql+@dbname+@tbname +','
if (CHARINDEX('jb_',@mb)>0) set @sql=@sql+'mps_job,' 
set @sql=@sql+'mps_login,mps_job_users 
where '
if (CHARINDEX('ord_',@mb)>0) set @sql=@sql+' ord_id=@ordid and '
if (CHARINDEX('jb_',@mb)>0) set @sql=@sql+' jb_id=@jbid and ' 
set @sql=@sql+' jp_type=''wrkaudit'' and jp_jbid=@jbid and jp_cuid=cu_id and cu_type=0 and len(cu_mobile)>=11 '+char(13)+char(10) 
if (@msgcount>0) 
set @sql=@sql+'and (cu_ding+cu_qyh+cu_gzh1+cu_gzh2+cu_gzh3+cu_gzh4+cu_gzh5)='''' ' +char(13)+char(10)  
end

set @mpid=0 ; set @emp='' ; set @mb='' ; set @wxtype='' ; set @wxcode=''  ; set @wxlen='0' 
select @mpid=wrk_audit_end_msgid , @emp=mp_scope , @mb=mp_pctext  , @s1=mp_caption ,@mpname=mp_name,@mpcaption=mp_caption  
from mps_flowwork , ebs_kernel.dbo.pub_messagetemplate 
where wrk_id=@wrkid and wrk_audit_end_msgid>0 and mp_id=wrk_audit_end_msgid and len(mp_pctext)>10  
if (@mpid>0) 
begin
set @icc = @icc + 1
set @sql=@sql+'
insert into mps_alarm_message(
 almmsg_cmpid,almmsg_almid,almmsg_identkey,almmsg_type,almmsg_title,almmsg_category,almmsg_cuid,almmsg_text,almmsg_objid,almmsg_jbid,almmsg_objtype) 
select @cmpid,'+convert(varchar(10),@mpid)+','','',''message'','''+@mpcaption+''','''+@mpname+''',cu_id,'+@mb+' , @ordid,@jbid ,''biz'' 
from ' 
if (CHARINDEX('ord_',@mb)>0) set @sql=@sql+@dbname+@tbname +','
if (CHARINDEX('jb_',@mb)>0) set @sql=@sql+'mps_job,' 
set @sql=@sql+'mps_login,mps_job_users 
where '
if (CHARINDEX('ord_',@mb)>0) set @sql=@sql+' ord_id=@ordid and '
if (CHARINDEX('jb_',@mb)>0) set @sql=@sql+' jb_id=@jbid and ' 
set @sql=@sql+' jp_type=''wrkaudit'' and jp_jbid=@jbid and jp_cuid=cu_id and cu_type=0  '+char(13)+char(10) 
end 

set @sql=@sql+char(13)+char(10)+'------ 给协作人发消息  ----------  '+char(13)+char(10)
--- 以下处理 工作开始 给协作人发消息 
set @msgcount = 0 
set @mpid=0 ; set @emp='' ; set @mb='' ; set @wxtype='' ; set @wxcode=''  ; set @wxlen='0' 
select @mpid=wrk_coop_end_msgid , @emp=mp_scope , @mb=mp_gzhtext , @wxtype=wx_type , @wxcode=wx_code ,@mpname=mp_name,@mpcaption=mp_caption  
from mps_flowwork , ebs_kernel.dbo.pub_messagetemplate, ebs_kernel.dbo.pub_weixin 
where wrk_id=@wrkid and wrk_coop_end_msgid>0 and mp_id=wrk_coop_end_msgid and len(mp_gzhtext)>10 and mp_gzhid=wx_id 
set @wxlen = convert(varchar(10),len(@wxcode)+2)  
if (@mpid>0) 
begin
set @icc = @icc + 1
set @msgcount = @msgcount+1
set @sql=@sql+'
insert into ebs_kernel.dbo.pub_message(msg_cmpid,msg_mpid,msg_text,msg_senddate,msg_type,msg_target,msg_cuid)  
select @cmpid,'+convert(varchar(10),@mpid)+','+@mb+',getdate(),'''+@wxtype+''',
case when cu_gzh1 like '''+@wxcode+'%''  then substring(cu_gzh1,'+@wxlen+',50) 
when cu_gzh2 like '''+@wxcode+'%''  then substring(cu_gzh2,'+@wxlen+',50) 
when cu_gzh3 like '''+@wxcode+'%''  then substring(cu_gzh3,'+@wxlen+',50) 
when cu_gzh4 like '''+@wxcode+'%''  then substring(cu_gzh4,'+@wxlen+',50) 
when cu_gzh5 like '''+@wxcode+'%''  then substring(cu_gzh5,'+@wxlen+',50) end , cu_id 
from ' 
if (CHARINDEX('ord_',@mb)>0) set @sql=@sql+@dbname+@tbname +','
if (CHARINDEX('jb_',@mb)>0) set @sql=@sql+'mps_job,' 
set @sql=@sql+'mps_login,mps_job_users 
where '
if (CHARINDEX('ord_',@mb)>0) set @sql=@sql+' ord_id=@ordid and '
if (CHARINDEX('jb_',@mb)>0) set @sql=@sql+' jb_id=@jbid and ' 
set @sql=@sql+' jp_type=''wrkcoops'' and jp_jbid=@jbid and jp_cuid=cu_id and 
cu_type=0 and (cu_gzh1 like '''+@wxcode+'%'' or cu_gzh2 like '''+@wxcode+'%'' or '+
'cu_gzh3 like '''+@wxcode+'%'' or cu_gzh4 like '''+@wxcode+'%'' or cu_gzh5 like '''+@wxcode+'%'') ' +char(13)+char(10)
end 

set @mpid=0 ; set @emp='' ; set @mb='' ; set @wxtype='' ; set @wxcode=''  ; set @wxlen='0' 
select @mpname=mp_name,@mpcaption=mp_caption ,@mpid=wrk_coop_end_msgid , @emp=mp_scope , @mb=mp_qyhtext , @wxtype=wx_type , @wxcode=wx_code 
from mps_flowwork , ebs_kernel.dbo.pub_messagetemplate, ebs_kernel.dbo.pub_weixin 
where wrk_id=@wrkid and wrk_coop_end_msgid>0 and mp_id=wrk_coop_end_msgid and len(mp_qyhtext)>10 and mp_qyhid=wx_id 
set @wxlen = convert(varchar(10),len(@wxcode)+2)  
if (@mpid>0)  
begin
set @icc = @icc + 1
set @msgcount = @msgcount+1
set @sql=@sql+'
insert into ebs_kernel.dbo.pub_message(msg_cmpid,msg_mpid,msg_text,msg_senddate,msg_type,msg_target,msg_cuid)  
select @cmpid,'+convert(varchar(10),@mpid)+','+@mb+',getdate(),'''+@wxtype+''',substring(cu_qyh , '+@wxlen+',50)    , cu_id 
from ' 
if (CHARINDEX('ord_',@mb)>0) set @sql=@sql+@dbname+@tbname +','
if (CHARINDEX('jb_',@mb)>0) set @sql=@sql+'mps_job,' 
set @sql=@sql+'mps_login,mps_job_users 
where '
if (CHARINDEX('ord_',@mb)>0) set @sql=@sql+' ord_id=@ordid and '
if (CHARINDEX('jb_',@mb)>0) set @sql=@sql+' jb_id=@jbid and ' 
set @sql=@sql+' jp_type=''wrkcoops'' and jp_jbid=@jbid and jp_cuid=cu_id and 
cu_type=0 and cu_qyh>'''' ' +char(13)+char(10) 
end

set @mpid=0 ; set @emp='' ; set @mb='' ; set @wxtype='' ; set @wxcode=''  ; set @wxlen='0' 
select @mpname=mp_name,@mpcaption=mp_caption ,@mpid=wrk_coop_end_msgid , @emp=mp_scope , @mb=mp_dingtext , @wxtype=wx_type , @wxcode=wx_code 
from mps_flowwork , ebs_kernel.dbo.pub_messagetemplate, ebs_kernel.dbo.pub_weixin 
where wrk_id=@wrkid and wrk_coop_end_msgid>0 and mp_id=wrk_coop_end_msgid and len(mp_dingtext)>10 and mp_dingid=wx_id 
set @wxlen = convert(varchar(10),len(@wxcode)+2)  
if (@mpid>0)  
begin
set @icc = @icc + 1
set @msgcount = @msgcount+1
set @sql=@sql+'
insert into ebs_kernel.dbo.pub_message(msg_cmpid,msg_mpid,msg_text,msg_senddate,msg_type,msg_target,msg_cuid)  
select @cmpid,'+convert(varchar(10),@mpid)+','+@mb+',getdate(),'''+@wxtype+''',substring(cu_ding , '+@wxlen+',50)    , cu_id 
from ' 
if (CHARINDEX('ord_',@mb)>0) set @sql=@sql+@dbname+@tbname +','
if (CHARINDEX('jb_',@mb)>0) set @sql=@sql+'mps_job,' 
set @sql=@sql+'mps_login,mps_job_users 
where '
if (CHARINDEX('ord_',@mb)>0) set @sql=@sql+' ord_id=@ordid and '
if (CHARINDEX('jb_',@mb)>0) set @sql=@sql+' jb_id=@jbid and ' 
set @sql=@sql+' jp_type=''wrkcoops'' and jp_jbid=@jbid and jp_cuid=cu_id and 
cu_type=0 and cu_ding>'''' ' +char(13)+char(10) 
end

set @mpid=0 ; set @emp='' ; set @mb='' ; set @wxtype='' ; set @wxcode=''  ; set @wxlen='0' 
select @mpname=mp_name,@mpcaption=mp_caption ,@mpid=wrk_coop_end_msgid , @emp=mp_scope , @mb=mp_dayutext , @wxtype=wx_type , @wxcode=wx_code 
from mps_flowwork , ebs_kernel.dbo.pub_messagetemplate, ebs_kernel.dbo.pub_weixin 
where wrk_id=@wrkid and wrk_coop_end_msgid>0 and mp_id=wrk_coop_end_msgid and len(mp_dayutext)>10 and mp_dayuid=wx_id 
set @wxlen = convert(varchar(10),len(@wxcode)+2)  
if (@mpid>0)  
begin
set @icc = @icc + 1
set @sql=@sql+'
insert into ebs_kernel.dbo.pub_message(msg_cmpid,msg_mpid,msg_text,msg_senddate,msg_type,msg_target,msg_cuid)  
select @cmpid,'+convert(varchar(10),@mpid)+','+@mb+',getdate(),'''+@wxtype+''',cu_mobile  , cu_id 
from ' 
if (CHARINDEX('ord_',@mb)>0) set @sql=@sql+@dbname+@tbname +','
if (CHARINDEX('jb_',@mb)>0) set @sql=@sql+'mps_job,' 
set @sql=@sql+'mps_login,mps_job_users 
where '
if (CHARINDEX('ord_',@mb)>0) set @sql=@sql+' ord_id=@ordid and '
if (CHARINDEX('jb_',@mb)>0) set @sql=@sql+' jb_id=@jbid and ' 
set @sql=@sql+' jp_type=''wrkcoops'' and jp_jbid=@jbid and jp_cuid=cu_id and cu_type=0 and len(cu_mobile)>=11 '+char(13)+char(10) 
if (@msgcount>0) 
set @sql=@sql+'and (cu_ding+cu_qyh+cu_gzh1+cu_gzh2+cu_gzh3+cu_gzh4+cu_gzh5)='''' ' +char(13)+char(10)  
end

set @mpid=0 ; set @emp='' ; set @mb='' ; set @wxtype='' ; set @wxcode=''  ; set @wxlen='0' 
select @mpname=mp_name,@mpcaption=mp_caption ,@mpid=wrk_coop_end_msgid , @emp=mp_scope , @mb=mp_pctext  , @s1=mp_caption 
from mps_flowwork , ebs_kernel.dbo.pub_messagetemplate 
where wrk_id=@wrkid and wrk_coop_end_msgid>0 and mp_id=wrk_coop_end_msgid and len(mp_pctext)>10  
if (@mpid>0) 
begin
set @icc = @icc + 1
set @sql=@sql+'
insert into mps_alarm_message(
 almmsg_cmpid,almmsg_almid,almmsg_identkey,almmsg_type,almmsg_title,almmsg_category,almmsg_cuid,almmsg_text,almmsg_objid,almmsg_jbid,almmsg_objtype) 
select @cmpid,'+convert(varchar(10),@mpid)+','','',''message'','''+@mpcaption+''','''+@mpname+''',cu_id,'+@mb+' , @ordid,@jbid ,''biz'' 
from ' 
if (CHARINDEX('ord_',@mb)>0) set @sql=@sql+@dbname+@tbname +','
if (CHARINDEX('jb_',@mb)>0) set @sql=@sql+'mps_job,' 
set @sql=@sql+'mps_login,mps_job_users 
where '
if (CHARINDEX('ord_',@mb)>0) set @sql=@sql+' ord_id=@ordid and '
if (CHARINDEX('jb_',@mb)>0) set @sql=@sql+' jb_id=@jbid and ' 
set @sql=@sql+' jp_type=''wrkcoops'' and jp_jbid=@jbid and jp_cuid=cu_id and cu_type=0  '+char(13)+char(10) 
end 

set @sql=@sql+char(13)+char(10)+'------ 给知会人发消息  ----------  '+char(13)+char(10)
--- 以下处理 工作开始 给知会人发消息 
set @msgcount = 0 
set @mpid=0 ; set @emp='' ; set @mb='' ; set @wxtype='' ; set @wxcode=''  ; set @wxlen='0' 
select @mpname=mp_name,@mpcaption=mp_caption ,@mpid=wrk_view_end_msgid , @emp=mp_scope , @mb=mp_gzhtext , @wxtype=wx_type , @wxcode=wx_code 
from mps_flowwork , ebs_kernel.dbo.pub_messagetemplate, ebs_kernel.dbo.pub_weixin 
where wrk_id=@wrkid and wrk_view_end_msgid>0 and mp_id=wrk_view_end_msgid and len(mp_gzhtext)>10 and mp_gzhid=wx_id 
set @wxlen = convert(varchar(10),len(@wxcode)+2)  
if (@mpid>0) 
begin
set @icc = @icc + 1
set @msgcount = @msgcount+1
set @sql=@sql+'
insert into ebs_kernel.dbo.pub_message(msg_cmpid,msg_mpid,msg_text,msg_senddate,msg_type,msg_target,msg_cuid)  
select @cmpid,'+convert(varchar(10),@mpid)+','+@mb+',getdate(),'''+@wxtype+''',
case when cu_gzh1 like '''+@wxcode+'%''  then substring(cu_gzh1,'+@wxlen+',50) 
when cu_gzh2 like '''+@wxcode+'%''  then substring(cu_gzh2,'+@wxlen+',50) 
when cu_gzh3 like '''+@wxcode+'%''  then substring(cu_gzh3,'+@wxlen+',50) 
when cu_gzh4 like '''+@wxcode+'%''  then substring(cu_gzh4,'+@wxlen+',50) 
when cu_gzh5 like '''+@wxcode+'%''  then substring(cu_gzh5,'+@wxlen+',50) end , cu_id 
from ' 
if (CHARINDEX('ord_',@mb)>0) set @sql=@sql+@dbname+@tbname +','
if (CHARINDEX('jb_',@mb)>0) set @sql=@sql+'mps_job,' 
set @sql=@sql+'mps_login,mps_job_users 
where '
if (CHARINDEX('ord_',@mb)>0) set @sql=@sql+' ord_id=@ordid and '
if (CHARINDEX('jb_',@mb)>0) set @sql=@sql+' jb_id=@jbid and ' 
set @sql=@sql+' jp_type=''wrkview'' and jp_jbid=@jbid and jp_cuid=cu_id and 
cu_type=0 and (cu_gzh1 like '''+@wxcode+'%'' or cu_gzh2 like '''+@wxcode+'%'' or '+
'cu_gzh3 like '''+@wxcode+'%'' or cu_gzh4 like '''+@wxcode+'%'' or cu_gzh5 like '''+@wxcode+'%'') ' +char(13)+char(10)
end 

set @mpid=0 ; set @emp='' ; set @mb='' ; set @wxtype='' ; set @wxcode=''  ; set @wxlen='0' 
select @mpname=mp_name,@mpcaption=mp_caption ,@mpid=wrk_view_end_msgid , @emp=mp_scope , @mb=mp_qyhtext , @wxtype=wx_type , @wxcode=wx_code 
from mps_flowwork , ebs_kernel.dbo.pub_messagetemplate, ebs_kernel.dbo.pub_weixin 
where wrk_id=@wrkid and wrk_view_end_msgid>0 and mp_id=wrk_view_end_msgid and len(mp_qyhtext)>10 and mp_qyhid=wx_id 
set @wxlen = convert(varchar(10),len(@wxcode)+2)  
if (@mpid>0)  
begin
set @icc = @icc + 1
set @msgcount = @msgcount+1
set @sql=@sql+'
insert into ebs_kernel.dbo.pub_message(msg_cmpid,msg_mpid,msg_text,msg_senddate,msg_type,msg_target,msg_cuid)  
select @cmpid,'+convert(varchar(10),@mpid)+','+@mb+',getdate(),'''+@wxtype+''',substring(cu_qyh , '+@wxlen+',50)    , cu_id 
from ' 
if (CHARINDEX('ord_',@mb)>0) set @sql=@sql+@dbname+@tbname +','
if (CHARINDEX('jb_',@mb)>0) set @sql=@sql+'mps_job,' 
set @sql=@sql+'mps_login,mps_job_users 
where '
if (CHARINDEX('ord_',@mb)>0) set @sql=@sql+' ord_id=@ordid and '
if (CHARINDEX('jb_',@mb)>0) set @sql=@sql+' jb_id=@jbid and ' 
set @sql=@sql+' jp_type=''wrkview'' and jp_jbid=@jbid and jp_cuid=cu_id and 
cu_type=0 and cu_qyh>'''' ' +char(13)+char(10) 
end

set @mpid=0 ; set @emp='' ; set @mb='' ; set @wxtype='' ; set @wxcode=''  ; set @wxlen='0' 
select @mpname=mp_name,@mpcaption=mp_caption ,@mpid=wrk_view_end_msgid , @emp=mp_scope , @mb=mp_dingtext , @wxtype=wx_type , @wxcode=wx_code 
from mps_flowwork , ebs_kernel.dbo.pub_messagetemplate, ebs_kernel.dbo.pub_weixin 
where wrk_id=@wrkid and wrk_view_end_msgid>0 and mp_id=wrk_view_end_msgid and len(mp_dingtext)>10 and mp_dingid=wx_id 
set @wxlen = convert(varchar(10),len(@wxcode)+2)  
if (@mpid>0)  
begin
set @icc = @icc + 1
set @msgcount = @msgcount+1
set @sql=@sql+'
insert into ebs_kernel.dbo.pub_message(msg_cmpid,msg_mpid,msg_text,msg_senddate,msg_type,msg_target,msg_cuid)  
select @cmpid,'+convert(varchar(10),@mpid)+','+@mb+',getdate(),'''+@wxtype+''',substring(cu_ding , '+@wxlen+',50)    , cu_id 
from ' 
if (CHARINDEX('ord_',@mb)>0) set @sql=@sql+@dbname+@tbname +','
if (CHARINDEX('jb_',@mb)>0) set @sql=@sql+'mps_job,' 
set @sql=@sql+'mps_login,mps_job_users 
where '
if (CHARINDEX('ord_',@mb)>0) set @sql=@sql+' ord_id=@ordid and '
if (CHARINDEX('jb_',@mb)>0) set @sql=@sql+' jb_id=@jbid and ' 
set @sql=@sql+' jp_type=''wrkview'' and jp_jbid=@jbid and jp_cuid=cu_id and 
cu_type=0 and cu_ding>'''' ' +char(13)+char(10) 
end

set @mpid=0 ; set @emp='' ; set @mb='' ; set @wxtype='' ; set @wxcode=''  ; set @wxlen='0' 
select @mpname=mp_name,@mpcaption=mp_caption ,@mpid=wrk_view_end_msgid , @emp=mp_scope , @mb=mp_dayutext , @wxtype=wx_type , @wxcode=wx_code 
from mps_flowwork , ebs_kernel.dbo.pub_messagetemplate, ebs_kernel.dbo.pub_weixin 
where wrk_id=@wrkid and wrk_view_end_msgid>0 and mp_id=wrk_view_end_msgid and len(mp_dayutext)>10 and mp_dayuid=wx_id 
set @wxlen = convert(varchar(10),len(@wxcode)+2)  
if (@mpid>0)  
begin
set @icc = @icc + 1
set @sql=@sql+'
insert into ebs_kernel.dbo.pub_message(msg_cmpid,msg_mpid,msg_text,msg_senddate,msg_type,msg_target,msg_cuid)  
select @cmpid,'+convert(varchar(10),@mpid)+','+@mb+',getdate(),'''+@wxtype+''',cu_mobile  , cu_id 
from ' 
if (CHARINDEX('ord_',@mb)>0) set @sql=@sql+@dbname+@tbname +','
if (CHARINDEX('jb_',@mb)>0) set @sql=@sql+'mps_job,' 
set @sql=@sql+'mps_login,mps_job_users 
where '
if (CHARINDEX('ord_',@mb)>0) set @sql=@sql+' ord_id=@ordid and '
if (CHARINDEX('jb_',@mb)>0) set @sql=@sql+' jb_id=@jbid and ' 
set @sql=@sql+' jp_type=''wrkview'' and jp_jbid=@jbid and jp_cuid=cu_id and cu_type=0 and len(cu_mobile)>=11 '+char(13)+char(10) 
if (@msgcount>0) 
set @sql=@sql+'and (cu_ding+cu_qyh+cu_gzh1+cu_gzh2+cu_gzh3+cu_gzh4+cu_gzh5)='''' ' +char(13)+char(10)  
end

set @mpid=0 ; set @emp='' ; set @mb='' ; set @wxtype='' ; set @wxcode=''  ; set @wxlen='0' 
select @mpname=mp_name,@mpcaption=mp_caption ,@mpid=wrk_view_end_msgid , @emp=mp_scope , @mb=mp_pctext  , @s1=mp_caption 
from mps_flowwork , ebs_kernel.dbo.pub_messagetemplate 
where wrk_id=@wrkid and wrk_view_end_msgid>0 and mp_id=wrk_view_end_msgid and len(mp_pctext)>10  
if (@mpid>0) 
begin
set @icc = @icc + 1
set @sql=@sql+'
insert into mps_alarm_message(
 almmsg_cmpid,almmsg_almid,almmsg_identkey,almmsg_type,almmsg_title,almmsg_category,almmsg_cuid,almmsg_text,almmsg_objid,almmsg_jbid,almmsg_objtype) 
select @cmpid,'+convert(varchar(10),@mpid)+','','',''message'','''+@mpcaption+''','''+@mpname+''',cu_id,'+@mb+' , @ordid,@jbid ,''biz'' 
from ' 
if (CHARINDEX('ord_',@mb)>0) set @sql=@sql+@dbname+@tbname +','
if (CHARINDEX('jb_',@mb)>0) set @sql=@sql+'mps_job,' 
set @sql=@sql+'mps_login,mps_job_users 
where '
if (CHARINDEX('ord_',@mb)>0) set @sql=@sql+' ord_id=@ordid and '
if (CHARINDEX('jb_',@mb)>0) set @sql=@sql+' jb_id=@jbid and ' 
set @sql=@sql+' jp_type=''wrkview'' and jp_jbid=@jbid and jp_cuid=cu_id and cu_type=0  '+char(13)+char(10) 
end 

set @sql=@sql+char(13)+char(10)+'------ 给日志参与人发消息  ----------  '+char(13)+char(10)
--- 以下处理 工作开始 给日志参与人发消息 
set @msgcount = 0 
set @mpid=0 ; set @emp='' ; set @mb='' ; set @wxtype='' ; set @wxcode=''  ; set @wxlen='0' 
select @mpname=mp_name,@mpcaption=mp_caption ,@mpid=wrk_log_end_msgid , @emp=mp_scope , @mb=mp_gzhtext , @wxtype=wx_type , @wxcode=wx_code 
from mps_flowwork , ebs_kernel.dbo.pub_messagetemplate, ebs_kernel.dbo.pub_weixin 
where wrk_id=@wrkid and wrk_log_end_msgid>0 and mp_id=wrk_log_end_msgid and len(mp_gzhtext)>10 and mp_gzhid=wx_id 
set @wxlen = convert(varchar(10),len(@wxcode)+2)  
if (@mpid>0) 
begin
set @icc = @icc + 1
set @msgcount = @msgcount+1
set @sql=@sql+'
insert into ebs_kernel.dbo.pub_message(msg_cmpid,msg_mpid,msg_text,msg_senddate,msg_type,msg_target,msg_cuid)  
select @cmpid,'+convert(varchar(10),@mpid)+','+@mb+',getdate(),'''+@wxtype+''',
case when cu_gzh1 like '''+@wxcode+'%''  then substring(cu_gzh1,'+@wxlen+',50) 
when cu_gzh2 like '''+@wxcode+'%''  then substring(cu_gzh2,'+@wxlen+',50) 
when cu_gzh3 like '''+@wxcode+'%''  then substring(cu_gzh3,'+@wxlen+',50) 
when cu_gzh4 like '''+@wxcode+'%''  then substring(cu_gzh4,'+@wxlen+',50) 
when cu_gzh5 like '''+@wxcode+'%''  then substring(cu_gzh5,'+@wxlen+',50) end , cu_id 
from ' 
if (CHARINDEX('ord_',@mb)>0) set @sql=@sql+@dbname+@tbname +','
if (CHARINDEX('jb_',@mb)>0) set @sql=@sql+'mps_job,' 
set @sql=@sql+'mps_login,mps_job_users 
where '
if (CHARINDEX('ord_',@mb)>0) set @sql=@sql+' ord_id=@ordid and '
if (CHARINDEX('jb_',@mb)>0) set @sql=@sql+' jb_id=@jbid and ' 
set @sql=@sql+' jp_type=''wrklog'' and jp_jbid=@jbid and jp_cuid=cu_id and 
cu_type=0 and (cu_gzh1 like '''+@wxcode+'%'' or cu_gzh2 like '''+@wxcode+'%'' or '+
'cu_gzh3 like '''+@wxcode+'%'' or cu_gzh4 like '''+@wxcode+'%'' or cu_gzh5 like '''+@wxcode+'%'') ' +char(13)+char(10)
end 

set @mpid=0 ; set @emp='' ; set @mb='' ; set @wxtype='' ; set @wxcode=''  ; set @wxlen='0' 
select @mpname=mp_name,@mpcaption=mp_caption ,@mpid=wrk_log_end_msgid , @emp=mp_scope , @mb=mp_qyhtext , @wxtype=wx_type , @wxcode=wx_code 
from mps_flowwork , ebs_kernel.dbo.pub_messagetemplate, ebs_kernel.dbo.pub_weixin 
where wrk_id=@wrkid and wrk_log_end_msgid>0 and mp_id=wrk_log_end_msgid and len(mp_qyhtext)>10 and mp_qyhid=wx_id 
set @wxlen = convert(varchar(10),len(@wxcode)+2)  
if (@mpid>0)  
begin
set @icc = @icc + 1
set @msgcount = @msgcount+1
set @sql=@sql+'
insert into ebs_kernel.dbo.pub_message(msg_cmpid,msg_mpid,msg_text,msg_senddate,msg_type,msg_target,msg_cuid)  
select @cmpid,'+convert(varchar(10),@mpid)+','+@mb+',getdate(),'''+@wxtype+''',substring(cu_qyh , '+@wxlen+',50)    , cu_id 
from ' 
if (CHARINDEX('ord_',@mb)>0) set @sql=@sql+@dbname+@tbname +','
if (CHARINDEX('jb_',@mb)>0) set @sql=@sql+'mps_job,' 
set @sql=@sql+'mps_login,mps_job_users 
where '
if (CHARINDEX('ord_',@mb)>0) set @sql=@sql+' ord_id=@ordid and '
if (CHARINDEX('jb_',@mb)>0) set @sql=@sql+' jb_id=@jbid and ' 
set @sql=@sql+' jp_type=''wrklog'' and jp_jbid=@jbid and jp_cuid=cu_id and 
cu_type=0 and cu_qyh>'''' ' +char(13)+char(10) 
end

set @mpid=0 ; set @emp='' ; set @mb='' ; set @wxtype='' ; set @wxcode=''  ; set @wxlen='0' 
select @mpname=mp_name,@mpcaption=mp_caption ,@mpid=wrk_log_end_msgid , @emp=mp_scope , @mb=mp_dingtext , @wxtype=wx_type , @wxcode=wx_code 
from mps_flowwork , ebs_kernel.dbo.pub_messagetemplate, ebs_kernel.dbo.pub_weixin 
where wrk_id=@wrkid and wrk_log_end_msgid>0 and mp_id=wrk_log_end_msgid and len(mp_dingtext)>10 and mp_dingid=wx_id 
set @wxlen = convert(varchar(10),len(@wxcode)+2)  
if (@mpid>0)  
begin
set @icc = @icc + 1
set @msgcount = @msgcount+1
set @sql=@sql+'
insert into ebs_kernel.dbo.pub_message(msg_cmpid,msg_mpid,msg_text,msg_senddate,msg_type,msg_target,msg_cuid)  
select @cmpid,'+convert(varchar(10),@mpid)+','+@mb+',getdate(),'''+@wxtype+''',substring(cu_ding , '+@wxlen+',50)    , cu_id 
from ' 
if (CHARINDEX('ord_',@mb)>0) set @sql=@sql+@dbname+@tbname +','
if (CHARINDEX('jb_',@mb)>0) set @sql=@sql+'mps_job,' 
set @sql=@sql+'mps_login,mps_job_users 
where '
if (CHARINDEX('ord_',@mb)>0) set @sql=@sql+' ord_id=@ordid and '
if (CHARINDEX('jb_',@mb)>0) set @sql=@sql+' jb_id=@jbid and ' 
set @sql=@sql+' jp_type=''wrklog'' and jp_jbid=@jbid and jp_cuid=cu_id and 
cu_type=0 and cu_ding>'''' ' +char(13)+char(10) 
end

set @mpid=0 ; set @emp='' ; set @mb='' ; set @wxtype='' ; set @wxcode=''  ; set @wxlen='0' 
select @mpname=mp_name,@mpcaption=mp_caption ,@mpid=wrk_log_end_msgid , @emp=mp_scope , @mb=mp_dayutext , @wxtype=wx_type , @wxcode=wx_code 
from mps_flowwork , ebs_kernel.dbo.pub_messagetemplate, ebs_kernel.dbo.pub_weixin 
where wrk_id=@wrkid and wrk_log_end_msgid>0 and mp_id=wrk_log_end_msgid and len(mp_dayutext)>10 and mp_dayuid=wx_id 
set @wxlen = convert(varchar(10),len(@wxcode)+2)  
if (@mpid>0)  
begin
set @icc = @icc + 1
set @sql=@sql+'
insert into ebs_kernel.dbo.pub_message(msg_cmpid,msg_mpid,msg_text,msg_senddate,msg_type,msg_target,msg_cuid)  
select @cmpid,'+convert(varchar(10),@mpid)+','+@mb+',getdate(),'''+@wxtype+''',cu_mobile  , cu_id 
from ' 
if (CHARINDEX('ord_',@mb)>0) set @sql=@sql+@dbname+@tbname +','
if (CHARINDEX('jb_',@mb)>0) set @sql=@sql+'mps_job,' 
set @sql=@sql+'mps_login,mps_job_users 
where '
if (CHARINDEX('ord_',@mb)>0) set @sql=@sql+' ord_id=@ordid and '
if (CHARINDEX('jb_',@mb)>0) set @sql=@sql+' jb_id=@jbid and ' 
set @sql=@sql+' jp_type=''wrklog'' and jp_jbid=@jbid and jp_cuid=cu_id and cu_type=0 and len(cu_mobile)>=11 '+char(13)+char(10) 
if (@msgcount>0) 
set @sql=@sql+'and (cu_ding+cu_qyh+cu_gzh1+cu_gzh2+cu_gzh3+cu_gzh4+cu_gzh5)='''' ' +char(13)+char(10)  
end

set @mpid=0 ; set @emp='' ; set @mb='' ; set @wxtype='' ; set @wxcode=''  ; set @wxlen='0' 
select @mpname=mp_name,@mpcaption=mp_caption ,@mpid=wrk_log_end_msgid , @emp=mp_scope , @mb=mp_pctext  , @s1=mp_caption 
from mps_flowwork , ebs_kernel.dbo.pub_messagetemplate 
where wrk_id=@wrkid and wrk_log_end_msgid>0 and mp_id=wrk_log_end_msgid and len(mp_pctext)>10  
if (@mpid>0) 
begin
set @icc = @icc + 1
set @sql=@sql+'
insert into mps_alarm_message(
 almmsg_cmpid,almmsg_almid,almmsg_identkey,almmsg_type,almmsg_title,almmsg_category,almmsg_cuid,almmsg_text,almmsg_objid,almmsg_jbid,almmsg_objtype) 
select @cmpid,'+convert(varchar(10),@mpid)+','','',''message'','''+@mpcaption+''','''+@mpname+''',cu_id,'+@mb+' , @ordid,@jbid ,''biz'' 
from ' 
if (CHARINDEX('ord_',@mb)>0) set @sql=@sql+@dbname+@tbname +','
if (CHARINDEX('jb_',@mb)>0) set @sql=@sql+'mps_job,' 
set @sql=@sql+'mps_login,mps_job_users 
where '
if (CHARINDEX('ord_',@mb)>0) set @sql=@sql+' ord_id=@ordid and '
if (CHARINDEX('jb_',@mb)>0) set @sql=@sql+' jb_id=@jbid and ' 
set @sql=@sql+' jp_type=''wrklog'' and jp_jbid=@jbid and jp_cuid=cu_id and cu_type=0  '+char(13)+char(10) 
end 

set @sql=@sql+char(13)+char(10)+'------ 给员工1发消息  ----------  '+char(13)+char(10)
--- 以下处理 工作开始 给员工1发消息 
set @msgcount = 0 
set @mpid=0 ; set @emp='' ; set @mb='' ; set @wxtype='' ; set @wxcode=''  ; set @wxlen='0' ; set @msgfld='' ; set @datasource='' 
select @mpname=mp_name,@mpcaption=mp_caption ,@mpid=wrk_emp1_end_msgid , @msgfld=wrk_emp1_source,@datasource=wrk_emp1_datasource,@emp=mp_scope , @mb=mp_gzhtext , @wxtype=wx_type , @wxcode=wx_code 
from mps_flowwork , ebs_kernel.dbo.pub_messagetemplate, ebs_kernel.dbo.pub_weixin 
where wrk_id=@wrkid and wrk_emp1_end_msgid>0 and mp_id=wrk_emp1_end_msgid and len(wrk_emp1_source)>5 and len(mp_gzhtext)>10 and mp_gzhid=wx_id 
set @wxlen = convert(varchar(10),len(@wxcode)+2)  
set @datasource= isnull(@datasource,0) 
if (@datasource<=0) set @datasource = 0
if (@mpid>0) 
begin
set @icc = @icc + 1
set @msgcount = @msgcount+1
set @sql=@sql+'
insert into ebs_kernel.dbo.pub_message(msg_cmpid,msg_mpid,msg_text,msg_senddate,msg_type,msg_target,msg_cuid)  
select @cmpid,'+convert(varchar(10),@mpid)+','+@mb+',getdate(),'''+@wxtype+''',
case when cu_gzh1 like '''+@wxcode+'%''  then substring(cu_gzh1,'+@wxlen+',50) 
when cu_gzh2 like '''+@wxcode+'%''  then substring(cu_gzh2,'+@wxlen+',50) 
when cu_gzh3 like '''+@wxcode+'%''  then substring(cu_gzh3,'+@wxlen+',50) 
when cu_gzh4 like '''+@wxcode+'%''  then substring(cu_gzh4,'+@wxlen+',50) 
when cu_gzh5 like '''+@wxcode+'%''  then substring(cu_gzh5,'+@wxlen+',50) end , cu_id 
from ' 
if (CHARINDEX('ord_',@mb+@msgfld)>0 or @datasource=999) set @sql=@sql+@dbname+@tbname +','
if (CHARINDEX('ord2_',@mb+@msgfld)>0) set @sql=@sql+@dbname+@tbname +'_'+convert(varchar(10),@datasource)+','
if (CHARINDEX('jb_',@mb+@msgfld)>0) set @sql=@sql+'mps_job,' 
if (@datasource=999) set @sql=@sql+@dbname+@tbname+'_users,' 
set @sql=@sql+'mps_login 
where '
if (CHARINDEX('ord_',@mb+@msgfld)>0 or @datasource=999) set @sql=@sql+' ord_id=@ordid and '
if (CHARINDEX('ord2_',@mb+@msgfld)>0) set @sql=@sql+' ord2_ordid=@ordid and '
if (CHARINDEX('jb_',@mb+@msgfld)>0) set @sql=@sql+' jb_id=@jbid and ' 
if (@datasource=999) set @sql=@sql+' ordp_ordid=ord_id and ordp_type='''+@msgfld+''' and cu_id=ordp_cuid and ' 
else set @sql=@sql+@msgfld+'=cu_id and ' 
set @sql=@sql+' 
cu_type=0 and (cu_gzh1 like '''+@wxcode+'%'' or cu_gzh2 like '''+@wxcode+'%'' or '+
'cu_gzh3 like '''+@wxcode+'%'' or cu_gzh4 like '''+@wxcode+'%'' or cu_gzh5 like '''+@wxcode+'%'') ' +char(13)+char(10)
end 

set @mpid=0 ; set @emp='' ; set @mb='' ; set @wxtype='' ; set @wxcode=''  ; set @wxlen='0' ; set @msgfld='' ; set @datasource='' 
select @mpname=mp_name,@mpcaption=mp_caption ,@mpid=wrk_emp1_end_msgid , @msgfld=wrk_emp1_source,@datasource=wrk_emp1_datasource,@emp=mp_scope , @mb=mp_qyhtext , @wxtype=wx_type , @wxcode=wx_code 
from mps_flowwork , ebs_kernel.dbo.pub_messagetemplate, ebs_kernel.dbo.pub_weixin 
where wrk_id=@wrkid and wrk_emp1_end_msgid>0 and mp_id=wrk_emp1_end_msgid and len(wrk_emp1_source)>5 and len(mp_qyhtext)>10 and mp_qyhid=wx_id 
set @datasource= isnull(@datasource,0) 
if (@datasource<=0) set @datasource = 0
set @wxlen = convert(varchar(10),len(@wxcode)+2)  
if (@mpid>0)  
begin
set @icc = @icc + 1
set @msgcount = @msgcount+1
set @sql=@sql+'
insert into ebs_kernel.dbo.pub_message(msg_cmpid,msg_mpid,msg_text,msg_senddate,msg_type,msg_target,msg_cuid)  
select @cmpid,'+convert(varchar(10),@mpid)+','+@mb+',getdate(),'''+@wxtype+''',substring(cu_qyh , '+@wxlen+',50)    , cu_id 
from ' 
if (CHARINDEX('ord_',@mb+@msgfld)>0 or @datasource=999) set @sql=@sql+@dbname+@tbname +','
if (CHARINDEX('ord2_',@mb+@msgfld)>0) set @sql=@sql+@dbname+@tbname +'_'+convert(varchar(10),@datasource)+','
if (CHARINDEX('jb_',@mb+@msgfld)>0) set @sql=@sql+'mps_job,' 
if (@datasource=999) set @sql=@sql+@dbname+@tbname+'_users,' 
set @sql=@sql+'mps_login 
where '
if (CHARINDEX('ord_',@mb+@msgfld)>0 or @datasource=999) set @sql=@sql+' ord_id=@ordid and '
if (CHARINDEX('ord2_',@mb+@msgfld)>0) set @sql=@sql+' ord2_ordid=@ordid and '
if (CHARINDEX('jb_',@mb+@msgfld)>0) set @sql=@sql+' jb_id=@jbid and ' 
if (@datasource=999) set @sql=@sql+' ordp_ordid=ord_id and ordp_type='''+@msgfld+''' and cu_id=ordp_cuid and ' 
else set @sql=@sql+@msgfld+'=cu_id and ' 
set @sql=@sql+' 
cu_type=0 and cu_qyh>'''' ' +char(13)+char(10) 
end

set @mpid=0 ; set @emp='' ; set @mb='' ; set @wxtype='' ; set @wxcode=''  ; set @wxlen='0' ; set @msgfld='' ; set @datasource='' 
select @mpname=mp_name,@mpcaption=mp_caption ,@mpid=wrk_emp1_end_msgid ,@msgfld=wrk_emp1_source, @datasource=wrk_emp1_datasource,@emp=mp_scope , @mb=mp_dingtext , @wxtype=wx_type , @wxcode=wx_code 
from mps_flowwork , ebs_kernel.dbo.pub_messagetemplate, ebs_kernel.dbo.pub_weixin 
where wrk_id=@wrkid and wrk_emp1_end_msgid>0 and mp_id=wrk_emp1_end_msgid and len(wrk_emp1_source)>5 and len(mp_dingtext)>10 and mp_dingid=wx_id 
set @datasource= isnull(@datasource,0) 
if (@datasource<=0) set @datasource = 0
set @wxlen = convert(varchar(10),len(@wxcode)+2)  
if (@mpid>0)  
begin
set @icc = @icc + 1
set @msgcount = @msgcount+1
set @sql=@sql+'
insert into ebs_kernel.dbo.pub_message(msg_cmpid,msg_mpid,msg_text,msg_senddate,msg_type,msg_target,msg_cuid)  
select @cmpid,'+convert(varchar(10),@mpid)+','+@mb+',getdate(),'''+@wxtype+''',substring(cu_ding , '+@wxlen+',50)    , cu_id 
from ' 
if (CHARINDEX('ord_',@mb+@msgfld)>0 or @datasource=999) set @sql=@sql+@dbname+@tbname +','
if (CHARINDEX('ord2_',@mb+@msgfld)>0) set @sql=@sql+@dbname+@tbname +'_'+convert(varchar(10),@datasource)+','
if (CHARINDEX('jb_',@mb+@msgfld)>0) set @sql=@sql+'mps_job,' 
if (@datasource=999) set @sql=@sql+@dbname+@tbname+'_users,' 
set @sql=@sql+'mps_login 
where '
if (CHARINDEX('ord_',@mb+@msgfld)>0 or @datasource=999) set @sql=@sql+' ord_id=@ordid and '
if (CHARINDEX('ord2_',@mb+@msgfld)>0) set @sql=@sql+' ord2_ordid=@ordid and '
if (CHARINDEX('jb_',@mb+@msgfld)>0) set @sql=@sql+' jb_id=@jbid and ' 
if (@datasource=999) set @sql=@sql+' ordp_ordid=ord_id and ordp_type='''+@msgfld+''' and cu_id=ordp_cuid and ' 
else set @sql=@sql+@msgfld+'=cu_id and ' 
set @sql=@sql+' 
cu_type=0 and cu_ding>'''' ' +char(13)+char(10) 
end

set @mpid=0 ; set @emp='' ; set @mb='' ; set @wxtype='' ; set @wxcode=''  ; set @wxlen='0' ; set @msgfld='' ; set @datasource='' 
select @mpname=mp_name,@mpcaption=mp_caption ,@mpid=wrk_emp1_end_msgid , @msgfld=wrk_emp1_source,@datasource=wrk_emp1_datasource,@emp=mp_scope , @mb=mp_dayutext , @wxtype=wx_type , @wxcode=wx_code 
from mps_flowwork , ebs_kernel.dbo.pub_messagetemplate, ebs_kernel.dbo.pub_weixin 
where wrk_id=@wrkid and wrk_emp1_end_msgid>0 and mp_id=wrk_emp1_end_msgid and len(wrk_emp1_source)>5 and len(mp_dayutext)>10 and mp_dayuid=wx_id 
set @datasource= isnull(@datasource,0) 
if (@datasource<=0) set @datasource = 0
set @wxlen = convert(varchar(10),len(@wxcode)+2)  
if (@mpid>0)  
begin
set @icc = @icc + 1
set @sql=@sql+'
insert into ebs_kernel.dbo.pub_message(msg_cmpid,msg_mpid,msg_text,msg_senddate,msg_type,msg_target,msg_cuid)  
select @cmpid,'+convert(varchar(10),@mpid)+','+@mb+',getdate(),'''+@wxtype+''',cu_mobile  , cu_id 
from ' 
if (CHARINDEX('ord_',@mb+@msgfld)>0 or @datasource=999) set @sql=@sql+@dbname+@tbname +','
if (CHARINDEX('ord2_',@mb+@msgfld)>0) set @sql=@sql+@dbname+@tbname +'_'+convert(varchar(10),@datasource)+','
if (CHARINDEX('jb_',@mb+@msgfld)>0) set @sql=@sql+'mps_job,' 
if (@datasource=999) set @sql=@sql+@dbname+@tbname+'_users,' 
set @sql=@sql+'mps_login 
where '
if (CHARINDEX('ord_',@mb+@msgfld)>0 or @datasource=999) set @sql=@sql+' ord_id=@ordid and '
if (CHARINDEX('ord2_',@mb+@msgfld)>0) set @sql=@sql+' ord2_ordid=@ordid and '
if (CHARINDEX('jb_',@mb+@msgfld)>0) set @sql=@sql+' jb_id=@jbid and ' 
if (@datasource=999) set @sql=@sql+' ordp_ordid=ord_id and ordp_type='''+@msgfld+''' and cu_id=ordp_cuid and ' 
else set @sql=@sql+@msgfld+'=cu_id and ' 
set @sql=@sql+' cu_type=0 and len(cu_mobile)>=11 '+char(13)+char(10) 
if (@msgcount>0) 
set @sql=@sql+'and (cu_ding+cu_qyh+cu_gzh1+cu_gzh2+cu_gzh3+cu_gzh4+cu_gzh5)='''' ' +char(13)+char(10)  
end


set @mpid=0 ; set @emp='' ; set @mb='' ; set @wxtype='' ; set @wxcode=''  ; set @wxlen='0' ; set @msgfld='' ; set @datasource='' 
select @mpname=mp_name,@mpcaption=mp_caption ,@mpid=wrk_emp1_end_msgid , @msgfld=wrk_emp1_source,@datasource=wrk_emp1_datasource,@emp=mp_scope , @mb=mp_pctext  , @s1=mp_caption  
from mps_flowwork , ebs_kernel.dbo.pub_messagetemplate 
where wrk_id=@wrkid and wrk_emp1_end_msgid>0 and mp_id=wrk_emp1_end_msgid and len(wrk_emp1_source)>5 and len(mp_pctext)>10  
set @datasource= isnull(@datasource,0) 
if (@datasource<=0) set @datasource = 0
set @wxlen = convert(varchar(10),len(@wxcode)+2)  
if (@mpid>0)  
begin
set @icc = @icc + 1
set @sql=@sql+'
insert into mps_alarm_message(
 almmsg_cmpid,almmsg_almid,almmsg_identkey,almmsg_type,almmsg_title,almmsg_category,almmsg_cuid,almmsg_text,almmsg_objid,almmsg_jbid,almmsg_objtype) 
select @cmpid,'+convert(varchar(10),@mpid)+','','',''message'','''+@mpcaption+''','''+@mpname+''',cu_id,'+@mb+' , @ordid,@jbid ,''biz'' 
from ' 
if (CHARINDEX('ord_',@mb+@msgfld)>0 or @datasource=999) set @sql=@sql+@dbname+@tbname +','
if (CHARINDEX('ord2_',@mb+@msgfld)>0) set @sql=@sql+@dbname+@tbname +'_'+convert(varchar(10),@datasource)+','
if (CHARINDEX('jb_',@mb+@msgfld)>0) set @sql=@sql+'mps_job,' 
if (@datasource=999) set @sql=@sql+@dbname+@tbname+'_users,' 
set @sql=@sql+'mps_login 
where '
if (CHARINDEX('ord_',@mb+@msgfld)>0 or @datasource=999) set @sql=@sql+' ord_id=@ordid and '
if (CHARINDEX('ord2_',@mb+@msgfld)>0) set @sql=@sql+' ord2_ordid=@ordid and '
if (CHARINDEX('jb_',@mb+@msgfld)>0) set @sql=@sql+' jb_id=@jbid and ' 
if (@datasource=999) set @sql=@sql+' ordp_ordid=ord_id and ordp_type='''+@msgfld+''' and cu_id=ordp_cuid and ' 
else set @sql=@sql+@msgfld+'=cu_id and ' 
set @sql=@sql+' cu_type=0  '+char(13)+char(10) 
end


set @sql=@sql+char(13)+char(10)+'------ 给员工2发消息  ----------  '+char(13)+char(10)
--- 以下处理 工作开始 给员工2发消息 
set @msgcount = 0 
set @mpid=0 ; set @emp='' ; set @mb='' ; set @wxtype='' ; set @wxcode=''  ; set @wxlen='0' ; set @msgfld='' ; set @datasource='' 
select @mpname=mp_name,@mpcaption=mp_caption ,@mpid=wrk_emp2_end_msgid , @msgfld=wrk_emp2_source,@datasource=wrk_emp2_datasource,@emp=mp_scope , @mb=mp_gzhtext , @wxtype=wx_type , @wxcode=wx_code 
from mps_flowwork , ebs_kernel.dbo.pub_messagetemplate, ebs_kernel.dbo.pub_weixin 
where wrk_id=@wrkid and wrk_emp2_end_msgid>0 and mp_id=wrk_emp2_end_msgid and len(wrk_emp2_source)>5 and len(mp_gzhtext)>10 and mp_gzhid=wx_id 
set @wxlen = convert(varchar(10),len(@wxcode)+2)  
set @datasource= isnull(@datasource,0) 
if (@datasource<=0) set @datasource = 0
if (@mpid>0) 
begin
set @icc = @icc + 1
set @msgcount = @msgcount+1
set @sql=@sql+'
insert into ebs_kernel.dbo.pub_message(msg_cmpid,msg_mpid,msg_text,msg_senddate,msg_type,msg_target,msg_cuid)  
select @cmpid,'+convert(varchar(10),@mpid)+','+@mb+',getdate(),'''+@wxtype+''',
case when cu_gzh1 like '''+@wxcode+'%''  then substring(cu_gzh1,'+@wxlen+',50) 
when cu_gzh2 like '''+@wxcode+'%''  then substring(cu_gzh2,'+@wxlen+',50) 
when cu_gzh3 like '''+@wxcode+'%''  then substring(cu_gzh3,'+@wxlen+',50) 
when cu_gzh4 like '''+@wxcode+'%''  then substring(cu_gzh4,'+@wxlen+',50) 
when cu_gzh5 like '''+@wxcode+'%''  then substring(cu_gzh5,'+@wxlen+',50) end , cu_id 
from ' 
if (CHARINDEX('ord_',@mb+@msgfld)>0 or @datasource=999) set @sql=@sql+@dbname+@tbname +','
if (CHARINDEX('ord2_',@mb+@msgfld)>0) set @sql=@sql+@dbname+@tbname +'_'+convert(varchar(10),@datasource)+','
if (CHARINDEX('jb_',@mb+@msgfld)>0) set @sql=@sql+'mps_job,' 
if (@datasource=999) set @sql=@sql+@dbname+@tbname+'_users,' 
set @sql=@sql+'mps_login 
where '
if (CHARINDEX('ord_',@mb+@msgfld)>0 or @datasource=999) set @sql=@sql+' ord_id=@ordid and '
if (CHARINDEX('ord2_',@mb+@msgfld)>0) set @sql=@sql+' ord2_ordid=@ordid and '
if (CHARINDEX('jb_',@mb+@msgfld)>0) set @sql=@sql+' jb_id=@jbid and ' 
if (@datasource=999) set @sql=@sql+' ordp_ordid=ord_id and ordp_type='''+@msgfld+''' and cu_id=ordp_cuid and ' 
else set @sql=@sql+@msgfld+'=cu_id and ' 
set @sql=@sql+' 
cu_type=0 and (cu_gzh1 like '''+@wxcode+'%'' or cu_gzh2 like '''+@wxcode+'%'' or '+
'cu_gzh3 like '''+@wxcode+'%'' or cu_gzh4 like '''+@wxcode+'%'' or cu_gzh5 like '''+@wxcode+'%'') ' +char(13)+char(10)
end 

set @mpid=0 ; set @emp='' ; set @mb='' ; set @wxtype='' ; set @wxcode=''  ; set @wxlen='0' ; set @msgfld='' ; set @datasource='' 
select @mpname=mp_name,@mpcaption=mp_caption ,@mpid=wrk_emp2_end_msgid , @msgfld=wrk_emp2_source,@datasource=wrk_emp2_datasource,@emp=mp_scope , @mb=mp_qyhtext , @wxtype=wx_type , @wxcode=wx_code 
from mps_flowwork , ebs_kernel.dbo.pub_messagetemplate, ebs_kernel.dbo.pub_weixin 
where wrk_id=@wrkid and wrk_emp2_end_msgid>0 and mp_id=wrk_emp2_end_msgid and len(wrk_emp2_source)>5 and len(mp_qyhtext)>10 and mp_qyhid=wx_id 
set @datasource= isnull(@datasource,0) 
if (@datasource<=0) set @datasource = 0
set @wxlen = convert(varchar(10),len(@wxcode)+2)  
if (@mpid>0)  
begin
set @icc = @icc + 1
set @msgcount = @msgcount+1
set @sql=@sql+'
insert into ebs_kernel.dbo.pub_message(msg_cmpid,msg_mpid,msg_text,msg_senddate,msg_type,msg_target,msg_cuid)  
select @cmpid,'+convert(varchar(10),@mpid)+','+@mb+',getdate(),'''+@wxtype+''',substring(cu_qyh , '+@wxlen+',50)    , cu_id 
from ' 
if (CHARINDEX('ord_',@mb+@msgfld)>0 or @datasource=999) set @sql=@sql+@dbname+@tbname +','
if (CHARINDEX('ord2_',@mb+@msgfld)>0) set @sql=@sql+@dbname+@tbname +'_'+convert(varchar(10),@datasource)+','
if (CHARINDEX('jb_',@mb+@msgfld)>0) set @sql=@sql+'mps_job,' 
if (@datasource=999) set @sql=@sql+@dbname+@tbname+'_users,' 
set @sql=@sql+'mps_login 
where '
if (CHARINDEX('ord_',@mb+@msgfld)>0 or @datasource=999) set @sql=@sql+' ord_id=@ordid and '
if (CHARINDEX('ord2_',@mb+@msgfld)>0) set @sql=@sql+' ord2_ordid=@ordid and '
if (CHARINDEX('jb_',@mb+@msgfld)>0) set @sql=@sql+' jb_id=@jbid and ' 
if (@datasource=999) set @sql=@sql+' ordp_ordid=ord_id and ordp_type='''+@msgfld+''' and cu_id=ordp_cuid and ' 
else set @sql=@sql+@msgfld+'=cu_id and ' 
set @sql=@sql+' 
cu_type=0 and cu_qyh>'''' ' +char(13)+char(10) 
end

set @mpid=0 ; set @emp='' ; set @mb='' ; set @wxtype='' ; set @wxcode=''  ; set @wxlen='0' ; set @msgfld='' ; set @datasource='' 
select @mpname=mp_name,@mpcaption=mp_caption ,@mpid=wrk_emp2_end_msgid ,@msgfld=wrk_emp2_source, @datasource=wrk_emp2_datasource,@emp=mp_scope , @mb=mp_dingtext , @wxtype=wx_type , @wxcode=wx_code 
from mps_flowwork , ebs_kernel.dbo.pub_messagetemplate, ebs_kernel.dbo.pub_weixin 
where wrk_id=@wrkid and wrk_emp2_end_msgid>0 and mp_id=wrk_emp2_end_msgid and len(wrk_emp2_source)>5 and len(mp_dingtext)>10 and mp_dingid=wx_id 
set @datasource= isnull(@datasource,0) 
if (@datasource<=0) set @datasource = 0
set @wxlen = convert(varchar(10),len(@wxcode)+2)  
if (@mpid>0)  
begin
set @icc = @icc + 1
set @msgcount = @msgcount+1
set @sql=@sql+'
insert into ebs_kernel.dbo.pub_message(msg_cmpid,msg_mpid,msg_text,msg_senddate,msg_type,msg_target,msg_cuid)  
select @cmpid,'+convert(varchar(10),@mpid)+','+@mb+',getdate(),'''+@wxtype+''',substring(cu_ding , '+@wxlen+',50)    , cu_id 
from ' 
if (CHARINDEX('ord_',@mb+@msgfld)>0 or @datasource=999) set @sql=@sql+@dbname+@tbname +','
if (CHARINDEX('ord2_',@mb+@msgfld)>0) set @sql=@sql+@dbname+@tbname +'_'+convert(varchar(10),@datasource)+','
if (CHARINDEX('jb_',@mb+@msgfld)>0) set @sql=@sql+'mps_job,' 
if (@datasource=999) set @sql=@sql+@dbname+@tbname+'_users,' 
set @sql=@sql+'mps_login 
where '
if (CHARINDEX('ord_',@mb+@msgfld)>0 or @datasource=999) set @sql=@sql+' ord_id=@ordid and '
if (CHARINDEX('ord2_',@mb+@msgfld)>0) set @sql=@sql+' ord2_ordid=@ordid and '
if (CHARINDEX('jb_',@mb+@msgfld)>0) set @sql=@sql+' jb_id=@jbid and ' 
if (@datasource=999) set @sql=@sql+' ordp_ordid=ord_id and ordp_type='''+@msgfld+''' and cu_id=ordp_cuid and ' 
else set @sql=@sql+@msgfld+'=cu_id and ' 
set @sql=@sql+' 
cu_type=0 and cu_ding>'''' ' +char(13)+char(10) 
end

set @mpid=0 ; set @emp='' ; set @mb='' ; set @wxtype='' ; set @wxcode=''  ; set @wxlen='0' ; set @msgfld='' ; set @datasource='' 
select @mpname=mp_name,@mpcaption=mp_caption ,@mpid=wrk_emp2_end_msgid , @msgfld=wrk_emp2_source,@datasource=wrk_emp2_datasource,@emp=mp_scope , @mb=mp_dayutext , @wxtype=wx_type , @wxcode=wx_code 
from mps_flowwork , ebs_kernel.dbo.pub_messagetemplate, ebs_kernel.dbo.pub_weixin 
where wrk_id=@wrkid and wrk_emp2_end_msgid>0 and mp_id=wrk_emp2_end_msgid and len(wrk_emp2_source)>5 and len(mp_dayutext)>10 and mp_dayuid=wx_id 
set @datasource= isnull(@datasource,0) 
if (@datasource<=0) set @datasource = 0
set @wxlen = convert(varchar(10),len(@wxcode)+2)  
if (@mpid>0)  
begin
set @icc = @icc + 1
set @sql=@sql+'
insert into ebs_kernel.dbo.pub_message(msg_cmpid,msg_mpid,msg_text,msg_senddate,msg_type,msg_target,msg_cuid)  
select @cmpid,'+convert(varchar(10),@mpid)+','+@mb+',getdate(),'''+@wxtype+''',cu_mobile  , cu_id 
from ' 
if (CHARINDEX('ord_',@mb+@msgfld)>0 or @datasource=999) set @sql=@sql+@dbname+@tbname +','
if (CHARINDEX('ord2_',@mb+@msgfld)>0) set @sql=@sql+@dbname+@tbname +'_'+convert(varchar(10),@datasource)+','
if (CHARINDEX('jb_',@mb+@msgfld)>0) set @sql=@sql+'mps_job,' 
if (@datasource=999) set @sql=@sql+@dbname+@tbname+'_users,' 
set @sql=@sql+'mps_login 
where '
if (CHARINDEX('ord_',@mb+@msgfld)>0 or @datasource=999) set @sql=@sql+' ord_id=@ordid and '
if (CHARINDEX('ord2_',@mb+@msgfld)>0) set @sql=@sql+' ord2_ordid=@ordid and '
if (CHARINDEX('jb_',@mb+@msgfld)>0) set @sql=@sql+' jb_id=@jbid and ' 
if (@datasource=999) set @sql=@sql+' ordp_ordid=ord_id and ordp_type='''+@msgfld+''' and cu_id=ordp_cuid and ' 
else set @sql=@sql+@msgfld+'=cu_id and ' 
set @sql=@sql+' cu_type=0 and len(cu_mobile)>=11 '+char(13)+char(10) 
if (@msgcount>0) 
set @sql=@sql+'and (cu_ding+cu_qyh+cu_gzh1+cu_gzh2+cu_gzh3+cu_gzh4+cu_gzh5)='''' ' +char(13)+char(10)  
end


set @mpid=0 ; set @emp='' ; set @mb='' ; set @wxtype='' ; set @wxcode=''  ; set @wxlen='0' ; set @msgfld='' ; set @datasource='' 
select @mpname=mp_name,@mpcaption=mp_caption ,@mpid=wrk_emp2_end_msgid , @msgfld=wrk_emp2_source,@datasource=wrk_emp2_datasource,@emp=mp_scope , @mb=mp_pctext  , @s1=mp_caption  
from mps_flowwork , ebs_kernel.dbo.pub_messagetemplate 
where wrk_id=@wrkid and wrk_emp2_end_msgid>0 and mp_id=wrk_emp2_end_msgid and len(wrk_emp2_source)>5 and len(mp_pctext)>10  
set @datasource= isnull(@datasource,0) 
if (@datasource<=0) set @datasource = 0
set @wxlen = convert(varchar(10),len(@wxcode)+2)  
if (@mpid>0)  
begin
set @icc = @icc + 1
set @sql=@sql+'
insert into mps_alarm_message(
 almmsg_cmpid,almmsg_almid,almmsg_identkey,almmsg_type,almmsg_title,almmsg_category,almmsg_cuid,almmsg_text,almmsg_objid,almmsg_jbid,almmsg_objtype) 
select @cmpid,'+convert(varchar(10),@mpid)+','','',''message'','''+@mpcaption+''','''+@mpname+''',cu_id,'+@mb+' , @ordid,@jbid ,''biz'' 
from ' 
if (CHARINDEX('ord_',@mb+@msgfld)>0 or @datasource=999) set @sql=@sql+@dbname+@tbname +','
if (CHARINDEX('ord2_',@mb+@msgfld)>0) set @sql=@sql+@dbname+@tbname +'_'+convert(varchar(10),@datasource)+','
if (CHARINDEX('jb_',@mb+@msgfld)>0) set @sql=@sql+'mps_job,' 
if (@datasource=999) set @sql=@sql+@dbname+@tbname+'_users,' 
set @sql=@sql+'mps_login 
where '
if (CHARINDEX('ord_',@mb+@msgfld)>0 or @datasource=999) set @sql=@sql+' ord_id=@ordid and '
if (CHARINDEX('ord2_',@mb+@msgfld)>0) set @sql=@sql+' ord2_ordid=@ordid and '
if (CHARINDEX('jb_',@mb+@msgfld)>0) set @sql=@sql+' jb_id=@jbid and ' 
if (@datasource=999) set @sql=@sql+' ordp_ordid=ord_id and ordp_type='''+@msgfld+''' and cu_id=ordp_cuid and ' 
else set @sql=@sql+@msgfld+'=cu_id and ' 
set @sql=@sql+' cu_type=0  '+char(13)+char(10) 
end


set @sql=@sql+char(13)+char(10)+'------ 给员工3发消息  ----------  '+char(13)+char(10)
--- 以下处理 工作开始 给员工3发消息 
set @msgcount = 0 
set @mpid=0 ; set @emp='' ; set @mb='' ; set @wxtype='' ; set @wxcode=''  ; set @wxlen='0' ; set @msgfld='' ; set @datasource='' 
select @mpname=mp_name,@mpcaption=mp_caption ,@mpid=wrk_emp3_end_msgid , @msgfld=wrk_emp3_source,@datasource=wrk_emp3_datasource,@emp=mp_scope , @mb=mp_gzhtext , @wxtype=wx_type , @wxcode=wx_code 
from mps_flowwork , ebs_kernel.dbo.pub_messagetemplate, ebs_kernel.dbo.pub_weixin 
where wrk_id=@wrkid and wrk_emp3_end_msgid>0 and mp_id=wrk_emp3_end_msgid and len(wrk_emp3_source)>5 and len(mp_gzhtext)>10 and mp_gzhid=wx_id 
set @wxlen = convert(varchar(10),len(@wxcode)+2)  
set @datasource= isnull(@datasource,0) 
if (@datasource<=0) set @datasource = 0
if (@mpid>0) 
begin
set @icc = @icc + 1
set @msgcount = @msgcount+1
set @sql=@sql+'
insert into ebs_kernel.dbo.pub_message(msg_cmpid,msg_mpid,msg_text,msg_senddate,msg_type,msg_target,msg_cuid)  
select @cmpid,'+convert(varchar(10),@mpid)+','+@mb+',getdate(),'''+@wxtype+''',
case when cu_gzh1 like '''+@wxcode+'%''  then substring(cu_gzh1,'+@wxlen+',50) 
when cu_gzh2 like '''+@wxcode+'%''  then substring(cu_gzh2,'+@wxlen+',50) 
when cu_gzh3 like '''+@wxcode+'%''  then substring(cu_gzh3,'+@wxlen+',50) 
when cu_gzh4 like '''+@wxcode+'%''  then substring(cu_gzh4,'+@wxlen+',50) 
when cu_gzh5 like '''+@wxcode+'%''  then substring(cu_gzh5,'+@wxlen+',50) end , cu_id 
from ' 
if (CHARINDEX('ord_',@mb+@msgfld)>0 or @datasource=999) set @sql=@sql+@dbname+@tbname +','
if (CHARINDEX('ord2_',@mb+@msgfld)>0) set @sql=@sql+@dbname+@tbname +'_'+convert(varchar(10),@datasource)+','
if (CHARINDEX('jb_',@mb+@msgfld)>0) set @sql=@sql+'mps_job,' 
if (@datasource=999) set @sql=@sql+@dbname+@tbname+'_users,' 
set @sql=@sql+'mps_login 
where '
if (CHARINDEX('ord_',@mb+@msgfld)>0 or @datasource=999) set @sql=@sql+' ord_id=@ordid and '
if (CHARINDEX('ord2_',@mb+@msgfld)>0) set @sql=@sql+' ord2_ordid=@ordid and '
if (CHARINDEX('jb_',@mb+@msgfld)>0) set @sql=@sql+' jb_id=@jbid and ' 
if (@datasource=999) set @sql=@sql+' ordp_ordid=ord_id and ordp_type='''+@msgfld+''' and cu_id=ordp_cuid and ' 
else set @sql=@sql+@msgfld+'=cu_id and ' 
set @sql=@sql+' 
cu_type=0 and (cu_gzh1 like '''+@wxcode+'%'' or cu_gzh2 like '''+@wxcode+'%'' or '+
'cu_gzh3 like '''+@wxcode+'%'' or cu_gzh4 like '''+@wxcode+'%'' or cu_gzh5 like '''+@wxcode+'%'') ' +char(13)+char(10)
end 

set @mpid=0 ; set @emp='' ; set @mb='' ; set @wxtype='' ; set @wxcode=''  ; set @wxlen='0' ; set @msgfld='' ; set @datasource='' 
select @mpname=mp_name,@mpcaption=mp_caption ,@mpid=wrk_emp3_end_msgid , @msgfld=wrk_emp3_source,@datasource=wrk_emp3_datasource,@emp=mp_scope , @mb=mp_qyhtext , @wxtype=wx_type , @wxcode=wx_code 
from mps_flowwork , ebs_kernel.dbo.pub_messagetemplate, ebs_kernel.dbo.pub_weixin 
where wrk_id=@wrkid and wrk_emp3_end_msgid>0 and mp_id=wrk_emp3_end_msgid and len(wrk_emp3_source)>5 and len(mp_qyhtext)>10 and mp_qyhid=wx_id 
set @datasource= isnull(@datasource,0) 
if (@datasource<=0) set @datasource = 0
set @wxlen = convert(varchar(10),len(@wxcode)+2)  
if (@mpid>0)  
begin
set @icc = @icc + 1
set @msgcount = @msgcount+1
set @sql=@sql+'
insert into ebs_kernel.dbo.pub_message(msg_cmpid,msg_mpid,msg_text,msg_senddate,msg_type,msg_target,msg_cuid)  
select @cmpid,'+convert(varchar(10),@mpid)+','+@mb+',getdate(),'''+@wxtype+''',substring(cu_qyh , '+@wxlen+',50)    , cu_id 
from ' 
if (CHARINDEX('ord_',@mb+@msgfld)>0 or @datasource=999) set @sql=@sql+@dbname+@tbname +','
if (CHARINDEX('ord2_',@mb+@msgfld)>0) set @sql=@sql+@dbname+@tbname +'_'+convert(varchar(10),@datasource)+','
if (CHARINDEX('jb_',@mb+@msgfld)>0) set @sql=@sql+'mps_job,' 
if (@datasource=999) set @sql=@sql+@dbname+@tbname+'_users,' 
set @sql=@sql+'mps_login 
where '
if (CHARINDEX('ord_',@mb+@msgfld)>0 or @datasource=999) set @sql=@sql+' ord_id=@ordid and '
if (CHARINDEX('ord2_',@mb+@msgfld)>0) set @sql=@sql+' ord2_ordid=@ordid and '
if (CHARINDEX('jb_',@mb+@msgfld)>0) set @sql=@sql+' jb_id=@jbid and ' 
if (@datasource=999) set @sql=@sql+' ordp_ordid=ord_id and ordp_type='''+@msgfld+''' and cu_id=ordp_cuid and ' 
else set @sql=@sql+@msgfld+'=cu_id and ' 
set @sql=@sql+' 
cu_type=0 and cu_qyh>'''' ' +char(13)+char(10) 
end

set @mpid=0 ; set @emp='' ; set @mb='' ; set @wxtype='' ; set @wxcode=''  ; set @wxlen='0' ; set @msgfld='' ; set @datasource='' 
select @mpname=mp_name,@mpcaption=mp_caption ,@mpid=wrk_emp3_end_msgid ,@msgfld=wrk_emp3_source, @datasource=wrk_emp3_datasource,@emp=mp_scope , @mb=mp_dingtext , @wxtype=wx_type , @wxcode=wx_code 
from mps_flowwork , ebs_kernel.dbo.pub_messagetemplate, ebs_kernel.dbo.pub_weixin 
where wrk_id=@wrkid and wrk_emp3_end_msgid>0 and mp_id=wrk_emp3_end_msgid and len(wrk_emp3_source)>5 and len(mp_dingtext)>10 and mp_dingid=wx_id 
set @datasource= isnull(@datasource,0) 
if (@datasource<=0) set @datasource = 0
set @wxlen = convert(varchar(10),len(@wxcode)+2)  
if (@mpid>0)  
begin
set @icc = @icc + 1
set @msgcount = @msgcount+1
set @sql=@sql+'
insert into ebs_kernel.dbo.pub_message(msg_cmpid,msg_mpid,msg_text,msg_senddate,msg_type,msg_target,msg_cuid)  
select @cmpid,'+convert(varchar(10),@mpid)+','+@mb+',getdate(),'''+@wxtype+''',substring(cu_ding , '+@wxlen+',50)    , cu_id 
from ' 
if (CHARINDEX('ord_',@mb+@msgfld)>0 or @datasource=999) set @sql=@sql+@dbname+@tbname +','
if (CHARINDEX('ord2_',@mb+@msgfld)>0) set @sql=@sql+@dbname+@tbname +'_'+convert(varchar(10),@datasource)+','
if (CHARINDEX('jb_',@mb+@msgfld)>0) set @sql=@sql+'mps_job,' 
if (@datasource=999) set @sql=@sql+@dbname+@tbname+'_users,' 
set @sql=@sql+'mps_login 
where '
if (CHARINDEX('ord_',@mb+@msgfld)>0 or @datasource=999) set @sql=@sql+' ord_id=@ordid and '
if (CHARINDEX('ord2_',@mb+@msgfld)>0) set @sql=@sql+' ord2_ordid=@ordid and '
if (CHARINDEX('jb_',@mb+@msgfld)>0) set @sql=@sql+' jb_id=@jbid and ' 
if (@datasource=999) set @sql=@sql+' ordp_ordid=ord_id and ordp_type='''+@msgfld+''' and cu_id=ordp_cuid and ' 
else set @sql=@sql+@msgfld+'=cu_id and ' 
set @sql=@sql+' 
cu_type=0 and cu_ding>'''' ' +char(13)+char(10) 
end

set @mpid=0 ; set @emp='' ; set @mb='' ; set @wxtype='' ; set @wxcode=''  ; set @wxlen='0' ; set @msgfld='' ; set @datasource='' 
select @mpname=mp_name,@mpcaption=mp_caption ,@mpid=wrk_emp3_end_msgid , @msgfld=wrk_emp3_source,@datasource=wrk_emp3_datasource,@emp=mp_scope , @mb=mp_dayutext , @wxtype=wx_type , @wxcode=wx_code 
from mps_flowwork , ebs_kernel.dbo.pub_messagetemplate, ebs_kernel.dbo.pub_weixin 
where wrk_id=@wrkid and wrk_emp3_end_msgid>0 and mp_id=wrk_emp3_end_msgid and len(wrk_emp3_source)>5 and len(mp_dayutext)>10 and mp_dayuid=wx_id 
set @datasource= isnull(@datasource,0) 
if (@datasource<=0) set @datasource = 0
set @wxlen = convert(varchar(10),len(@wxcode)+2)  
if (@mpid>0)  
begin
set @icc = @icc + 1
set @sql=@sql+'
insert into ebs_kernel.dbo.pub_message(msg_cmpid,msg_mpid,msg_text,msg_senddate,msg_type,msg_target,msg_cuid)  
select @cmpid,'+convert(varchar(10),@mpid)+','+@mb+',getdate(),'''+@wxtype+''',cu_mobile  , cu_id 
from ' 
if (CHARINDEX('ord_',@mb+@msgfld)>0 or @datasource=999) set @sql=@sql+@dbname+@tbname +','
if (CHARINDEX('ord2_',@mb+@msgfld)>0) set @sql=@sql+@dbname+@tbname +'_'+convert(varchar(10),@datasource)+','
if (CHARINDEX('jb_',@mb+@msgfld)>0) set @sql=@sql+'mps_job,' 
if (@datasource=999) set @sql=@sql+@dbname+@tbname+'_users,' 
set @sql=@sql+'mps_login 
where '
if (CHARINDEX('ord_',@mb+@msgfld)>0 or @datasource=999) set @sql=@sql+' ord_id=@ordid and '
if (CHARINDEX('ord2_',@mb+@msgfld)>0) set @sql=@sql+' ord2_ordid=@ordid and '
if (CHARINDEX('jb_',@mb+@msgfld)>0) set @sql=@sql+' jb_id=@jbid and ' 
if (@datasource=999) set @sql=@sql+' ordp_ordid=ord_id and ordp_type='''+@msgfld+''' and cu_id=ordp_cuid and ' 
else set @sql=@sql+@msgfld+'=cu_id and ' 
set @sql=@sql+' cu_type=0 and len(cu_mobile)>=11 '+char(13)+char(10) 
if (@msgcount>0) 
set @sql=@sql+'and (cu_ding+cu_qyh+cu_gzh1+cu_gzh2+cu_gzh3+cu_gzh4+cu_gzh5)='''' ' +char(13)+char(10)  
end




set @mpid=0 ; set @emp='' ; set @mb='' ; set @wxtype='' ; set @wxcode=''  ; set @wxlen='0' ; set @msgfld='' ; set @datasource='' 
select @mpid=wrk_other_end_msgid , @msgfld=wrk_other_source,@datasource=wrk_other_datasource,@emp=mp_scope , @mb=mp_dayutext , @wxtype=wx_type , @wxcode=wx_code ,@mpname=mp_name,@mpcaption=mp_caption  
from mps_flowwork , ebs_kernel.dbo.pub_messagetemplate, ebs_kernel.dbo.pub_weixin 
where wrk_id=@wrkid and wrk_other_end_msgid>0 and mp_id=wrk_other_end_msgid and len(wrk_other_source)>5 and len(mp_dayutext)>10 and mp_dayuid=wx_id 
set @datasource= isnull(@datasource,0) 
if (@datasource<=0) set @datasource = 0
set @wxlen = convert(varchar(10),len(@wxcode)+2)  
if (@mpid>0)  
begin
set @icc = @icc + 1
set @sql=@sql+'
insert into ebs_kernel.dbo.pub_message(msg_cmpid,msg_mpid,msg_text,msg_senddate,msg_type,msg_target,msg_cuid)  
select @cmpid,'+convert(varchar(10),@mpid)+','+@mb+',getdate(),'''+@wxtype+''','+@msgfld+'  , 0  
from ' 

if (CHARINDEX('ord2_',@mb+@msgfld)>0) set @sql=@sql+@dbname+@tbname +'_'+convert(varchar(10),@datasource)+','
if (CHARINDEX('jb_',@mb+@msgfld)>0) set @sql=@sql+'mps_job,' 
set @sql=@sql+@dbname+@tbname +'  
where '
if (CHARINDEX('ord2_',@mb+@msgfld)>0) set @sql=@sql+' ord2_ordid=@ordid and '
if (CHARINDEX('jb_',@mb+@msgfld)>0) set @sql=@sql+' jb_id=@jbid and ' 
set @sql=@sql+' ord_id=@ordid and len('+@msgfld+')>=11 '+char(13)+char(10) 

end




set @mpid=0 ; set @emp='' ; set @mb='' ; set @wxtype='' ; set @wxcode=''  ; set @wxlen='0' ; set @msgfld='' ; set @datasource='' 
select @mpname=mp_name,@mpcaption=mp_caption ,@mpid=wrk_emp3_end_msgid , @msgfld=wrk_emp3_source,@datasource=wrk_emp3_datasource,@emp=mp_scope , @mb=mp_pctext  , @s1=mp_caption  
from mps_flowwork , ebs_kernel.dbo.pub_messagetemplate 
where wrk_id=@wrkid and wrk_emp3_end_msgid>0 and mp_id=wrk_emp3_end_msgid and len(wrk_emp3_source)>5 and len(mp_pctext)>10  
set @datasource= isnull(@datasource,0) 
if (@datasource<=0) set @datasource = 0
set @wxlen = convert(varchar(10),len(@wxcode)+2)  
if (@mpid>0)  
begin
set @icc = @icc + 1
set @sql=@sql+'
insert into mps_alarm_message(
 almmsg_cmpid,almmsg_almid,almmsg_identkey,almmsg_type,almmsg_title,almmsg_category,almmsg_cuid,almmsg_text,almmsg_objid,almmsg_jbid,almmsg_objtype) 
select @cmpid,'+convert(varchar(10),@mpid)+','','',''message'','''+@mpcaption+''','''+@mpname+''',cu_id,'+@mb+' , @ordid,@jbid ,''biz'' 
from ' 
if (CHARINDEX('ord_',@mb+@msgfld)>0 or @datasource=999) set @sql=@sql+@dbname+@tbname +','
if (CHARINDEX('ord2_',@mb+@msgfld)>0) set @sql=@sql+@dbname+@tbname +'_'+convert(varchar(10),@datasource)+','
if (CHARINDEX('jb_',@mb+@msgfld)>0) set @sql=@sql+'mps_job,' 
if (@datasource=999) set @sql=@sql+@dbname+@tbname+'_users,' 
set @sql=@sql+'mps_login 
where '
if (CHARINDEX('ord_',@mb+@msgfld)>0 or @datasource=999) set @sql=@sql+' ord_id=@ordid and '
if (CHARINDEX('ord2_',@mb+@msgfld)>0) set @sql=@sql+' ord2_ordid=@ordid and '
if (CHARINDEX('jb_',@mb+@msgfld)>0) set @sql=@sql+' jb_id=@jbid and ' 
if (@datasource=999) set @sql=@sql+' ordp_ordid=ord_id and ordp_type='''+@msgfld+''' and cu_id=ordp_cuid and ' 
else set @sql=@sql+@msgfld+'=cu_id and ' 
set @sql=@sql+' cu_type=0  '+char(13)+char(10) 
end

set @sql=@sql+char(13)+char(10)+'
set nocount off  
end  
'
exec myprint @sql 
if (@icc>0) execute(@sql)
set nocount off

end