I need to run a script saved in my computer through CMD (SQLCMD) and save the result in other file in my computer.
How to execute a file.sql (script) and save the results in a file in my computer using CMD (SQLCMD)?
Asked
Active
Viewed 2,698 times
-2
-
If that's what you need to do, what's stopping you? What have you tried so far and why didn't it work? If you've not tried anything, why not? What research did you perform instead and what about it didn't you understand? If you tell us we can help explain it in more simple terms or with further examples. (Also, I recommend using PowerShell over CMD now, especially in a Windows 10 environment, where CMD doesn't really exist anymore.) – Thom A Apr 24 '19 at 16:56
3 Answers
2
Use SQLCMD to run a script from command line, see:
https://learn.microsoft.com/en-us/sql/tools/sqlcmd-utility?view=sql-server-2017
To save output to a file, look into using pipes for your output, see:
Redirect Windows cmd stdout and stderr to a single file
or
https://helpdeskgeek.com/how-to/redirect-output-from-command-line-to-text-file/

Jon Roberts
- 2,262
- 2
- 13
- 17
1
You can run the script and then create another backup.
You can try mssql-scripter that is a multiplatform SQL command line tools such as sqlcmd.

Kevin Hernández
- 704
- 10
- 25
0
Use bcp
bcp "SELECT * FROM TABLE" queryout file.csv -t";" -SSERVERNAME -T -c -e errors.txt

Alex M
- 141
- 2
- 6