question

sand143eep avatar image
sand143eep asked

NEED to send a mail through sql

Hello all, i am trying to send an email using the tsql statement as below: USE msdb EXEC sp_send_dbmail @profile_name = 'DB-Admin', @recipients = 'madhava.vemagoti@synechron.com', @subject = 'POF-DB Analysis', @body = 'The result from SELECT is appended below.', @execute_query_database = 'msdb', @query =print ' hello, how are u doing dude how was the life Regarda, duda. ' when i am executing the query then i am getting the error:Msg 156, Level 15, State 1, Line 6 Incorrect syntax near the keyword 'print'. please suggest how to rectify this error
sql-server-2005dbmail
5 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Actually i need a mail to be sent by printing some text in that mail to the users so i used print can u suggest how i can print the message
0 Likes 0 ·
That is what the @body parameter is for, that is where the body of the email is configured
0 Likes 0 ·
in the sence shall i proceed with this USE msdb EXEC sp_send_dbmail @profile_name = 'DB-Admin', @recipients = 'madhava.vemagoti@synechron.com', @subject = 'POF-DB Analysis', @body = 'print ' hello, how are u feeling with Support, how was the life Regarda, duda. '', @execute_query_database = 'msdb' then i got the error as shown below: Msg 102, Level 15, State 1, Line 8 Incorrect syntax near 'hello'. Msg 319, Level 15, State 1, Line 10 Incorrect syntax near the keyword 'with'. If this statement is a common table expression or an xmlnamespaces clause, the previous statement must be terminated with a semicolon. Msg 102, Level 15, State 1, Line 11 Incorrect syntax near 'making'.
0 Likes 0 ·
No. Dont use 'print', it means nothing there. use @body = 'The message you want to send' That is all you need
0 Likes 0 ·
Thanks a lot Fatherjack for your support it worked.
0 Likes 0 ·

1 Answer

·
Fatherjack avatar image
Fatherjack answered
You need to remove the word print and replace your text with a TSQL query. This currently has no meaning or context in the place you have it. the @query variable is being assigned the string that you need simply by using the = operator and will be executed when the sp_send_dbmail procedure runs. If you change your code to .... @execute_query_database = 'adventureworks', @query = 'select top 5 * from person.contact' then you will find an email is sent with the results of the query
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.