Send DBMail SP error

  • I am using code below, attempting to save query results to a file to be emailed using the Send DBMail SP (which I now have working)

    But when executing this -

    exec msdb.dbo.sp_send_dbmail

    @profile_name = 'Default Public Profile',

    @recipients = 'example@example.com',

    --@copy_recipients = 'example@example.com',

    @subject = 'I022 flows',

    @query = 'select * from [GATEKEEPER].[dbo].[DATAFLOW_CLOB] where [RAW_CLOB]

    LIKE 'AAA|S0141007%SPH%'',

    @attach_query_result_as_file = 1,

    @query_attachment_filename = 'test.csv'

    GO

    I'm getting this error -

    Msg 102, Level 15, State 1, Line 9

    Incorrect syntax near 'AAA'.

    I can understand why as I'm effectively closing the select query with the apostrophe so it doesn't pick up the AAA onwards, I'm just wondering if anyone knows a way around this.

    Thanks for any help in advance.

  • Add more apostrophes 🙂

    exec msdb.dbo.sp_send_dbmail

    @profile_name = 'Default Public Profile',

    @recipients = 'example@example.com',

    --@copy_recipients = 'example@example.com',

    @subject = 'I022 flows',

    @query = 'select * from [GATEKEEPER].[dbo].[DATAFLOW_CLOB] where [RAW_CLOB]

    LIKE ''AAA|S0141007%SPH%''',

    @attach_query_result_as_file = 1,

    @query_attachment_filename = 'test.csv'

    GO

  • Thanks 🙂 that worked.

  • No problem 🙂

Viewing 4 posts - 1 through 3 (of 3 total)

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