1

I was able to write a SQL query on a SQL Server 2008 database to show the data I need. Now I need to figure out how to export the query into a csv file automatically every night with the date of the export as the file name.

My research tells me there are two ways to do it. One is using task scheduler to run a query file, or to use the server agent. Unfortunately I'm not able to get to agent working so I'll have to figure out how to write a query that will do what I need.

1
  • 1
    Are you versed in PowerShell? You could write a small script that connects to the database, runs the query, puts the results in a CSV format and writes it to a file. Then you can schedule the execution of the task in the Windows Task Scheduler or any other scheduling system you or your client has. Here's one example I found: billfellows.blogspot.com/2011/03/… Commented Apr 14, 2016 at 21:58

2 Answers 2

1

DOS batch script, then schedule the task to run it.

set mydate=%date:~6,4%%date:~3,2%%date:~0,2%
sqlcmd -U UserName -P Password -S ServerName -Q "SELECT * FROM TABLE" -o "File_Name_%mydate%.csv" -s","
Sign up to request clarification or add additional context in comments.

Comments

1

Thanks to the powershell script link posted by CORY, I modified the script to access my database and then used (Get-Date).AddDays(-1).ToString('MM-dd-yyyy') to get the yesterday's date in string, then put it all together for the Export-CSV file path.

I'm going to spend some time exploring Powershell to see what else it can do.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.