Question

enter image description here

I want to spool my output to a particular file. My database in SqlServer. I am entering code in command prompt like this:

enter image description here

First I am connecting to my data base like this:

sqlcmd -S SUPPORT2/SUPPORT2 -U sa -P solutions

SUPPORT2/SUPPORT2 is a my server name.
I choose my database name(vallett), then I am selecting Ename from EmployeeMaster_tbl.
I want to spool this output to a particular word file, how can I do this?

I tried somthing like this..but getting an error

Was it helpful?

Solution

-o is a parameter for the call of sqlcmd a call could look like this

sqlcmd -S SUPPORT2/SUPPORT2 -U sa -P solutions -Q " SELECT Ename from Vallett.dbo.EmployeeMaster_tbl" -o C:\temp\test.txt 

Make shure the destination file can be written (C:\txt1.txt might not be possible)

If you want to create the output file interactive you can use :OUT to redirect the output to a file and reset ist to stdout. An example could look like this:

 sqlcmd -S SUPPORT2/SUPPORT2 -U sa -P solutions 
 use Vallett
 GO
 :OUT C:\temp\test.txt
 SELECT Ename from dbo.EmployeeMaster_tbl
 GO
 :OUT stdout

OTHER TIPS

Another option. If you don't need an interactive query, you can load a file with the query, and the following standard output redirection allows to add the output to a file with previous content.

sqlcmd -S SUPPORT2/SUPPORT2 -U sa -P solutions -i file_w_query.sql >> C:\temp\file_output.txt

And using a single symbol redirection, there is not difference with using the parameter -o

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top