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)
select @tmessageid+ROW_NUMBER()over(order by fphonenumber), siteid, getDate(), 0, $fmessage$, $ftype$, 0, null, tenterprise_userid
from tenterprise_users
where fphonenumber is not null and siteid=$siteid$ and ($tenterprise_userid$=0 or tenterprise_userid = $tenterprise_userid$)