I have the following stored proc. I want to embed the declared variables into the body and subject of outgoing emails, but I continue to get the error "incorrect syntax near '+'" However, when I use literals, the code works.
Thanks in advance,
Donald
Code:
DECLARE @cardname varchar(100),
@mailrecipients varchar(100),
@numatcard varchar(100),
@trackno varchar(50)
SELECT T1.E_mail,T0.CardName,T0.NumAtCard,T0.U_trackno AS TrackNo, 0 as processed
INTO #maillist
FROM ODLN T0 INNER JOIN OCRD T1 ON T0.CardCode = T1.CardCode
WHERE T0.SlpCode = 3 and T0.DocDate > getdate()-1
WHILE (select count(*) from #maillist where processed = 0) > 0
BEGIN
SELECT TOP 1 @cardname = isnull(CardName,''),
@numatcard = isnull(NumAtCard,''),
@trackno = isnull(TrackNo,''),
@mailrecipients = isnull(E_mail,'')
FROM #maillist
WHERE processed = 0
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'MyMailProfile',
@recipients = @mailrecipients,
@subject = @numatcard + 'hello',
@body = @cardname + ',<br>Thank you for your recent online Goorin purchase. As the industry leader, we are confident that our quality and craftsmanship will give you many years of enjoyment from your hat. <br>We are pleased to inform you that order:' + @numatcard + 'has shipped. <br>You may track your shipment at: [URL unfurl="true"]http://www.goorin.com/'+[/URL] @numatcard + '.htm' ,
@body_format = 'HTML';
UPDATE #maillist set processed = 1 where CardName = @cardname
END;
SELECT * FROM #maillist;
DROP TABLE #maillist;
Thanks in advance,
Donald