You can manage databases directly from the Windows command prompt using the SQL Server command-line tools (sqlcmd and bcp). This approach is ideal for batch scripts, automation, and running commands on servers without a graphical interface. To begin, you must have SQL Server installed on a Windows machine and the SQL Server Client Tools installed, which are usually part of the "SQL Server Management Tools" or "Client Tools Connectivity" packages during installation.
What You'll Need
- Windows-based computer with SQL Server installed (Express, Standard, or Enterprise).
- SQL Server Client Tools installed (specifically the sqlcmd utility).
- Administrative privileges on the server to execute commands.
- A database name, username, and password to connect to your SQL Server instance.
Step-by-Step
- Open the Command Prompt. Press the Windows key, type
cmd, and select Command Prompt or Windows PowerShell from the results. It is recommended to use Command Prompt for basic sqlcmd usage. - Verify sqlcmd is available. Type
sqlcmd -?and press Enter. If the tool is installed correctly, a help screen detailing available options will appear. If the command is not recognized, you may need to add the SQL Server tools directory to your system's PATH environment variable or run the command from the specific folder where sqlcmd.exe is located (usuallyC:\Program Files\Microsoft SQL Server\Client Tools\Binn). - Connect to a local instance. To connect to the default SQL Server instance on the local machine, use the following command:
sqlcmd -S .Press Enter. You should see a prompt indicating you are now connected, typically1>. - Connect to a named instance. If SQL Server is installed as a named instance (e.g.,
SQLExpress), use the backslash notation:sqlcmd -S .\SQLExpress. ReplaceSQLExpresswith your actual instance name. - Connect using authentication. By default, sqlcmd attempts to connect using Windows Integrated Authentication, which uses your current Windows user credentials. To connect using a specific SQL Server login, add the
-U(username) and-P(password) flags:sqlcmd -S . -U sa -P YourStrongPassword123. Note that passwords are not echoed on the screen for security. - Execute a simple query. Once connected, you can run Transact-SQL (T-SQL) commands. Type
SELECT @@VERSION;and press Enter. The server version information will be returned. To exit the query editor, typeGOand press Enter. - Run a batch of commands. You can write multiple lines of T-SQL without executing them until you type
GO. For example:USE YourDatabaseName; GO SELECT Name FROM sys.databases; GO
This tells the server to execute the batch starting from the previous line up to theGOkeyword. - Execute a query and output to a file. To save the results of a query to a text file, use the
-oflag. For instance, to list all tables in a database and save them totables.txt, run:sqlcmd -S . -d YourDatabaseName -Q "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE';" -o tables.txt. The-Qflag executes the query and quits immediately. - Script a database backup. A common use case is creating a backup script. The following command creates a full backup of a database to a file on the server (not the client):
BACKUP DATABASE YourDatabaseName TO DISK = 'C:\Backups\YourDatabaseName.bak' WITH FORMAT, MEDIANAME = 'SQLServerBackups', NAME = 'Full Backup of YourDatabaseName'; GO
You can then execute this script directly from the command line by typing the filename:sqlcmd -S . -i C:\Scripts\backup_script.sql. This reads and executes commands from the file. - Import data using bcp. To bulk copy data from a flat file (CSV, TXT) into a SQL Server table, use the Bulk Copy Program (bcp) utility. The syntax is
bcp YourDatabaseName.dbo.YourTableName IN data_file.csv -S . -T -c -t ,. Here,-Tuses trusted connection,-cspecifies character mode (no data types), and-t ,sets the comma as the field terminator.
Common Mistakes
A frequent issue is attempting to connect to a remote server without specifying the port number. If the default port (1433) is not used, or if the SQL Server Browser service is not running, the connection will fail. Use the -L flag to list local SQL Server instances: sqlcmd -L. Another common error is forgetting to use GO when running a script; without it, only the first line of the batch is sent to the server to be parsed, while subsequent lines are treated as new commands and rejected.
Tips
For faster command entry, use tab completion. If you have a long server name or username, type the first few letters and press Tab to cycle through matching options. When scripting, always include the database name (using -d) to ensure you are querying the correct target, especially if multiple databases exist. Note that command-line tools are case-sensitive regarding the SQL Server instance name (e.g., MyInstance is different from myinstance).
Troubleshooting
If you receive an error regarding the sqlcmd: not recognized or command not found, the issue is almost certainly that the SQL Server Client Tools folder is not in your system PATH environment variable. To fix this, search for "Environment Variables" in Windows settings, edit the Path variable for your user or system, and add the path to the Binn folder of your SQL Server installation.
If you see the error Msg 2, Level 16, State 1: Named Pipes Provider: Could not open a connection to SQL Server [2]., this usually indicates a firewall blocking the connection. Ensure that the SQL Server service is running and that Windows Firewall or a third-party firewall allows traffic on port 1433. If you are connecting to a named instance that is not the default, the SQL Server Browser service must be running and accessible, or you must specify the port (e.g., -S .,1434).
If the GO command results in Msg 102, Level 15, State 1: Incorrect syntax near 'GO'., it means the server does not support T-SQL batch separators. This typically happens if you are connecting to a system that only supports SQL dialects without the GO batch command, or if the connection type is misconfigured.
Frequently Asked Questions
How do I exit sqlcmd?
Simply type EXIT and press Enter. Alternatively, you can type QUIT or press Ctrl + C.
Can I use sqlcmd on Linux?
Yes, sqlcmd is available for Linux and can be installed via package managers like apt-get on Ubuntu/Debian or yum on RHEL/CentOS. The syntax and functionality are nearly identical to the Windows version.
What is the difference between -Q and -q?
The -Q flag executes the query and then immediately quits sqlcmd, but it does not return the results to the console. The -q flag executes the query but keeps the sqlcmd session open so you can run more commands afterward.
Why does my password appear as asterisks when I type it?
This is a security feature of the command prompt. sqlcmd does not echo the password to the screen for any user to see, including yourself. Ensure you type it correctly, as there is no visual feedback.
How do I connect to a SQL Server running in Azure?
You must specify the server's fully qualified domain name (FQDN) or the Azure resource name, usually in the format tcp:myserver.database.windows.net,1433. You will also need to use the -U and -P flags with the SQL Server login and its corresponding password, as Azure SQL does not support Windows Integrated Authentication from outside the Azure network.