To open SQL Server using a command, run sqlcmd -S localhost in Command Prompt or PowerShell to connect to the default instance, or specify a server and instance name like sqlcmd -S SERVERNAME\INSTANCENAME to connect to a named instance. This launches the sqlcmd utility, allowing you to execute T-SQL commands directly from the command line.

What You'll Need

  • A Windows machine with SQL Server installed
  • The SQL Server command-line tools (sqlcmd) installed and accessible in the system PATH
  • Appropriate permissions to connect to the SQL Server instance
  • Command Prompt or PowerShell with administrative privileges (if connecting via Windows Authentication and UAC is enabled)

Step-by-Step

  1. Open Command Prompt or PowerShell: Press Win + R, type cmd or powershell, and press Enter.
  2. Verify sqlcmd is available: Type sqlcmd -? and press Enter. If you see usage information, the tool is installed and in your PATH. If not, install the SQL Server Command Line Utilities from the Microsoft Download Center or via the SQL Server Installation Center.
  3. Determine your server and instance name: If connecting to the default instance on the local machine, use localhost or a period (.). For a named instance, use SERVERNAME\INSTANCENAME (e.g., MYPC\SQLEXPRESS). To find installed instances, open SQL Server Configuration Manager and check SQL Server Services.
  4. Choose authentication method:
    • For Windows Authentication (recommended for local connections): Use sqlcmd -S SERVERNAME\INSTANCENAME -E
    • For SQL Server Authentication: Use sqlcmd -S SERVERNAME\INSTANCENAME -U username -P password
  5. Run the connection command: For example, to connect to the default instance using Windows Authentication, type sqlcmd -S localhost -E and press Enter.
  6. Upon successful connection, you will see a prompt like 1>, indicating you are now in the sqlcmd environment and can run T-SQL commands.
  7. To exit sqlcmd and return to the command line, type EXIT and press Enter.

Common Mistakes

  • Assuming sqlcmd is installed by default: Many SQL Server installations (especially Express) do not include the command-line tools unless explicitly selected during setup.
  • Using the wrong server name: Forgetting the \ separator for named instances or using the computer name incorrectly.
  • Incorrect authentication: Trying to use SQL Server Authentication when the instance is configured for Windows Authentication only, or vice versa.
  • Network issues: Attempting to connect to a remote server without enabling TCP/IP in SQL Server Configuration Manager or without opening firewall ports (default 1433).
  • Case sensitivity in passwords: SQL Server passwords are case-sensitive; ensure Caps Lock is off.

Tips

  • Use . as the server name to connect to the default instance on the local machine: sqlcmd -S . -E
  • Save frequently used connections in a batch file: Create a .bat file with your sqlcmd command for quick access.
  • Use trusted connections (-E) whenever possible for better security and to avoid storing passwords in scripts.
  • To run a script file, use sqlcmd -S SERVERNAME -i C:\script.sql
  • Enable TCP/IP protocol in SQL Server Configuration Manager if connecting remotely.
  • Test connectivity with sqlcmd -S SERVERNAME -Q "SELECT @@VERSION" to run a query and exit immediately.

Troubleshooting

  • 'sqlcmd' is not recognized: Install the Microsoft Command Line Utilities for SQL Server. After installation, restart the command prompt to refresh the PATH.
  • Connection refused or network error:
    • Ensure the SQL Server service is running (check in Services.msc or SQL Server Configuration Manager).
    • Verify TCP/IP is enabled under SQL Server Network Configuration.
    • Check Windows Firewall: Allow inbound connections on port 1433 (or your configured port).
    • Confirm you are using the correct server name and instance.
  • Login failed for user:
    • Double-check username and password (case-sensitive).
    • Ensure the login exists and has permissions to connect.
    • If using Windows Authentication, verify your domain account is authorized.
    • Check SQL Server authentication mode: Right-click the server in SSMS > Properties > Security > ensure it’s set to Windows and SQL Server Authentication if needed.
  • Timeout expired:
    • Increase login timeout with -l 30 (30 seconds).
    • Check network latency or server load.
    • Verify the server is not paused or offline.
  • Still stuck: Try connecting via SQL Server Management Studio (SSMS) first to confirm the server is accessible. If SSMS works but sqlcmd doesn’t, recheck your command syntax and tool installation.

Frequently Asked Questions

Can I use sqlcmd on Linux or macOS?

Yes, Microsoft provides a version of sqlcmd for Linux and macOS as part of the mssql-tools package. Install it via the official Microsoft repository and use the same syntax to connect to SQL Server instances on any platform.

Do I need to install SQL Server to use sqlcmd?

No, you only need to install the SQL Server Command Line Utilities (which include sqlcmd). These can be installed independently of the database engine.

What’s the difference between sqlcmd and SSMS?

sqlcmd is a lightweight, scriptable command-line tool for executing T-SQL. SSMS is a full graphical IDE with visual designers, query editors, and management tools. Use sqlcmd for automation and SSMS for interactive development and administration.

Can I run sqlcmd commands in a PowerShell script?

Yes, you can call sqlcmd from within PowerShell scripts using the same syntax. Capture output using standard redirection (>) or invoke-expression if needed.

Is it safe to include passwords in sqlcmd commands?

Including passwords in plain text (-P password) is insecure as they may appear in process lists or shell history. For better security, use Windows Authentication (-E) or prompt for input securely in scripts.

What port does sqlcmd use by default?

sqlcmd uses the same protocols as other SQL Server clients. By default, it connects via TCP/IP on port 1433 for the default instance. Named instances use dynamic ports unless configured otherwise—check SQL Server Configuration Manager for the exact port.