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

insert into DSBCRM..tsubmitedit
(tsubmiteditid, siteid, createby, createdate, fstatus, rmkenable, tsubmiteditmodelid, tenterprise_userid, tagentsid,fisread)
select @tsubmiteditid + ROW_NUMBER() over (order by siteid),
       siteid, $createby$, getdate(),
       'ĐÂ½¨', 0, $tsubmiteditmodelid$, tenterprise_userid, tagentsid,0
from DSBCRM..tenterprise_users
where siteid=$siteid$ and tenterprise_userid=$tenterprise_userid$
  and not exists (select * from tsubmitedit where siteid=$siteid$ and tsubmiteditmodelid=$tsubmiteditmodelid$ and tenterprise_userid=$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$