To use SQL Server Express from the command line, you use the sqlcmd utility. This tool allows you to connect to your database instance, run T-SQL queries, and execute script files without needing a graphical interface like SQL Server Management Studio (SSMS). If you have SQL Server Express installed, you likely already have the tools, but you may need to add the binary folder to your system environment variables to call the command from any terminal window.
What You'll Need
- A computer with SQL Server Express installed and running.
- The SQL Server Command Line Utilities (sqlcmd) installed. If these were not included in your initial installation, they can be downloaded separately from the official Microsoft site.
- Administrative privileges on the local machine to modify environment variables or start/stop services.
- The name of your SQL instance (the default for Express is usually
.\SQLEXPRESS).
Step-by-Step
- Verify the Installation of sqlcmd: Open a Command Prompt or PowerShell window. Type
sqlcmd -?and press Enter. If you see a list of options and switches, the tool is installed and available in your path. If you receive an error stating that the command is not recognized, you must locate thesqlcmd.exefile on your hard drive (typically found inC:\Program Files\Microsoft SQL Server\Client SDK\ODBC\or a similar path depending on the version) and add that folder to your System Environment Variables under the Path variable. - Identify Your Instance Name: SQL Server Express does not usually install as a default instance. It typically uses a named instance called SQLEXPRESS. To connect, you will use the server name
localhost\SQLEXPRESSor the shorthand.\SQLEXPRESS. The dot represents the local machine. - Connect Using Windows Authentication: The simplest way to connect if you are the administrator of the machine is via Windows Authentication. In your terminal, type the following command:
sqlcmd -S .\SQLEXPRESS -E. The-Sswitch specifies the server/instance, and the-Eswitch tells the utility to use a trusted connection (your current Windows login). - Connect Using SQL Server Authentication: If you have a specific username (like 'sa') and password, use the following syntax:
sqlcmd -S .\SQLEXPRESS -U sa -P your_password. Replaceyour_passwordwith your actual password. Note that SQL Server Authentication must be enabled in the server properties for this to work. - Entering the Interactive Mode: Once you press Enter on a successful connection command, the prompt will change to
1>. This indicates you are now in the interactive SQL session. You are no longer typing DOS or PowerShell commands; you are typing T-SQL (Transact-SQL). - Executing Your First Query: To run a command in interactive mode, you must type the query and then type
GOon a new line. For example, to see the version of your server, type:SELECT @@VERSION;GO
Press Enter. The server will process the request and return the result directly in the terminal. - Switching Databases: By default, you connect to the
masterdatabase. To switch to a specific user database, use theUSEcommand:USE MyDatabaseName;GO - Running a SQL Script File: If you have a large
.sqlfile and do not want to type every command manually, you can execute the file from the standard command prompt (not inside the interactive session). Use the-iswitch:sqlcmd -S .\SQLEXPRESS -E -i C:\scripts\setup.sql. This will run every command in the file and output the results to the screen. - Exporting Results to a File: To save the output of a query or script to a text file instead of displaying it on the screen, use the
-oswitch:sqlcmd -S .\SQLEXPRESS -E -Q "SELECT * FROM MyTable" -o C:\output\results.txt. The-Qswitch executes the query and exits immediately. - Exiting the Utility: To leave the interactive
sqlcmdsession and return to the standard command prompt, typeQUITorEXITand press Enter.
Common Mistakes
One of the most frequent errors is forgetting the GO command. In many SQL editors, you can simply highlight a line and click run. In sqlcmd, the utility buffers your input. It will not send the T-SQL commands to the server until it encounters the keyword GO. If your screen just shows 2>, 3>, and so on, without any output, it is because you haven't told the utility to execute the batch.
Another common mistake is confusing the server name. Users often try to connect using just sqlcmd -S localhost. Because SQL Server Express uses a named instance, the connection will fail unless you specify localhost\SQLEXPRESS. If you are unsure of the instance name, you can check the SQL Server Configuration Manager under "SQL Server Services" to see the exact name of the running instance.
Users also frequently encounter permission issues when exporting files. If you try to use the -o switch to save a file directly to the root of the C:\ drive, Windows may block the action due to security restrictions. Always specify a folder where your user account has write permissions, such as C:\Users\YourName\Documents\.
Tips
For those who frequently use the command line, creating a batch file (.bat) can save significant time. You can create a script that contains your connection string and common maintenance queries, allowing you to perform database health checks with a single double-click.
When working with large datasets, the default output format of sqlcmd can be messy because it uses fixed-width columns. To make the data more readable for other applications, use the -s switch to specify a column separator, such as a comma for CSV output: sqlcmd -S .\SQLEXPRESS -E -s "," -Q "SELECT * FROM Users".
If you are managing a remote SQL Server Express instance, ensure that the "SQL Server Browser" service is running on the host machine. Without the Browser service, the command line utility cannot resolve the named instance (SQLEXPRESS) to a port number, and the connection will time out.
Troubleshooting
If you receive a "Network-related or instance-specific error occurred while establishing a connection," follow these steps in order:
- Check the Service Status: Open the Windows Services manager (services.msc) and ensure that "SQL Server (SQLEXPRESS)" is currently in the "Running" state. If it is stopped, right-click and select Start.
- Verify the Instance Name: Double-check that you are using
.\SQLEXPRESS. If you renamed the instance during installation, you must use that specific name. - Enable TCP/IP: Open the SQL Server Configuration Manager. Navigate to SQL Server Network Configuration > Protocols for SQLEXPRESS. Ensure that "TCP/IP" is Enabled. If you change this setting, you must restart the SQL Server service for it to take effect.
- Firewall Settings: If connecting from a different machine, ensure that the Windows Firewall is allowing traffic through port 1433 (the default SQL port) and UDP port 1434 (for the SQL Browser service).
- Credential Check: If using
-Uand-P, ensure the account is not locked out and that the server is configured for "Mixed Mode Authentication." If the server is set to "Windows Authentication Only," thesaaccount will not work regardless of the password.
Frequently Asked Questions
Can I run SQL Server Express command line on Linux?
Yes, Microsoft provides a version of SQL Server for Linux. The sqlcmd utility is available as part of the mssql-tools package on distributions like Ubuntu and Red Hat.
What is the difference between -q and -Q?
The lowercase -q executes the query and keeps the session open in interactive mode. The uppercase -Q executes the query and immediately exits the utility, returning you to the system prompt.
How do I hide the column headers in the output?
You can use the -h -1 switch. The -h switch controls the column headers, and setting it to -1 removes them entirely from the results.
Can I use PowerShell instead of Command Prompt?
Yes, sqlcmd works in PowerShell. However, be aware that some characters (like backslashes in instance names) may need to be handled differently or wrapped in quotes to avoid being interpreted as PowerShell escape characters.
Is there a way to run multiple queries in one line?
Yes, you can separate queries with semicolons and include the GO command within the string, though it is often cleaner to use a -i script file for complex operations.