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 [2]
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

 About This Book

 JDBC (Java Database Connectivity) Introduction

 JDK (Java SE) Installation

 Installing and Running Java DB - Derby

 Derby (Java DB) JDBC Driver

 Derby (Java DB) JDBC DataSource Objects

 Java DB (Derby) - DML Statements

 Java DB (Derby) - ResultSet Objects of Queries

 Java DB (Derby) - PreparedStatement

 MySQL Installation on Windows

 MySQL JDBC Driver (MySQL Connector/J)

 MySQL - PreparedStatement

 MySQL - Reference Implementation of JdbcRowSet

 MySQL - JBDC CallableStatement

 MySQL CLOB (Character Large Object) - TEXT

 MySQL BLOB (Binary Large Object) - BLOB

 Oracle Express Edition Installation on Windows

 Oracle JDBC Drivers

 Oracle - Reference Implementation of JdbcRowSet

 Oracle - PreparedStatement

 Oracle - JBDC CallableStatement

 Oracle CLOB (Character Large Object) - TEXT

 Oracle BLOB (Binary Large Object) - BLOB

Microsoft SQL Server 2005 Express Edition

 Downloading Microsoft SQL Server 2014 Express Edition

 Installing Microsoft SQL Server 2014 Express Edition

SQLCMD SQL Server Command Line Tool

 Installing AdventureWorksLT Sample Database

 Microsoft JDBC Driver for SQL Server - sqljdbc42.jar

 Microsoft JDBC Driver - Query Statements and Result Sets

 Microsoft JDBC Driver - DatabaseMetaData Object

 Microsoft JDBC Driver - DDL Statements

 Microsoft JDBC Driver - DML Statements

 SQL Server - PreparedStatement

 SQL Server CLOB (Character Large Object) - TEXT

 SQL Server BLOB (Binary Large Object) - BLOB

 JDBC-ODBC Bridge Driver - sun.jdbc.odbc.JdbcOdbcDriver

 JDBC-ODBC Bridge Driver - Flat Text Files

 JDBC-ODBC Bridge Driver - MS Access

 JDBC-ODBC Bridge Driver - MS SQL Server

 Summary of JDBC Drivers and Database Servers

 Additional Tutorial Notes to Be Added

 Outdated Tutorials

 References

 PDF Printing Version