Sqlserver
 sql >> Datenbank >  >> RDS >> Sqlserver

Verwenden des Cursors in dbMail in SQL Server 2008

Ich denke, Sie möchten Ihre E-Mail innerhalb des Cursors erstellen - etwas in der Art von

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