How to Execute SQL Statements in Command Prompt (Cmd)

how to execute SQL statements in command prompt (CMD)

You are looking for the sqlcmd utility lets you enter Transact-SQL statements, system procedures, and script files at the command prompt

sqlcmd -U myLogin -P myPassword -S MyServerName -d MyDatabaseName 
-Q "query"

Refer this

Edit: The OP said The sqlcmd.exe file is available in the installation path C:\Program Files\Microsoft SQL Server\110\Tools\Binn

You are executing with C:\Users> make the path to C:\Program Files\Microsoft SQL Server\110\Tools\Binn and execute sqlcmd or add the sqlcmd path (C:\Program Files\Microsoft SQL Server\110\Tools\Binn) to system PATH

Execute SQL script from command line

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"

How to I run a single sql command from the sqlcmd prompt?

I think you want the -q switch:

sqlcmd -S localhost -U MyUser -P MyPass -d MyDb -q "SELECT 1"

Documentation is here

Run SQL script via command line to remote server

-d - Specifies the database name - learn.microsoft.com/en-us/sql/tools/sqlcmd-utility – Ctznkane525

sqlcmd -S myServer\instanceName -i C:\myScript.sql -d DatabaseName


Related Topics



Leave a reply



Submit