Search code examples
sql-servercommandsqlcmd

spool output to particular file from command prompt


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


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