We help IT Professionals succeed at work.
Get Started

SQL Query Output to file

Last Modified: 2015-07-14
I am running MSSQL 2012 on Windows 2008
I have a SQL Query that is run on a daily basis and outputs to a Text file. This is done by a Batch file that uses SQLCMD similar to the following ; sqlcmd -S %SERVERNAME% -d DATABASE -s "|" -m1 -W -h-1 -i J:\Folder1\Query1.sql  -o J:\Folder1\Output\Results_%DATE:~4,2%_%DATE:~7,2%_%DATE:~-4%.txt

This Batch file is kicked off using Windows Task Scheduler. Once it completes another batch file is scheduled to move the file from the local folder to a shared folder on another server.

The SQL Query Batch is beginning to run long and past the start time of the Move Batch. I have adjusted the start times to fix this, but I do not want to keep doing this.

What I would like to do, if possible, is leverage SQL Server Agent to schedule a job in SSMS the Runs the Query and Outputs the results directly to the Shared folder on the other server. Is this possible? If so How?
Watch Question
"Batchelor", Developer and EE Topic Advisor
Top Expert 2015
This problem has been solved!
Unlock 1 Answer and 13 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE