0

I'm trying to automate a SQL query that needs to run on multiple databases. The query is generally run at the beginning of the week manually, ran for each individual database, and then exported to a CSV.

I need to find a simple way to have this query ran and have the result set exported to a CSV for me.

digital.aaron
  • 5,435
  • 2
  • 24
  • 43
Sarah
  • 1
  • 1
  • 2
  • Take a look at the Powershell solution https://stackoverflow.com/questions/3169220/export-query-result-to-csv-file-in-sql-server-2008 – Philippe Dec 21 '17 at 16:43
  • For some reason, my Powershell SQL addins are not installed. -- Is there any other method I could accomplish this task? – Sarah Dec 21 '17 at 17:22
  • Are the databases on the same server? Are the table names the same between all the dbs? If the answers to both questions are `yes`, then I'd put all the db names in a table, and use that table and your existing query to build and execute dynamic sql. Then I'd put the whole thing into a SQL Agent job and give it a schedule to run on. – digital.aaron Dec 21 '17 at 19:28
  • Can the SQL Agent Job export to a CSV once it finishes running the query? – Sarah Dec 21 '17 at 20:17
  • No, but you could create an SSIS package that the job would run. The package would use the same technique I mentioned above, and it would also be able to write the results to a .csv. – digital.aaron Dec 21 '17 at 21:38

0 Answers0