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

sqlcmd - connect to local instance of SQLEXPRESS

I want to run a .sql batch script to delete records from a number of tables from cmd window. So, for that I am trying to connect to my local instance of SQLEXPRESS using following command- sqlcmd -e -s \SQLEXPRESS However, I am not able to…
inutan
  • 10,558
  • 27
  • 84
  • 126
15
votes
9 answers

Cannot connect with SSMS to SQL Server on Docker

I have followed the official Microsoft documentation and I have installed SQL Server Docker image As result I have a SQL Server image running on Docker at the IP address 172.17.0.2 I also can easily connect to it using sqlcmd with my dummy…
Francesco Mantovani
  • 10,216
  • 13
  • 73
  • 113
15
votes
3 answers

"Login timeout expired" error when accessing MS SQL db via sqlalchemy and pyodbc

So I have some trouble getting sqlalchemy and pyodbc working with a remote MS SQL Server. Local sqlcmd worked properly but not when I try to read the db via python code. Any help would be appreciated. Environment: Centos 7 SQLCmd version: Version…
ttback
  • 2,051
  • 5
  • 27
  • 40
15
votes
2 answers

sqlcmd script with spaces in filename

I have a simple SQLCMD script that includes some lines like this: /* Load data into Exampletable */ BULK INSERT dbo.Example /* NOTE: I've tried single AND double quotes here. */ FROM "C:\Example Filepath\test.csv" WITH ( …
SuperNES
  • 2,760
  • 9
  • 37
  • 49
15
votes
5 answers

SQL72007: The syntax check failed 'Unexpected end of file occurred.' in batch near :

In SSDT project (using VS2017/VS2015, SSDT version 15.1.61702.140), I cannot get my project to build. The compiler keeps complaining about the sql statement in my PostDeploymentScript (yes, I have set the BuildAction property to PostDeploy). The sql…
haku
  • 4,105
  • 7
  • 38
  • 63
14
votes
1 answer

sqlcmd.exe does not show any interactive output on the console (CMD and Powershell)

I just freshly installed SQL Server Express 2014 via Chocolatey. The service is currently running. When I run this inside my CMD or Powershell: > sqlcmd -e -S .\SQLEXPRESS 1> sp_databases; 2> I get no output, it just goes to the next prompt…
CMCDragonkai
  • 6,222
  • 12
  • 56
  • 98
14
votes
6 answers

sql scripting variable default value

I have a script file e.g. test.sql. I want to call this from another script, say caller.sql, in sqlcmd mode using :r test.sql. This works fine, but I want to use a scripting variable in test.sql. When I call test.sql from caller.sql I can set the…
DEH
  • 1,647
  • 3
  • 25
  • 45
14
votes
2 answers

Visual Studio 2010 database edition schema compare where target is dbproj

I'm using visual studio 2010 database edition and running a schema compare against a SQL database instance to sync up new objects on the database which arent yet in my project (dbproj). My solution contains many projects some of which reference each…
Dav Evans
  • 4,031
  • 7
  • 41
  • 60
14
votes
2 answers

ServerConnection.ExecuteNonQuery in SQLCMD Mode

I am using the Microsoft Data-Tier Application framework to create a deployment script based on a DacPackage object. I am attempting to use the Microsoft.SqlServer.Management.Smo.Server class to execute this script... SqlConnection deployConnection…
13
votes
3 answers

How to run a sql script file using sqlcmd and output to both shell and file

I'm trying to run a sql script from a file using sqlcmd, using the following command: sqlcmd -S -d -i -o -U -P I run my sql file and output to a log file. The problem is this…
Noam Shaish
  • 1,613
  • 2
  • 16
  • 37
13
votes
4 answers

Escape variable in sqlcmd / Invoke-SqlCmd

I am using powershell and using Invoke-SqlCmd. I am able to pass variables to SQL: $variables = @( "MyVariable='hello'" ) Invoke-SqlCmd ` -ServerInstance 'localhost' ` -Database 'master' ` -Username 'matthew' ` -Password 'qwerty'…
Matthew
  • 24,703
  • 9
  • 76
  • 110
12
votes
1 answer

Exporting CSV data using SQLCMD.EXE

I'm trying to export data from SQL Server into CSV format. I have a bat task to do this that's run at regular intervals. Command is: SQLCMD.EXE -d [db details] -i c:\export.sql -o c:\export.csv -s"," -W The SQL file is just a SELECT * from a view.…
Tim Fountain
  • 33,093
  • 5
  • 41
  • 69
12
votes
12 answers

How to use TAB as column separator in SQLCMD

SQLCMD supports the -s parameter to specify the column separator, but I couldn't figure how how to represent the tab (CHAR(9)) character. I have tried the following but both don't work: sqlcmd -S ServerName -E -Q"select * from mytable" -s"\t" -o…
dawntrader
  • 765
  • 2
  • 11
  • 19
12
votes
2 answers

Is it possible to automatically force SQLCMD mode in script?

We're using Visual Studio Database Professional and it makes heavy use of SQLCMD variables to differentiate between environments while deploying. I know there's several directives available for setting context (like :connect for server name). Is…
Nicolas Webb
  • 1,312
  • 10
  • 22
12
votes
2 answers

Unicode support for Invoke-Sqlcmd in PowerShell

The PowerShell sqlps module provides core support for SQL Server access from within PowerShell and its Invoke-Sqlcmd cmdlet is its main workhorse for executing literal queries or SQL script files (analogous to the non-PowerShell sqlcmd utility). I…
Michael Sorens
  • 35,361
  • 26
  • 116
  • 172
1 2
3
77 78