Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run .sql file from a batch file?

I am running sql server 2008 express and i need to schedule some stored procedures to run nightly...so i have built out these .sql files which i would want to run from .bat file...i need to know the command to execute these .sql files one by one and store their results i guess...can anyone help me out?

like image 273
Vishal Avatar asked Jul 20 '10 21:07

Vishal


4 Answers

I answered this in this other question:

You should invoke the sqlcmd command-line tool from your batch file. Assuming your sql file is "backup.sql", the command line would be something like:

sqlcmd -E -S yoursqlinstance -i backup.sql

-E uses trusted connection, replace with -U and -P if you need to specify a SQL username and password. See also this article with examples.

like image 120
driis Avatar answered Nov 09 '22 15:11

driis


See the sqlcmd utility:

http://msdn.microsoft.com/en-us/library/ms165702.aspx

This allows you to run sql scripts from the command line

like image 44
marc_s Avatar answered Nov 09 '22 14:11

marc_s


osql:

http://www.di-mgt.com.au/osqlUtility.htm

like image 32
Tahbaza Avatar answered Nov 09 '22 16:11

Tahbaza


I don't use SQL Server, but a batch file is just a list of DOS commands. So whatever you use to execute SQL files from the commandline can be used in a batch file.

A quick google search turns up:

sqlcmd -i <inputfile> -o <outputfile>
like image 35
Lèse majesté Avatar answered Nov 09 '22 16:11

Lèse majesté