5 pts.
 Filename from SQL 2005 Query
I have a batch file that runs a SQL query command overnight once a week and outputs the results of that query to a file as a .csv file. The query produces, amongst other things, a week ending date. Is it possible to to name the output file with result of the week ending column in the query. All rows will have the same week ending date in the column. The output file would be something like "NPQ_Asst_2010-11-12.csv"

Software/Hardware used:
SQL 2005
ASKED: November 12, 2010  12:56 PM
UPDATED: November 12, 2010  7:15 PM

Answer Wiki:
Are you using sqlcmd to execute the query ? Assuming so, here's a way to do it ( though I'm sure there must be a cleaner way to do it): <pre>sqlcmd -U username -P password -S SERVERINSTANCE -Q "SELECT TOP(1) week_ending_date FROM your_table" -o <b>date.txt</b> -h -1 set /p <b>var</b>= < <b>date.txt</b> set <b>file</b>=NPQ_Asst_<b>%var%</b>.csv sqlcmd -U username -P password -S SERVERINSTANCE -Q "YOUR SQL QUERY" -o <b>file</b></pre> In summary, it executes a query to read the ending date and puts the result into a temp file, whose contents are then assigned to a variable, which is used to construct the file name. Hope this helps.
Last Wiki Answer Submitted:  November 12, 2010  7:15 pm  by  carlosdl   63,580 pts.
All Answer Wiki Contributors:  carlosdl   63,580 pts.
To see all answers submitted to the Answer Wiki: View Answer History.


Discuss This Question:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _