I have a script that I need to execute using a batch file. Do I use SQLCMD in the batch file to run the script? Also, the script inserts data to a table in a database. How should I format the SQLCMD in the batch file so it knows what database it is suppose to work with?
Share
First, save your query into an sql text file (text file with .sql extension). Make sure to add the USE statement at the beginning, which tells the server which database you want to work with. Using the example from MSDN:
Then in your batch file, you run SQLCMD and pass it the sql file (with path) as a parameter.
If you need to authenticate as well, you’ll need to add in -U and -P parameters to your SQLCMD command.
Here’s an MSDN article dealing with the sqlcmd utility with more details.