Executing the script in batch file using “sqlcmd” command
I often create batch files to execute sql scripts ( specially SPs) and then schedule it using SQL agent job/Task Scheduler. In this post , I will show you how to create a batch file with sql commands.
First of all, The command that is used to execute SQL queries from commnd prompt is “SQLCMD“. The general syntax of this command is available in the link. I will go with a simple one as below
sqlcmd –E –S Servername –Q Query -o “Output Path”
C:\Users\geekEpisodes> sqlcmd -E -S “Local-SRV01″ -Q “Select top 10 * from MyTestDatabase..[ForecastHistory]” -o “C:\New Folder\Output.txt”
All the commands that you execute in the command prompt can be executed from batch/command files. to create a batch file with SQL commands follow the steps below