Command line utilities for SQL Server 18 enable efficient database administration, scripting, and automation without requiring a graphical interface, using tools like sqlcmd, bcp, and PowerShell modules directly from the terminal.
What You'll Need
- SQL Server 18 (or later) installed on Windows, Linux, or in a container
- SQL Server command-line tools package (mssql-tools18) installed
- Access to a terminal or command prompt with administrative or sufficient user privileges
- Network access to the target SQL Server instance
- Valid SQL Server authentication credentials (SQL login or Windows integrated security)
Step-by-Step
- Install the SQL Server 18 command-line tools by downloading the mssql-tools18 package from Microsoft’s official repository for your platform (Windows MSI, Linux .deb/.rpm, or macOS via brew).
- After installation, add the tools to your system PATH if not done automatically—typically located in /opt/mssql-tools18/bin/ on Linux or C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\17\Tools\Binn\ on Windows.
- Open a new terminal or command prompt to ensure the updated PATH is loaded.
- Test connectivity using sqlcmd: run
sqlcmd -S your_server_name -U your_username -P your_password -Q "SELECT @@VERSION"replacing placeholders with your server, credentials, and using -E for trusted connection if applicable. - If connecting to a named instance, specify the instance name:
sqlcmd -S servername\instancename -U user -P password. - To run a script file, use:
sqlcmd -S server -U user -P password -i C:\scripts\setup.sqlon Windows orsqlcmd -S server -U user -P password -i /home/user/setup.sqlon Linux. - For exporting data to a file, use bcp:
bcp "SELECT * FROM Database.dbo.Table" queryout C:\data\export.csv -c -t, -T -S servername(uses trusted connection; replace -T with -U user -P password for SQL auth). - To import data from a file, use:
bcp Database.dbo.Table in C:\data\import.csv -c -t, -T -S servername. - Use PowerShell with the SqlServer module: install it via
Install-Module -Name SqlServer(requires PowerShell 5.1+), then connect with$cred = Get-Credential; Invoke-Sqlcmd -ServerInstance servername -Username $cred.UserName -Password $cred.GetNetworkCredential().Password -Query "SELECT * FROM Table". - Automate tasks by saving sqlcmd or bcp commands in .bat (Windows) or .sh (Linux) scripts and schedule them using Task Scheduler or cron.
- Always validate output files and check error levels—sqlcmd and bcp return errorlevel 1 on failure, which can be checked in scripts.
Common Mistakes
- Forgetting to escape special characters in passwords on the command line, especially in Linux shells where $, !, or & have special meaning—use single quotes around the password or avoid -P and let sqlcmd prompt securely.
- Assuming the tools are in PATH after installation without verifying—some installers require a restart or manual PATH update.
- Using the wrong ODBC driver version—SQL Server 18 requires ODBC Driver 17 or 18; using an older driver causes connection failures.
- Not specifying the correct protocol—if TCP/IP is disabled in SQL Server Configuration Manager, named pipes or shared memory may be required, altering the -S syntax.
- Misunderstanding bcp’s format flags—using -c for character data is safe for most cases, but -w (Unicode) or native format (-n) may be needed for binary fidelity.
- Running bcp without a format file or query and expecting correct column mapping—always verify the source and destination structure match.
Tips
- Use sqlcmd’s -b option to batch abort on error, useful in scripts where you want to stop execution if a statement fails.
- Leverage sqlcmd variables with -v:
sqlcmd -v MyDB="Sales" -i script.sqlallows reuse of scripts across environments. - For secure password handling, omit -P and let sqlcmd prompt for input—this avoids exposing credentials in process lists or shell history.
- Use bcp with a format file (-f) for complex imports/exports to precisely control data types and column order.
- Combine sqlcmd with PowerShell for advanced logging: pipe output to
Out-FileorTee-Objectfor console and file logging. - When working in containers, use the container’s hostname or IP and ensure port 1433 is mapped—test with
telnet localhost 1433ornc -zv localhost 1433. - Regularly update mssql-tools18 to get security patches and compatibility fixes—Microsoft releases updates quarterly.
Troubleshooting
- If you get "Login failed" errors, verify credentials, ensure SQL Server authentication is enabled (if using SQL logins), and check that the account has connect permissions.
- For "Named Pipes Provider: Could not open a connection" errors, confirm the server name, instance name, and that the SQL Server Browser service is running (for named instances) or that you’re using the correct port.
- If bcp exports empty files, check your query syntax—especially if using multi-line statements—and ensure you’re querying the correct database (use USE db or fully qualified names).
- When scripts fail silently, always check %ERRORLEVEL% (Windows) or $? (Linux) after sqlcmd or bcp runs.
- For ODBC connection errors, run
odbcinst -q -d -n "ODBC Driver 17 for SQL Server"to verify the driver is installed correctly. - On Linux, if you see "libssl.so.1.1 not found", install compatibility packages like libssl1.1—newer distros may ship libssl3, requiring legacy symlinks.
- Use SQL Server Profiler or Extended Events to monitor incoming connections from command-line tools if authentication or connectivity issues persist.
Frequently Asked Questions
Can I use sqlcmd and bcp with Azure SQL Database?
Yes, both tools work with Azure SQL Database and Azure Synapse—use the server name in the format servername.database.windows.net and ensure your IP is allowed in the firewall.
Do I need to install SQL Server itself to use the command-line tools?
Is there a difference between sqlcmd and the Invoke-Sqlcmd PowerShell cmdlet?
Yes—sqlcmd is a native C++ utility with lower overhead, while Invoke-Sqlcmd is a PowerShell cmdlet that depends on the SqlServer module and offers richer object output but higher resource usage.
How do I encrypt the connection using sqlcmd?
Use the -N option to encrypt the connection (e.g., sqlcmd -S server -U user -P password -N)—this requires the server to trust the certificate or have a valid chain.
Can I use Windows Authentication from Linux?
Only if the Linux machine is domain-joined to Active Directory and you use Kerberos—otherwise, use SQL Server authentication or integrate with Azure AD via interactive login (not supported in bcp).
What’s the maximum file size bcp can handle?
There is no practical limit—bcp streams data and is only constrained by available disk space and system resources, making it suitable for large-scale exports.