We help IT Professionals succeed at work.

How do I "spool" query output to a txt file?

abrusko asked
Hi, can someone please tell how I would "spool" the results of a select statement to a text file for future processing?

I'm a little unclear (I'm familiar with Oracle/SQL*Plus) on how I would run a query directly on the server as well, so if someone could tell me how I go about running a query on the server AND how I can have the results of that query "spool" to a .TXT file, I would be much obliged.

Thanks alot!
Watch Question

You could use "bcp" with the queryout option, not quite like a SPOOL in PL/SQL but close enough. BCP will not put the column headers, that's all. For example, your query is "SELECT * FROM DB1.dbo.TABLE1" on server SERVER, you could do --
bcp "SELECT * FROM DB1.dbo.TABLE1" queryout Table1Select.txt -SSERVER -Usa -P -c
in the simplest format (you can define your field and row terminators with -t and -r options, default for -t is tab and -r is newline). Substitute -Usa -P with the appropriate UserID and Password. HTH.
try osql wih command line option -o

osql -S. -E -Q "sp_dboption 'master'" -o C:\my_file.txt


miron...thanks...we ended up using OSQL

jchopde...thanks for your help too, but the OSQL solution was best for us...thanks again to both of you...I appreciate it!