Questions tagged [sqlcmd]

SQLCMD is Microsoft SQL Server's command line utility that allows you execute T-SQL commands from a command prompt.

SQLCMD is Microsoft SQL Server's command line utility that allows you execute T-SQL commands from a command prompt. This utility was introduced in SQL Server 2005 and replaced SQL 2000's osql utility.

While commonly used from cmd.exe or Powershell, SQLCMD can also be accessed within SQL Server Management Studio by using SQLCMD mode.

This tag commonly relates to questions involving:

180 questions
52
votes
3 answers

How to make sqlcmd return an ERRORLEVEL other than 0 when the .sql script fails?

I'm running sqlcmd from a batch file and I was wondering how to make it return an ERRORLEVEL other than 0 when something goes wrong with the backup.
leeand00
  • 1,722
  • 6
  • 20
  • 36
26
votes
4 answers

SQLCMD command is not able to insert accents

I am trying to run sqlcmd.exe in order to setup a new database from command line. I am using SQL SERVER Express 2012 on Windows 7 64 bits. Here's the command I use: SQLCMD -S .\MSSQLSERVER08 -V 17 -E -i %~dp0\aqualogyDB.sql -o…
Oskytar
  • 363
  • 1
  • 3
  • 7
21
votes
3 answers

How to connect to SQL Server using sqlcmd on Linux?

I have set up the Microsoft ODBC Driver 13 for SQL Server on an Ubuntu 16.04 machine. I am now trying to test a database connection, that includes both the server name and an instance name. The following both work fine from a Windows…
geographika
  • 533
  • 2
  • 4
  • 15
19
votes
2 answers

Can I enable SQLCMD mode from inside a script?

Is there something like a directive which I can use in a script to force SSMS to enable/disable SQLCMD mode?
bernd_k
  • 12,369
  • 24
  • 79
  • 111
18
votes
1 answer

Connect to LocalDB via SQLCMD in PowerShell

I'm trying to access the "localdb\MSSQLLocalDB" server on my computer through PowerShell with the SQLCMD utility. I'm using PowerShell v5, .NET v5.0, and the server name is (localdb)\MSSQLLocalDB when I connect to it in Microsoft SQL Server…
jmk22
  • 305
  • 1
  • 3
  • 6
15
votes
1 answer

User-shared queries: Dynamic SQL vs. SQLCMD

I have to refactor and document a number of foo.sql queries which will be shared by a team of DB tech support (for customer configurations and things like that). There are types of tickets that come regularly where each customer have their own…
Phrancis
  • 1,320
  • 1
  • 7
  • 24
14
votes
1 answer

Setting variables in SQLCMD mode

Using SQL Server 2008 R2 Enterprise Edition Consider the following statement: :setvar source_server_name "SERVERNAME\INSTANCENAME" Is it possible in SQLCMD mode to get that value using TSQL Something like: :setvar source_server_name = SELECT…
user4659
12
votes
4 answers

How do I get SSMS to use the relative path of the current script with :r in sqlcmd mode like SSDT does?

If I have foo.sql and bar.sql in the same folder, foo.sql can reference bar.sql when run from SSDT in sqlcmd mode with :r ".\bar.sql". However, SSMS won't find it. Procmon shows SSMS is looking in %systemroot%\syswow64: How do I tell SSMS to look…
Justin Dearing
  • 2,717
  • 6
  • 36
  • 52
12
votes
6 answers

Did anybody use sqlcmd mode in practice?

SQLServer 2005 introduced something called SQLCMD Mode msdn link. At first glance this mode adds variable subsitution from command line batch files, and some escaping to OS commands. Is this feature used in your environments, production or test?
bernd_k
  • 12,369
  • 24
  • 79
  • 111
11
votes
5 answers

How to display MS SQL results vertically?

Here is what I've got currently when executing queries in sqlcmd: 1> SELECT 1,2,3,4 2> GO ----------- ----------- ----------- ----------- 1 2 3 4 (1 rows affected) Although in case of longer results this…
kenorb
  • 475
  • 1
  • 6
  • 11
10
votes
2 answers

Variable inside another variable in SQLCMD mode

Trying to tidy up my SQL script with SQLCMD mode and I ran into an issue: :setvar db_suffix "some_suffix" :setvar some_db "some_db_$(db_suffix)" print 'some_db: $(some_db)' The output of that is: some_db: some_db_$(db_suffix) However, what I…
user193130
  • 223
  • 3
  • 6
9
votes
2 answers

How do I pass multiple statements into sqlcmd?

Wanting to automate some tasks instead of firing up SSMS for everything, I'm trying to write a batch file to run these statements, which work perfectly when I paste them into the sqlcmd prompt. They simply check the mirroring status, suspend…
RonJohn
  • 694
  • 2
  • 12
  • 31
9
votes
1 answer

How to output more than 4000 characters in sqlcmd

I've made a stored procedure restoredatabase that returns code to restore a database. The output script looks like this: restore database [DB] from disk='F:\FULL1.bak' with norecovery restore database [DB] from disk='F:\DIFF1.bak' with…
9
votes
1 answer

Executing TSQL script generated from SSMS in SQLCMD. Failing on quotes

I am attempting to deploy a database to a machine by executing a script generated by the "Generate Scripts" tool in SQL Server Management Studio. Here is the command I've issued: sqlcmd -S LOCALHOST\sqlexpress -I -U user -P ******** -i …
munroan
  • 93
  • 1
  • 5
9
votes
2 answers

Suppress SQLCMD messages

Would it be possible to suppress all sqlcmd messages output to the cmd window? I am running a large script and it seems to be slowed down a lot with all the Processed XXXX total records messages. The script is over 4Gb so there is a large amount of…
Agony
  • 449
  • 3
  • 8
  • 14
1
2 3
11 12