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
8
votes
2 answers

How do we tell sqlcmd to continue "on error" with next batch?

Any ideas on how to accomplish this? USE [db_name] BEGIN TRANSACTION ...TONS OF INSERTS COMMIT; RAISERROR (..) WITH NOWAIT; //If error continue with next batch BEGIN TRANSACTION ...TONS OF INSERTS COMMIT; RAISERROR (..) WITH NOWAIT; ...
Newbie
  • 7,031
  • 9
  • 60
  • 85
7
votes
3 answers

Initialize SQL Server database in docker container without sqlcmd

I am working on a MacBook Pro with M1 CPU so I can't use the "normal" mssql docker image. I am using azure-sql-edge that doesn't have sqlcmd to initialize the database (create schema, database, login). I have created a sql script that I would like…
chenny
  • 769
  • 2
  • 17
  • 44
7
votes
3 answers

SQLCMD :r where path is a variable

Does the SQLCMD command :r support non-constant literal paths? For example: setvar $(path1) '.\script.sql' :r $(path1) -- SQL01260: A fatal parser error occurred: . :r '$(path1)' -- SQL01260: A fatal parser error occurred: . :r "$(path1)" --…
Serguei
  • 2,910
  • 3
  • 24
  • 34
7
votes
4 answers

sqlcmd won't run - Is this a config issue?

I'm trying to use sqlcmd to execute some SQL scripts. Using a test command with a simple query like: sqlcmd -S HOSTNAME -d MYDATABASE -Q 'SELECT Names FROM Customers' sqlcmd does not appear to make any attempt to connect to the server as it…
nuit9
  • 1,633
  • 3
  • 14
  • 8
7
votes
3 answers

sqlcmd not able to find a library (libmsodbcsql-17.0.so.1.1) that is there

I'm on Ubuntu 16.04 trying to use sqlcmd launched programmatically from a script to do a SQL query in the VM's cloud. vm-dev:~$ sudo sqlcmd -S my-db.url.net -d my-db I keep getting this error: Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server…
Hack-R
  • 22,422
  • 14
  • 75
  • 131
7
votes
2 answers

SQLCMD - trying to drop a database with alphanumeric name '14Data' gives Syntax Error

I am using SQL SERVER 2005 Express. I can delete the database from the query analyzer, what you call Mgmt Studio (ssmsee) by providing double quotes to the database name and things get done; Drop Database "14data" Go and the db is gone, but sqlcmd…
maqk
  • 313
  • 2
  • 7
  • 18
7
votes
2 answers

PowerShell Using **DacServices** With SQLCMD Variables To Deploy A DACPAC

In PowerShell I'm using Microsoft.SqlServer.Dac.DacServices and Microsoft.SqlServer.Dac.DacDeployOptions to deploy/update a database DACPAC. The problem I am having is finding where to set the SQLCMD Variables the package requires. Abbreviated…
Jon
  • 150
  • 2
  • 8
7
votes
1 answer

sqlcmd from command-line with sql authentication

SQLCMD uses windows authentication by default. According to the MSDN documentation, you can change it by adding a -U parameter to the command and then the user name. The problem is with the password. Is there any way to access the sql server if…
chama
  • 5,973
  • 14
  • 61
  • 77
7
votes
2 answers

How to use :setvar properly?

I call several sql files using: :r C:\Scripts\Script1.sql :r C:\Scripts\Script2.sql :r C:\Scripts\Script3.sql I have this idea about the :r call from here: TransactSQL to run another TransactSQL script. The Script1.sql script might as well also…
Thomas Mondel
  • 1,944
  • 3
  • 20
  • 25
6
votes
2 answers

sqlcmd with output file and screen output

I do some command line batch (.bat) with sqlcmd as this way: sqlcmd -i Scripts\STEP01.sql -o PROCESS.log -S MYSERVER -E -d MYDATABASE and i need an output file (it's works currently) and also the output trought the screen to do something…
Leandro Bardelli
  • 10,561
  • 15
  • 79
  • 116
6
votes
2 answers

How to Set value dynamically in setvar variable SQL

I am using SQLCMD mode. I want to set setvar variable value. How should I set value in :setvar? My Code are look like this : DECLARE @DatabaseName AS nvarchar(180) set @DatabaseName = '$(dbName)' print @DatabaseName :setvar DatabaseName…
Ravi Shah
  • 843
  • 12
  • 29
6
votes
3 answers

Execute SQLCMD.exe from PowerShell in an Azure DevOps Release Pipeline

I'm porting our deployment infrastructure to Azure DevOps (formerly Visual Studio Team Services), but I've run into an issue I can't find any good solutions to. We're using EF Core Migrations, and to work around some unrelated issues we need to…
Tomas Aschan
  • 58,548
  • 56
  • 243
  • 402
6
votes
8 answers

Combine command line and T-SQL - SQL Server

I am trying to execute some sqlcmd through T-SQL on SQL Server 2008. There is a part of my code where I am checking a data file size and if that data file size does not equal to 0, then start deleting the specific table so I can BCP in new data.…
AznDevil92
  • 554
  • 2
  • 11
  • 39
6
votes
2 answers

sqlcmd not showing RESTORE database stats

The following command in a cmd window sqlcmd -S. -Usa -Ppass -dmaster -Q "RESTORE DATABASE [MYDATABASE] FROM DISK = 'D:\SQL Server\MYDATABASE.BAK' WITH FILE = 1, NOUNLOAD, REPLACE, STATS = 10" displays the following progress output: 10 percent…
6
votes
2 answers

Is it possible to use SqlCmd to run scripts against a localdb database?

I've got a large SQL script (1gb) that I'm trying to run against a (localdb)\v11.0 instance, using SqlCmd in a command line window, but so far no commands I've tried have worked with it at all. E.g. sqlcmd.exe -S "(localdb)\v11.0" -Q "select…
Ted
  • 2,525
  • 2
  • 37
  • 54