Database Mail - EXEC msdb.dbo.sp_send_dbmail

  • I have a Row Count that saves to a variable RowCountResult. How do I include that in the body of my email? I'd ilke the body to say something like: There are ___ records.

    I'd also like it to send an attachment, but the attachment has a dynamic filename. Something like: C:\Ryan\Output_072109.xls or C:\Ryan\Output_June_2009.xls

    EXEC msdb.dbo.sp_send_dbmail

    @recipients=N'ryan@test.com',

    @subject ='Test Message Subject',

    @profile_name ='Ryan_Test'

    @body = ???

    @file_attachments = ???

    Any input/solution/comment is greatly appreciated!!

    Merci!

    Ryan

  • Hi Ryan,

    Something like this where @YouVar is the variable with your count

    DECLARE @MessageBody AS VARCHAR(255)

    SET @MessageBody = 'Contains ' + @YourVar + ' records'

    EXEC msdb.dbo.sp_send_dbmail

    @recipients=N'ryan@test.com',

    @subject ='Test Message Subject',

    @profile_name ='Ryan_Test'

    @body = @MessageBody,

    @file_attachments = ???

    But watch out for @YourVar being NULL

    You can probably do something similar with your attachment

    Allen

  • I have:

    Set @body = 'This step generated ' + @RowCountResult + ' records.'

    But I'm getting an error: "Must declare the scalar variable "@RowCountResult"

    I searched it and the solutions don't make sense to me. Replace "@RowCountResult" with "?" ?? Kind of weird.

    @RowCountResult is Int32 DataType so I tried: CAST(@RowCountResult AS varchar(10)) but it didn't work.

  • Can you post the SQL?

  • Declare @body VARCHAR(255)

    Set @body = 'This step generated ' + CAST(@RowCountResult AS varchar(10)) + ' records.'

    EXEC msdb.dbo.sp_send_dbmail

    @recipients=N'ryan@test.com',

    @subject = 'Message Subject',

    @profile_name ='Ryan_Test',

    @body = @body

    I'm guessing it's because I didn't declare/set @RowCountResult in this Execute SQL Task?

  • Great - I think you answered the question!

  • hehe So how do I do that exactly?

    Declare @RowCountResult Int (or Varchar??)

    Set @RowCountResult = 'SELECT Count(*) AS CountResult From Table Where name = 'Ryan''

    I'm getting a syntax error.

  • SELECT @RowCountResult = Count(*) AS CountResult From Table Where name = 'Ryan'

  • actually u may get errors because of no values or null values.

    Check out my blog for this section:

    http://sqlscape.blogspot.com/2009_03_01_archive.html

    It has the section to set the value correctly.

    Hope it helps.

  • More like this (on sql 2005)

    DECLARE @i AS INT

    SELECT @i = COUNT(*) FROM sys.columns WHERE object_id = 8

    SELECT @i -- lets see the result

  • Probably no need for NULL check because

    DECLARE @i AS INT

    SELECT @i = COUNT(*) FROM sys.columns WHERE 1 = 0

    SELECT @i

    Returns 0

    but no bad thing to mention our friend/enemy NULL 🙂

  • It worked!! Thanks so much for the help!

    I'm not worried about the NULL values. I have a Row Count that executes first and I can just edit the Constraint going to this Execute SQL Task. 🙂

    Thanks again!

  • Very welcome

Viewing 13 posts - 1 through 12 (of 12 total)

You must be logged in to reply to this topic. Login to reply