Wednesday, March 21, 2012

Saving a query to a csv file

Let's say I have a query such as

SELECT * FROM WORKORDER

I want to save the query to a .csv file which I understand I can set up in the query analyzer iptions. However, I want to be able to overqrite any existing data that is in the file and I don't want to be prompted about saving the file. I need to do this because this will be part of a job that runs this query and then I need to e-mail the results to another user. This job will run around midnight when no one is in the office and the .csv file should be available the first thing the next morning for use. Is there a way to do this by adding to the code abave?You can use bcp with a format file or command-line option to do this easily. Optionally, you can also use OSQL with command-line options but it is slightly tedious to get the correct output since you have to tweak lot of parameters. Lastly, you can actually use ISQLW itself from command-line. Use bcp since that gives more flexibility.

No comments:

Post a Comment