Skip to content

Instantly share code, notes, and snippets.

@BDollar
Created April 10, 2013 16:20
Show Gist options
  • Save BDollar/5356115 to your computer and use it in GitHub Desktop.
Save BDollar/5356115 to your computer and use it in GitHub Desktop.
Microsoft (R) SQL Server Command Line Tool - Use the command line to issue SQL commands
sqlcmd -S myServer
-D myDB
-E
-O "MyData.txt" ^
-Q "select bar from foo" ^
-W 999
-S ","
sqlcmd /?
Microsoft (R) SQL Server Command Line Tool
Version 10.0.5500.0 NT x64
usage: Sqlcmd [-U login id] [-P password]
[-S server] [-H hostname] [-E trusted connection]
[-d use database name] [-l login timeout] [-t query timeout]
[-h headers] [-s colseparator] [-w screen width]
[-a packetsize] [-e echo input] [-I Enable Quoted Identifiers]
[-c cmdend] [-L[c] list servers[clean output]]
[-q "cmdline query"] [-Q "cmdline query" and exit]
[-m errorlevel] [-V severitylevel] [-W remove trailing spaces]
[-u unicode output] [-r[0|1] msgs to stderr]
[-i inputfile] [-o outputfile] [-z new password]
[-f <codepage> | i:<codepage>[,o:<codepage>]] [-Z new password and exit]
[-k[1|2] remove[replace] control characters]
[-y variable length type display width]
[-Y fixed length type display width]
[-p[1] print statistics[colon format]]
[-R use client regional setting]
[-b On error batch abort]
[-v var = "value"...] [-A dedicated admin connection]
[-X[1] disable commands, startup script, enviroment variables [and exit]]
[-x disable variable substitution]
[-? show syntax summary]
Also see the MSDN sqlcmd reference. It puts the /? option's output to shame.
Example: sqlcmd -S MyServer -d myDB -E -Q "select col1, col2, col3 from SomeTable" -o "MyData.csv" -h-1 -s"," -w 700
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment