How to run a .sql file in a database using the sqlcmd tool.

In this article we will explain how to upload a .sql file to a SQL Server using the SqlCmd tool.

The first thing we need is to save the .sql file to a simple location to be used in the sqlcmd command.

For our example, the URL of the file will be C:\sc\script.sql

And the access credentials to the server are:

Server: .\SQLExpress
User: sa
Password: 123456

SqlCmd tells us that the sentence for this operation is the following:

sqlcmd -S [Server] -U [User] -P [Pasw] -i [Location of the file .sql] -o [file .rpt]

Where [file.rpt] refers to the server response after executing the .sql file script.

Therefore, for our example, the sentence would be the following:

sqlcmd -S .\SQLExpress -U sa -P 123456 -i C:\sc\script.sql -o C:\sc\out.rpt

To execute it, we only need to enter the windows console with administrator privileges and paste the script, as shown in the following image:

Then we simply press enter, and wait for the script to be executed.

The cursor will be located in the location before executing the command in the cmd console and an .rpt file will be generated in the location indicated in the syntax explained above, as shown in the following image:

For this example, the following script was used:

And the response from the server (archivo.rpt) is as follows:

If you have any questions or need advice or help on any other problem associated with development, you can leave your comments or communicate with us through the option Contact us.

Do not forget to follow us on our social networks:

Facebook / SoftwareLight.


Leave a Comment

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