简体   繁体   中英

How to run an SQL script in a batch file

I have a an sql script that creates a database, and want to know how to run this from a batch file at a command prompt. Do i create a batch file with a few lines of code pointing to the location of the .sql file, or create a new batch file containing the contents of the .sql file?

I've had a look at a couple of related questions, but can't seem to see a clear answer.

Thanks :)

You could create a batch file and use the -i flag with sqlcmd.exe, where -i sets the path to the .sql file you want to run:

sqlcmd.exe -i F:\wherever\the\file\is

See http://msdn.microsoft.com/en-us/library/ms162773.aspx for a full list of the flags and this post, How to use sqlcmd to create a database .

is quite easy, you only need 3 things:

SQL Client Path/ Script Path/ SQL Conection data/

Then you can run it directly from the batch without any complex script - let me know if you have the info I can arrange it.

regards

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM