SQL Express command line tools let you run queries, manage databases, and move data without a graphical interface—ideal for automation, scripting, or headless servers. You only need SQL Server Express installed with the command-line utilities included.

What You'll Need

  • SQL Server Express (2019 or later) installed with the 'Management Tools - Basic' feature
  • Access to a Windows command prompt (cmd.exe) or PowerShell
  • Network access to the SQL Server instance (if connecting remotely)
  • Appropriate login credentials (Windows Authentication or SQL Server login)

Step-by-Step

  1. Open Command Prompt: Press Win + R, type cmd, and hit Enter.
  2. Locate the tools: By default, sqlcmd and bcp are installed in C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\170\Tools\Binn\ (path may vary by version). Add this folder to your PATH environment variable if not already present, or navigate to it directly using cd.
  3. Test connectivity: Run sqlcmd -S .\SQLEXPRESS -E to connect to the local named instance using Windows Authentication. You should see a 1> prompt.
  4. Run a simple query: At the 1> prompt, type SELECT @@VERSION; and press Enter, then type GO on a new line and press Enter again to execute. The SQL Server version will return.
  5. Execute a script file: Save a query to C:\temp\test.sql (e.g., SELECT name FROM sys.databases;), then run sqlcmd -S .\SQLEXPRESS -i C:\temp\test.sql to output results to the console.
  6. Export data to a file: Use bcp to copy table data. For example, bcp "SELECT name, create_date FROM sys.databases" queryout C:\temp\databases.csv -c -t, -T -S .\SQLEXPRESS exports to a comma-separated file with trusted connection.
  7. Import data from a file: Prepare a format file or use native format. To import a CSV into a table, first ensure the table exists, then run bcp MyDatabase.dbo.MyTable in C:\temp\data.csv -c -t, -T -S .\SQLEXPRESS.
  8. Disconnect: Type EXIT at the sqlcmd prompt or close the command window.

Common Mistakes

  • Forgetting the GO terminator after a batch in sqlcmd—commands won’t execute until GO is entered on its own line.
  • Using the wrong server name: For named instances, use ServerName\InstanceName (e.g., localhost\SQLEXPRESS), not just the server name.
  • Assuming bcp uses the same delimiters as other tools—-c specifies character mode, but you must explicitly set field (-t) and row (-r) terminators if not using defaults.
  • Running bcp without proper permissions: The Windows account must have read/write access to the file system path and appropriate database permissions (e.g., ADMINISTER BULK OPERATIONS).
  • Overlooking that sqlcmd and bcp are separate utilities—sqlcmd runs T-SQL; bcp moves data. Confusing their syntax leads to errors.

Tips

  • Use -E for trusted connection to avoid managing passwords in scripts.
  • In PowerShell, you can invoke sqlcmd directly and capture output: & sqlcmd -S .\SQLEXPRESS -Q "SELECT DB_NAME()".
  • For reusable scripts, use sqlcmd variables: sqlcmd -v MyDB="MyDatabase" -i script.sql where script.sql contains USE $(MyDB);.
  • When troubleshooting connectivity, test with sqlcmd -S .\SQLEXPRESS -E -Q "SELECT 1"—a simple query isolates login/network issues.
  • Export large datasets with -n (native format) for speed, then import with the same format—avoids parsing overhead.
  • Always verify file paths are accessible from the account running the command—especially important in scheduled tasks or services.

Troubleshooting

  • ‘sqlcmd’ is not recognized: The tools aren’t in your PATH. Re-run the SQL Server Installation Center and add ‘Management Tools - Basic’, or manually add the Binn folder to PATH.
  • Login failed for user: Verify the instance name (-S), authentication mode (-E for Windows, -U and -P for SQL), and that the account has permissions.
  • Connection timeout: Ensure the SQL Server Browser service is running (for named instances), TCP/IP is enabled in SQL Server Configuration Manager, and firewall ports (default 1433) are open.
  • bcp error: Unable to open BCP host data-file: Check file path spelling, permissions, and that you’re running the command from an account with access to the location.
  • Incorrect syntax near 'GO': You placed GO inside a comment or string, or on a line with other text. GO must be alone on a line.
  • Data truncation in bcp output: Increase field width with -w (Unicode) or use native format -n to preserve precision.

Frequently Asked Questions

Do I need to install SQL Server Express to use sqlcmd and bcp?

Yes, the command-line tools are part of the SQL Server Express installation. They are not available as a standalone download.

Can I use sqlcmd and bcp on Linux?

No, these specific utilities are Windows-only. On Linux, use the mssql-tools package which provides sqlcmd and bcp equivalents.

What’s the difference between sqlcmd and osql?

osql is deprecated and removed in newer versions. sqlcmd is the current utility and supports all modern features.

Do I need to enable anything in SQL Server Express to allow remote connections for these tools?

Yes. In SQL Server Configuration Manager, enable TCP/IP under Protocols for SQLEXPRESS, and ensure the SQL Server Browser service is running if using named instances.

Can I use sqlcmd to run PowerShell commands?

No. sqlcmd only executes Transact-SQL. To run PowerShell, use PowerShell directly or invoke it from a batch file.

Is it safe to store passwords in bcp or sqlcmd command lines?

No. Passwords visible in command lines can be seen in process lists and command history. Use trusted authentication (-E) or secure credential management instead.