GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • SS64
  • SQL Server
  • How-to

This view

  • Original page
  • New address

GeezerVison is showing ss64.com — SQLCMD - SQL Server

  • SS64
  • SQL Server
  • How-to

SQLCMD

Run a T-SQL statement, procedure or script at the command prompt. (SQL 2016/Azure).

Syntax
      sqlcmd options

Options
   -a packet_size  
   -A  (dedicated administrator connection)  
   -b  (terminate batch job if there is an error)  
   -c batch_terminator
   -C  (trust the server certificate)  
   -d db_name
   -e  (echo input)  
   -E  (use trusted connection)  
   -fcodepage | i:codepage[,o:codepage] | o:codepage[,i:codepage]  
   -g (enable column encryption)
   -G (use Azure Active Directory for authentication)
   -h rows_per_header
   -H workstation_name
   -i input_file
   -I  (enable quoted identifiers)
   -j (Print raw error messages)
   -k[1 | 2] (remove or replace control characters)  
   -K application_intent
   -l login_timeout
   -L[c]  (list servers, optional clean output)  
   -m error_level
   -M multisubnet_failover
   -N  (encrypt connection)  
   -o output_file
   -p[1] (print statistics, optional colon format)  
   -P password
   -q "cmdline query"
   -Q "cmdline query" (and exit)  
   -r[0 | 1] (msgs to STDERR)  
   -R (use client regional settings)  
   -s col_separator
   -S [protocol:]server[\instance_name][,port]  
   -t query_timeout
   -u  (unicode output file)  
   -U login_id
   -v var = "value"
   -V error_severity_level
   -w column_width
   -W  (remove trailing spaces)  
   -x  (disable variable substitution)  
   -X[1] (disable commands, startup script, environment variables and optional exit)  
   -y variable_length_type_display_width
   -Y fixed_length_type_display_width
   -z new_password
   -Z new_password (and exit)
   -?  (usage)

Best Practice

  • Use integrated security.
  • Use -X in automated environments.
  • Secure input and output files by using appropriate NTFS file system permissions.
  • To increase performance, do as much in one sqlcmd session as you can, instead of in a series of sessions.
  • Set time-out values for batch or query execution higher than you expect it will take to execute the batch or query.

Add quotes around any fields containing strings that are likely to need them, and also escape any existing quotes within the data:

SELECT [Field1],
'"' + REPLACE([Field2], '"', '""') + '"' AS Field2, -- Enclose in quotes and escape existing quotes
[Field3]
FROM [YourTable]

Examples

Run the script DemoScript.sql using integrated security:

C:\> sqlcmd -S Server64\instance2 -i C:\demo\DemoScript.sql

Run the script DemoScript.sql and output to a file:

C:\> sqlcmd -S Server64\instance2 -i C:\demo\DemoScript.sql -o C:\demo\output.txt

“When debugging, novices insert corrective code; experts remove defective code” ~ Richard Pattis

Related commands

Invoke-Sqlcmd - Run an SQL script (PowerShell/SQLCMD)


 
Copyright © 1999-2026 SS64.com
Some rights reserved
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.