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
25
votes
4 answers

How to change the port when calling sqlcmd

I have this command sqlcmd.exe -E -i "C:\joe-db-scripts\joe-db-tasks.Install.sql" and I need to add the specific non-standard port of 32001. How do I do that? I was thinking of some flag like -p or something but I don't know the syntax.
Matt Elhotiby
  • 43,028
  • 85
  • 218
  • 321
24
votes
4 answers

SSDT Publish errors on Creating Publish Preview

I am using Visual Studio 2013 to manage a .sqlproj file containing our database schema. The schema has been deployed successfully dozens of times. When attempting to publish to one specific target database, the "Creating publish preview" step…
Eric Patrick
  • 2,097
  • 2
  • 20
  • 31
22
votes
7 answers

How to execute sqlcmd from powershell?

I have a string in powershell, which contains a native sqlcmd command. The command itself can be executed successfully in cmd.exe. I have difficulty in executing them in powershell. Anyone can help? Thanks. This is sql.sql select…
Just a learner
  • 26,690
  • 50
  • 155
  • 234
22
votes
6 answers

How to get the relative path of file in SQLCMD Mode in SSMS?

I have the below master script which creates tables, and inserts some data and then creates the stored procedures. --todo_master.sql use master go :r todo_create_ddl.sql :r todo_create_dml.sql :r todo_create_sprocs.sql go However, even though the…
Animesh
  • 4,926
  • 14
  • 68
  • 110
22
votes
2 answers

Return value of SQLCMD

I need to check the exit status (success/failure) of a query run through SQLCMD utility. For example, the server I am connecting doesn't have a database name EastWind. Then, the command below fails with the message ... > "C:\Program Files\Microsoft…
mutelogan
  • 6,477
  • 6
  • 20
  • 15
20
votes
5 answers

SQLCMD, command-line variables and script :setvar

When running SQLCMD.exe and providing command-line arguments for scripting variables, I expect that the values provided on the command-line will override those defined in the SQL script file. e.g. Given the following SQL Script: :setvar XXX "SQL…
VirtualStaticVoid
  • 1,656
  • 3
  • 15
  • 21
20
votes
3 answers

How to format SQLCMD output

I am using below command line to run a SQL query using SQLCMD sqlcmd -S Server -Q "select top 100 * From people" -d people -t 10 The table has 20 columns and when i look at output command line window wraps the text and makes it difficult to…
user1550159
  • 1,197
  • 3
  • 19
  • 36
19
votes
1 answer

sqlcmd how to run a query against specific database?

so this is the query that i want to run sqlcmd -E -S localhost\MSSQLSERVER_2016 -i C:\Tables.sql I have multiple databases in this instance and when the command above runs, i have no idea which database it runs the query against. Is there any way…
Vibol
  • 615
  • 1
  • 8
  • 25
19
votes
3 answers

Accessing the returned value of a SQL statement in SQLCMD

I'm trying to get the value of a SQL statement when I run it in a DOS batch file ... sqlcmd -E -S DEVSERVER -Q "SELECT COUNT(1) as [CaseCount] FROM Cases" I'm not after the error level as in this stackoverflow question, rather I'm after the actual…
SteveC
  • 15,808
  • 23
  • 102
  • 173
18
votes
4 answers

Error Handling with Batch File & Sqlcmd

I have a batch file that runs some SELECT queries using sqlcmd, puts the results into text files, and uploads those files onto an FTP server. That's all working just the way it should, which is how I like things to work. I've been wondering about…
SuperNES
  • 2,760
  • 9
  • 37
  • 49
17
votes
4 answers

passing path to SqlCmd within powershell script

I'm trying to write a powershell script which will execute sqlcmd.exe to run a sql script. The script contains a SQLCMD variable which I want to pass in on the command line via sqlcmd's -v switch. The problem is that powershell is doing something…
Dav Evans
  • 4,031
  • 7
  • 41
  • 60
16
votes
2 answers

How do I call a stored procedure with arguments using sqlcmd.exe?

I need to call a stored procedure and pass arguments in from Powershell. I think the best option is to use sqlcmd.exe but I'm not sure how to pass arguments to the stored proc using this tool.
Martin Doms
  • 8,598
  • 11
  • 43
  • 60
16
votes
3 answers

Cannot connect to LocalDB via sqlcmd

I am currently learning SQL Server and am having problems connecting to my local database using sqlcmd. When I type in the following into Powershell: sqlcmd -S "(localdb)\mssqllocaldb" I get the following error message: Sqlcmd: Error: Microsoft…
SQLnewbie
  • 161
  • 1
  • 1
  • 4
16
votes
1 answer

Get line number in file following TSQL error message

Consider the following sql script :ON ERROR EXIT PRINT 'Line 3' GO PRINT 'Line 6' GO SELECT * FROM NonExistingTable GO PRINT 'Line 12' GO When you run with SQLCMD > sqlcmd -i MyScript.sql Line 3 Line 6 Msg 208, Level 16, State 1, Server…
mnaoumov
  • 2,146
  • 2
  • 22
  • 31
16
votes
1 answer

How to use `sqlcmd` command without username and password in SQL Server 2008?

How can I run commands, passed to sqlcmd, as currently logged on user automatically i.e without having to enter username and password? I want to use Windows Authentication only. It is a local server.
Aashiq Hussain
  • 543
  • 2
  • 8
  • 17
1
2
3
77 78