SQL Server How to run an SQL script in a batch file

pu3pd22g  于 2023-11-16  发布在  其他
关注(0)|答案(2)|浏览(131)

I have a a 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?

cetgtptt

cetgtptt1#

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 .

ebdffaop

ebdffaop2#

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

相关问题