declare @tmessageid bigint
set @tmessageid = (select isnull(MAX(tmessageid), 0)
                   from tmessage)
insert into tmessage
(tmessageid, siteid, createdate, rmkenable, fmessage, ftype, fisread, freaddate, tenterprise_userid, objectname,
 objectid)
select @tmessageid + 1,
       t1.siteid,
       getDate(),
       0,
       '【' + isnull(t1.fname,'') + '】在【' + CONVERT(varchar(100), t1.createdate, 120) +
       '】提交了一个线索《' + isnull(t3.ftitle,'') + '》！',
       '系统',
       0,
       null,
       case when isnull(t2.fsaler2id, '') = '' then t2.fsaler1id else t2.fsaler2id end,
       'tagentactivities', t3.tagentactivitiesid
from tagentactivities_form t1
    inner join tagentactivities_share t2
on t1.siteid=t2.siteid and t1.tagentactivitiesid=t2.tagentactivitiesid and t1.tagentactivities_shareid=t2.tagentactivities_shareid
    inner join tagentactivities t3 on t2.siteid=t3.siteid and t2.tagentactivitiesid=t3.tagentactivitiesid
where t1.siteid=$siteid$
  and t1.tagentactivitiesid=$tagentactivitiesid$
  and t1.tagentactivities_shareid=$tagentactivities_shareid$
  and t1.tagentactivities_formid=$tagentactivities_formid$