To check your SQL Server version using the command line, you can rely on built-in methods that return precise and reliable information without requiring additional tools. This approach is ideal for administrators, developers, or anyone needing quick version confirmation before scripting, troubleshooting, or planning upgrades. **You’ll only need administrative access to run these commands on the SQL Server machine.**

What You'll Need

  • A computer running Microsoft Windows (Windows Server or Windows 10/11 with SQL Server installed).
  • Administrative or dedicated login rights to query SQL Server’s metadata.
  • Access to a command prompt (CMD) or PowerShell prompt on the server where SQL Server is installed.
  • No additional software—all methods work with the default SQL Server tools.

Step-by-Step

  1. Open Command Prompt as Administrator

    Press Win + X, then select Command Prompt (Admin) or Windows Terminal (Admin). If prompted, confirm elevation. Note: These steps assume SQL Server is installed and registered on this machine. If you’re on a remote server, connect via RDP or SSH with admin rights.
  2. Method 1: Use `sqlcmd` with the Server Version Query (Recommended)

    Run this command in the CMD prompt: sqlcmd -S .\SQLEXPRESS -Q "SELECT @@VERSION AS SQLServerVersion" Replace \SQLEXPRESS with your instance name (e.g., .\MySQLInstance or the default \ for Named Pipes). Alternative for older SQL Server versions: sqlcmd -S .\ -Q "SELECT name, version FROM sys.databases" This will show the server name and version in the output.
  3. Method 2: Query System Catalogs Directly

    Use PowerShell or CMD to run this SQL query: SELECT @@VERSION AS SQLServerVersion Paste it into CMD and press Enter. If you get a prompt, type sqlcmd (for SQL Server 2016+ with native CLI) or use the sqlserver command (Windows Server 2019+). For older versions: Try: SELECT SERVERPROPERTY('ProductVersion') AS SQLServerVersion
  4. Method 3: Check SQL Server Error Logs (Fallback)

    If neither method works, inspect the SQL Server error log: sqlcmd -S .\SQLEXPRESS -d master -Q "SELECT name FROM sys.dm_server_error_logs WHERE start_time > DATEADD(month, -1, GETDATE())" Look for entries under EventData that include ProductVersion or Version.

Common Mistakes

  • **Using the wrong instance name**: If your SQL Server is configured with an instance name like SQLEXPRESS, ensure you replace .\ with .\SQLEXPRESS. Defaults to .\ for Named Pipes.
  • **Permission issues**: If you get "You do not have permission to execute this query," run the command prompt as Administrator or use a dedicated SQL Server service account.
  • **Typo in query syntax**: Some older versions require exact syntax like SELECT @@VERSION (no quotes). Test with a simple query like SELECT @@VERSION first.
  • **Remote server misconfiguration**: If you’re querying a remote instance, ensure the server’s firewall allows connections and the account has proper permissions.

Tips

  • **For quick checks**: Use PowerShell’s Get-WindowsFeature to confirm SQL Server is installed: Get-WindowsFeature Microsoft-SQLServer This won’t show the version but confirms the feature is active.
  • **Version formats**: SQL Server versions follow this pattern: 15.0.2000.5 (X64) = SQL Server 2019 (2000 = CU 5). 14.0.3000.10 (X86) = SQL Server 2017 (3000 = CU 10).
  • **Scripting-friendly**: Save the query as a batch file and run it repeatedly. Example: sqlcmd -S .\ -Q "SELECT @@VERSION" > version.txt
  • **Check all instances**: To list all SQL Server instances on the machine: Get-WmiObject -Namespace "root\Microsoft\Windows\SQLServer" -Class MSFT_SqlInstance (Requires PowerShell 5.1+ and SQL Server PowerShell module.)

Troubleshooting

  • **Command prompt errors**: If you see "Login failed for user", verify the SQL Server service is running. Open Services (via Win + R > services.msc) and ensure SQL Server (MSSQLSERVER) or your instance is started.
  • **No response from query**: Test connectivity first with a basic query: sqlcmd -S .\ -Q "SELECT 1" If this fails, the issue is network/permissions, not the query.
  • **Output appears blank**: Try capturing the output to a file: sqlcmd -S .\ -Q "SELECT @@VERSION" > version.log Check the log file for any errors.
  • **Running on a Linux/macOS system**: Use the sqlcmd tool from Microsoft’s client tools (install via choco install sqlcmd or check [downloads.microsoft.com](https://docs.microsoft.com/en-us/sql/connect/odbc/download-odbc-driver-for-sql-server)).
  • **SQL Server Express edition**: For Express, the version query works the same way, but some older tools may require additional configuration (like enabling OLE DB).

Frequently Asked Questions

1. Why does my SQL Server version show as "unknown" or "0.0"?

This typically means either:

  • SQL Server isn’t installed on the machine, or
  • The service is stopped, or
  • The client tool (sqlcmd) isn’t properly configured for the instance.

Check the SQL Server service status and ensure the client has network access to the instance. For Express, you may need to enable OLE DB connectivity in Windows Features.

2. How do I check the version of a remote SQL Server instance?

Use the full instance name with the server name, e.g.: sqlcmd -S mydomain\myinstance -Q "SELECT @@VERSION"

If the instance is on a different subnet, ensure the firewall allows inbound connections (port 1433 by default). Use SQL Server Authentication (not Windows Auth) if needed.

3. Can I use this to check SQL Server on Linux?

Yes, but you need to install Microsoft’s SQL Server client tools. On Debian/Ubuntu, run: curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add - curl https://packages.microsoft.com/config/debian/10/prod.list > /etc/apt/sources.list.d/mssql-release.list apt-get update && apt-get install msodbcsql18 mssql-tools

Then use sqlcmd as shown above.

4. What if I get a "Syntax error" in the query?

The error likely means you’re using an unsupported syntax. For older versions (2012), try: sqlcmd -S .\ -Q "SELECT SERVERPROPERTY('ProductVersion')"

For newer versions, @@VERSION is the most reliable method. Always test with a simple query first.

5. How do I compare my SQL Server version to the latest patch?

Check Microsoft’s release notes for your version of SQL Server. Compare the version number (e.g., 15.0.2000.5) to the latest CU (Cumulative Update) listed there. For example, 2000 means CU 5 is installed.

6. Should I upgrade if my version is outdated?

Depends on your needs:

  • SQL Server 2012 or earlier: Consider upgrading for security and feature support (end of life for 2012 is 2023).
  • SQL Server 2019 (15.x): Still supported long-term, but check for critical fixes in the latest CU.
  • SQL Server 2022 (16.x): Newer features like AI/ML and enhanced performance.

For production systems, always test upgrades in a staging environment first.