declare @tsubmiteditid bigint
set @tsubmiteditid=(select isnull(MAX(tsubmiteditid),0) from tsubmitedit)

insert into DSBCRM..tsubmitedit
(tsubmiteditid,siteid, createby, changeby, createdate, changedate,fstatus, rmkenable, tsubmiteditmodelid, tenterprise_userid,tagentsid,fisread)
select @tsubmiteditid+ROW_NUMBER()over(order by t1.siteid),t1.siteid, $createby$, $createby$, getdate(), getdate(),'ÐÂ½¨', 0, $tsubmiteditmodelid$,t1.tenterprise_userid,t1.tagentsid,0
from DSBCRM..tenterprise_users t1
where t1.siteid=$siteid$ and t1.fusertype='ÆóÒµ'and t1.fphonenumber is not null
and not exists(select *from tsubmitedit where siteid=$siteid$ and tsubmiteditmodelid=$tsubmiteditmodelid$  and tenterprise_userid=t1.tenterprise_userid)

update tsubmiteditmodel
set frequireqty=t.fcount
from (
       select count(0) fcount from tsubmitedit where siteid =$siteid$ and tsubmiteditmodelid=$tsubmiteditmodelid$
     ) t
where tsubmiteditmodel.siteid =$siteid$ and tsubmiteditmodel.tsubmiteditmodelid=$tsubmiteditmodelid$