The sqlcmd utility lets you enter Transact-SQL statements, system procedures, and script files through a variety of available modes:

  • At the command prompt.
  • In Query Editor in SQLCMD mode.
  • In a Windows script file.
  • In an operating system (Cmd.exe) job step of a SQL Server Agent job.

The utility uses ODBC to execute Transact-SQL batches.

Run sql from command line.

ECHO Running script01.sql...
cmd /c sqlcmd -b -i script01.sql -S localhost -d master >script01.log
IF [%ERRORLEVEL%] NEQ [0] GOTO ERROR

Note:

SQLCMD is installed by default if you install SQL Server. If SQL Server is not installed, you will need to install C++ redistributor, ODBC, and SQL Util for SQLCMD.

Syntax

-b (terminate batch job if there is an error)

-i input_file

-S [protocol:]server[instance_name][,port]

-d db_name

Sources:

https://docs.microsoft.com/en-us/sql/tools/sqlcmd-utility?view=sql-server-ver15

Last modified: August 31, 2021

Author

Comments

Write a Reply or Comment