JDBC Tutorials - Herong's Tutorial Examples - Version 3.02, by Dr. Herong Yang
SQLCMD SQL Server Command Line Tool
This section describes how to SQL Server command line tool - SQLCMD.
Before you start using Java programs to interact with SQL Server databases, you should learn some SQL Server client tools to run SQL statements on the SQL Server. SQLCMD, Query Analyzer, and Management Studio are good client tools to use. But SQLCMD comes with SQL Server 2014 Express Edition installation (if you select the "With Tools" option), and is ready to use.
This tutorial shows you how to use SQLCMD, a command line client tool, to run SQL statements on your local SQL Server databases.
When "sqlcmd" is started and connected to a SQL Server, it will allow you to enter statements or commands. You can enter one or more statements in one or more lines to form a Transact-SQL statement batch.
To end a batch of statements and send it to the SQL Server for execution, you need to enter the GO command. The following "sqlcmd" tutorial session sends one batch to the SQL Server:
C:\herong>"\Program Files\Microsoft SQL Server\Client SDK \ODBC\110\Tools\Binn\SQLCMD.exe" -S localhost\SQLEXPRESS -U sa -P HerongY@ng 1> SELECT DB_NAME(); 2> GO ------------------------ master (1 rows affected) 1>QUIT
As you can see, I logged in to the server as "sa" with no problem. The "GO" command sends one SQL statement to the server. The returning output telling me that I was connected to the "master" database.
By the way, if you forget to specify the database instance name (\SQLEXPRESS) in the "-S" option, you will get the following error:
C:\herong>"\Program Files\Microsoft SQL Server\Client SDK \ODBC\110\Tools\Binn\SQLCMD.exe" -S localhost -U sa -P HerongY@ng Sqlcmd: Error: Microsoft ODBC Driver 11 for SQL Server : Named Pipes Provider: Could not open a connection to SQL Server  Sqlcmd: Error: Microsoft ODBC Driver 11 for SQL Server : Login timeout expired. Sqlcmd: Error: Microsoft ODBC Driver 11 for SQL Server : A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. ...
Also note that, in older versions of SQL Server, SQLCMD tool is located at a different folder. For example, "C:\Program Files\Microsoft SQL Server\100\Tools\Binn".
Last update: 2015.
Table of Contents