Sqlserver
 sql >> Base de données >  >> RDS >> Sqlserver

Utilisation du curseur dans dbMail dans SQL Server 2008

Je pense que vous voudriez construire votre e-mail à l'intérieur du curseur - quelque chose comme

DECLARE @body nvarchar(max)
DECLARE EmailCursor CURSOR FOR 
  SELECT checknum, checkamt, email FROM .... -- one row per required email

OPEN EmailCursor
FETCH NEXT FROM EmailCursor INTO  @checknum, @checkAmt, @EMAIL
WHILE (@@FETCH_STATUS = 0)
BEGIN
  -- do the bit to build email in here 
  set @body = '<table>'
  select @body = @body + '<tr><td>' + docnum + '</td>'

-- .... rest of fields here
                       + '<td>'+ Cardcode +'</td></tr>'
  from -- .... 
  where checknum = @checknum -- or whatever gives this context
  set @body = @body + '</table>'

  exec  msdb.dbo.sp_send_dbmail -- ...

  FETCH NEXT FROM EmailCursor INTO  @checknum, @checkAmt, @EMAIL
end