Hi,
I had to build a similar solution some time ago.
Maybe it's a little overkill and the real Experts will tell a much easier solution, however here it goes:
I'm using two files: Query.sql and a batch file
Query.sql:
SET nocount ON
SELECT [name],[database_id],[state_desc] FROM [master].[sys].[databases]
GO
Batch file (collect.bat):
sqlcmd -S. -i "Query.sql" -o o_%date:~6,4%%date:~3,2%%date:~0,2%.csv -W -s ","
findstr /R /C:"^[^-^]" o_%date:~6,4%%date:~3,2%%date:~0,2%.csv > %date:~6,4%%date:~3,2%%date:~0,2%.csv
del o_%date:~6,4%%date:~3,2%%date:~0,2%.csv
Basically all these files placed locally in one directory. The batch file opens the Query.sql (connects to default instance using windows authentication and will produce the csv with the date in the name as you have requested.)
The other two rows are needed because for some reason the first csv will contain dashes in the second row, so you need to remove them.
In short: You can run the batch file just as above and put your t-sql query in the Query.sql file.
Final output from my example:
20141124.csv
name database_idstate_desc
master 1 ONLINE
tempdb 2 ONLINE
model 3 ONLINE
msdb 4 ONLINE
Hope that helps.
---------------------------------
First entry. Please be patient.
________________________________________________________________
"Accept conditions as they exist, or accept the responsibility for changing them."