I'm trying to run a sql script from a file using sqlcmd
, using the following command:
sqlcmd -S <server> -d <database> -i <input file> -o <output file>
-U <user> -P <password>
I run my sql file and output to a log file.
The problem is this change the output of sqlcmd
to the file.. and i want to get the output also to the shell.
@Noam,
There's a standard program in UNIX world that reads from stdin and writes to stdout and to a specified file: the tee command. The following example lists the current directory to stdout and to "myfile":
$ ls | tee "myfile"
There are some open-source ports for windows, like wintee, and it's even trivial to make your own implementation (see here), but PowerShell actually has already this utility built-in: the Tee-Object.The following example is analogous the previous, in PowerShell:
PS [c:\tmp]
> dir | tee myDirContents.txt
Hence, the following command will execute myscript.sql into myserver, using current windows credentials and its result will be output either to the console and to "result.txt":
PS [c:\tmp]
> sqlcmd -i mysrcipt.sql -S myserver -E | tee "result.txt"