Skip to content
Advertisement

How to execute the SQL Script through Batch Script Command from SQL?

I am trying to execute the .sql file from batch script, but not able to execute. Script has multiple scripts such as Create Table , Stored Procedure separated by GO statement.

I tried with the below script, however script was not executed.

DB Servername : localhostSQL2017
Authentication : Windows

sqlcmd -E -S localhostSQL2017MyDatabase -i C:SQLScriptsTestScript.sql

Advertisement

Answer

Try this instead:

sqlcmd -E -S localhostSQL2017 -d MyDatabase -i C:SQLScriptsTestScript.sql

You need to use the -d parameter to specify the database name. The -S parameter should contain only the server name and the instance.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement