Questions tagged [sqlcmd]

SQLCMD is a command line tool used to run SQL batches on Microsoft SQL Server.

SQLCMD.EXE is a command line tool used to run SQL batches on Microsoft SQL Server. Get help by running SQLCMD -? and by searching for SQLCMD in the documentation for SQL Server Management Studio. SQLCMD is installed as a part of the Microsoft SQL Server client tools package, and completely supersedes OSQL.EXE.

In addition to command line flags, SQLCMD supports 18 special commands inside SQL files that are not a part of T-SQL syntax. SQLCMD commands aside from "GO" must be prefixed with a colon (:) and appear at the beginning of a line.

SQL Server Management Studio also supports these 18 commands if you select "SQLCMD Mode" from the "Query" menu. This disables IntelliSense.

Reference

SqlCmd on Microsoft TechNet

1169 questions
12
votes
3 answers

How to get SQLCMD to output errors and warnings only

How can you get SQLCMD, when executing a SQL script file, to just output any errors or warnings it encounters? I essentially dont want information based messages to be output.
bytedev
  • 8,252
  • 4
  • 48
  • 56
11
votes
3 answers

Proper way to detect if SQLCMD.exe is installed?

I am creating a class library that takes .SQL files as input (FileInfo) and a connection string. It then attempts to execute the sql file against the connection. I have decided to support Microsoft's SMO and SQLCMD.exe In testing, I have noticed…
Issa Fram
  • 2,556
  • 7
  • 33
  • 63
11
votes
2 answers

Can you make SQLCMD immediately run each statement in a script without the use of "GO"?

When running a script in SQLCMD, is there a setting, parameter or any other way of making it effectively run a GO command at the end of each SQL statement? This would prevent the need to insert periodic GO commands into very large scripts.
TheQuickBrownFox
  • 10,544
  • 1
  • 22
  • 35
11
votes
4 answers

Smarter way to use SQLCMD with dynamic calling

I have this MSSQL SQLCMD code, which can logon the database and in thisjust do an SELECT statement: :CONNECT czasql-001 SELECT * FROM [Lps_Hepper_Cz].[config].[LpsPlant] GO :CONNECT LS_Hepper_DK SELECT * FROM…
mortenstarck
  • 2,713
  • 8
  • 43
  • 76
11
votes
1 answer

Using variable in sql postdeployment build script?

What I would like to be able to do, is to be able to create a publising xml script in Visual Studio database project that defines a variable, that will be used to compose a script. My problem is that I get a Error: "SQL72008: Variable DeployType is…
Sturla
  • 3,446
  • 3
  • 39
  • 56
11
votes
4 answers

Using variables in SQLCMD for Linux

I'm running the Microsoft SQLCMD tool for Linux (CTP 11.0.1720.0) on a Linux box (Red Hat Enterprise Server 5.3 tikanga) with Korn shell. The tool is properly configured, and works in all cases except when using scripting variables. I have an SQL…
GPX
  • 3,506
  • 10
  • 52
  • 69
10
votes
2 answers

SQLCMD utility from BAT file - how to return ERRORLEVEL in case of syntax error

How can I get %ERRORLEVEL% from SQLCMD utility when some of .sql files contains syntax error? These files create stored procedures. They don't invoke "raiseerror", but they can conatin syntax error and I need to terminate the process. But it always…
kovalu
  • 129
  • 1
  • 2
  • 9
10
votes
4 answers

SQL Server Network Interfaces: Connection string is not valid [87]

When I am running this from cmd on my SQL Server 2008 instance: sqlcmd -U sa -S mymachinen_name\MSSQLSERVER (where MSSQLSERVER is my instance name) I get prompt for password and after that I get this: Password: HResult 0x57, Level 16, State 1 SQL…
rickepm
  • 113
  • 1
  • 1
  • 6
10
votes
2 answers

Sqlcmd trailing spaces in output file

Here is my simplified scenario: I have a table in SQL Server 2005 with single column of type varchar(500). Data in the column is always 350 characters in length. When I run a select on it in SSMS query editor, copy & paste the result set in to a…
Brian
  • 1,337
  • 5
  • 17
  • 34
10
votes
4 answers

SET QUOTED IDENTIFIER should be ON when inserting a record

I am stuck in a rather strange problem with SQL Server 2005, which throws "SET QUOTED IDENTIFIER should be on when inserting record" (using as SP) to the particular table. This worked fine earlier but is throwing this error randomly. I have…
RameshVel
  • 64,778
  • 30
  • 169
  • 213
9
votes
1 answer

sqlcmd - How to get around column length limit without empty spaces?

I'm trying to use sqlcmd on a windows machine running SQL Server 2005 to write a query to a csv file. The command line options we typically use are: -l 60 -t 300 -r 1 -b -W -h -1 However, the columns are getting truncated at 256 bytes. In an…
Tony Trozzo
  • 1,231
  • 6
  • 20
  • 34
9
votes
2 answers

Trouble connecting to SQL Server with sqlcmd from inside Docker

Working with debian linux inside a docker contaier. I have sqlcmd working properly and the neseccary drivers are installed and locatable. I know the server exists: root@0feafecac36f:/home/shiny# nmap -p 31010 -sT xxx.xxx.xxx.xxx Starting Nmap 7.60…
Stu
  • 1,543
  • 3
  • 17
  • 31
9
votes
2 answers

Unable to reach SQL database through SSH tunnel using sqlcmd

I need to connect to a an Azure SQL DB from my local machine through a jump box (Azure VM). I set up the port forwarding using the command: ssh -fN -L 41433:my-db.database.windows.net:1433 me@jump-box I can confirm the tunnel is set up because in…
nihil0
  • 359
  • 2
  • 10
9
votes
1 answer

How to declare variables in T-SQL scripts that are common across multiple scripts being run

I have a set of scripts to perform bulk data imports that I am trying to include in a single 'calling' script that uses SqlCmd mode. The issue I have is that each script includes the same set of declared vars that define paths or common objects.…
TravisPUK
  • 1,043
  • 1
  • 14
  • 17
9
votes
4 answers

How to tell if a variable (-v) is defined on command line (SQLCMD)

Is there a way to tell if a variable is defined on command line using SQLCMD? Here is my command line: sqlcmd -vDB="EduC_E100" -i"Z:\SQL Common\Admin\ProdToTest_DB.sql" Inside ProdToTest_DB.sql I would like to set some sort of conditional IF to…
Gerhard Weiss
  • 9,343
  • 18
  • 65
  • 67