5

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?

latonz
  • 1,601
  • 10
  • 21
Vishal
  • 12,133
  • 17
  • 82
  • 128
  • possible duplicate of [how do i run a script using a BAT file](http://stackoverflow.com/questions/3258975/how-do-i-run-a-script-using-a-bat-file) – driis Jul 20 '10 at 21:37

5 Answers5

16

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.

Community
  • 1
  • 1
driis
  • 161,458
  • 45
  • 265
  • 341
3

See the sqlcmd utility:

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

This allows you to run sql scripts from the command line

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
1

osql:

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

Tahbaza
  • 9,486
  • 2
  • 26
  • 39
1

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>
Lèse majesté
  • 7,923
  • 2
  • 33
  • 44
0

Hope this helps you :

sqlplus UserName/Password@DataBase @C:\sqlFolder\sqlFile.sql

P.S : Don't forget to add the command "commit;" at the end of sql file (sqlFile.sql), this command order Oracle to save performed changes in database

Shessuky
  • 1,846
  • 21
  • 24