How to run a SQL Server script from the command line

To run the script file

  1. Open a command prompt window.
  2. In the Command Prompt window, type: sqlcmd -S myServer\instanceName -i C:\myScript.sql
  3. Press ENTER.

To save this output to a text file

  1. Open a command prompt window.
  2. In the Command Prompt window, type: sqlcmd -S myServer\instanceName -i C:\myScript.sql -o C:\EmpAdds.txt
  3. Press ENTER.

No output is returned in the Command Prompt window. Instead, the output is sent to the EmpAdds.txt file. You can verify this output by opening the EmpAdds.txt file.

Author: Marcelo Ruiz

Marcelo has been working as a software developer for more than 15 years. He has participated in projects for companies in USA, Mexico, Argentina, Europe and Africa. He is skilled with Microsoft technologies such as ASP.NET, MVC, C#, WCF and SQL Server, among others.

Leave a Reply

Your email address will not be published. Required fields are marked *