I can execute the query via batch file and send it to the folder but I need to send the results to an email and I am not sure how to send the sql query/sp results in an email via batch file.
Any suggestions and help is appreciated.
Thanks
I can execute the query via batch file and send it to the folder but I need to send the results to an email and I am not sure how to send the sql query/sp results in an email via batch file.
Any suggestions and help is appreciated.
Thanks
It sounds like you're executing your SQL query and I'm guess dumping it to a text file (let's say output.txt). I'm assuming Windows and I'm assuming SQL Server since you didn't specify. The answer is below, but I would like to offer two options instead:
OPTION 1: Setup SQL Server to send your email which would have several benefits including:
It's not hard to setup email in SQL and is clearly explained here:
How to send email from SQL Server?
OPTION 2: Use PowerShell to make your SQL call.
The answer to your question (below) uses PowerShell to send the email message. Since you'll use PS for that you might as well use PS to make your database call. You can find hundreds of examples of how to call SQL from PS.
Finally the answer you asked for...
If you want to continue to use a command line solution (maybe you have to kick off a bunch of non-SQL commands) you'll need to use PowerShell (assuming you don't want start making things complicated, like writing your own .exe).
STEP 1: Read the contents of the query (output.txt) by using:
$sqlOutput = Get-Content Output.txt -Raw
STEP 2: Use $sqlOutput
as the body of the email. Here's a nice walkthrough:
STEP 3: If you want to attach the file as an attachment, you can use the example here:
https://msdn.microsoft.com/en-us/library/system.net.mail.attachment(v=vs.110).aspx