Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute SQL script from command line

I need to alter a database using a batch file, for a simple example, drop a table. I´m using local SQL Express (SQL Server 2008 R2) with user sa and its password.

How would the bat file be?

How can I specify in the script the password and that I use in SQL Express?

like image 805
blur Avatar asked Jul 18 '11 17:07

blur


People also ask

How do I run a MySQL script from command line?

use the MySQL command line client: mysql -h hostname -u user database < path/to/test. sql. Install the MySQL GUI tools and open your SQL file, then execute it.


1 Answers

Take a look at the sqlcmd utility. It allows you to execute SQL from the command line.

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

It's all in there in the documentation, but the syntax should look something like this:

sqlcmd -U myLogin -P myPassword -S MyServerName -d MyDatabaseName      -Q "DROP TABLE MyTable" 
like image 141
rsbarro Avatar answered Oct 11 '22 09:10

rsbarro