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

1084 questions
146
votes
11 answers

How to export data as CSV format from SQL Server using sqlcmd?

I can quite easily dump data into a text file such as: sqlcmd -S myServer -d myDB -E -Q "select col1, col2, col3 from SomeTable" -o "MyData.txt" However, I have looked at the help files for SQLCMD but have not seen an option specifically for…
Ray
  • 169,974
  • 95
  • 213
  • 200
95
votes
6 answers

How do I grant myself admin access to a local SQL Server instance?

I installed SQL Server 2008 R2 to my local machine. But, I can't create a new database because of rights (or lack of). "CREATE DATABASE PERMISSION DENIED" So, I tried to assign the admin privileges to my current login "User does not have…
SkonJeet
  • 4,167
  • 3
  • 20
  • 31
87
votes
3 answers

Is there a way to suppress "x rows affected" in SQLCMD from the command line?

Is there a way to suppress "x rows affected" in SQLCMD from the command line? I'm running an MSBuild script and don't want it clogging up my log on my build server. I'd rather not have to add "SET NOCOUNT ON" in every script, so if there's a way…
Josh Kodroff
  • 25,181
  • 26
  • 90
  • 147
63
votes
13 answers

How to export SQL Server 2005 query to CSV

I want to export some SQL Server 2005 data to CSV format (comma-separated with quotes). I can think of a lot of complicated ways to do it, but I want to do it the right way. I've looked at bcp, but I can't figure out how to put the quotes around the…
John M Gant
  • 17,902
  • 17
  • 59
  • 82
62
votes
4 answers

How to properly manage database deployment with SSDT and Visual Studio 2012 Database Projects?

I'm in the research phase trying to adopt 2012 Database Projects on an existing small project. I'm a C# developer, not a DBA, so I'm not particularly fluent with best practices. I've been searching google and stackoverflow for a few hours now but I…
54
votes
6 answers

Conditional logic in PostDeployment.sql script using SQLCMD

I am using a SQL 2008 database project (in visual studio) to manage the schema and initial test data for my project. The atabase project uses a post deployment which includes a number of other scripts using SQLCMD's ":r " syntax. I would like to be…
Rob Bird
  • 3,584
  • 2
  • 23
  • 37
35
votes
3 answers

How to connect Sqlcmd to the server?

I just installed SQL Server 2008, and I wanted to use the command editor to execute queries. To do that I opened the command prompt, and I typed Sqlcmd -S Serverinstance but I got an error saying : Named Pipes Provider: Could not open a…
Zakaria Marrah
  • 707
  • 6
  • 14
  • 24
32
votes
1 answer

How to pass in parameters to a SQL Server script called with sqlcmd?

Is it possible to pass parameters to a SQL Server script? I have a script that creates a database. It is called from a batch file using sqlcmd. Part of that SQL script is as follows: CREATE DATABASE [SAMPLE] ON PRIMARY ( NAME = N'SAMPLE', FILENAME…
Jeremy
  • 2,751
  • 6
  • 24
  • 31
31
votes
18 answers

How to suppress hyphens in SQLCMD

How can I suppress hyphens (------------) from the results set of this sqlcmd command: C:\temp>sqlcmd -d AdventureWorks -s ";" -Q "SET NOCOUNT ON SELECT top 5 FirstName, LastName FROM Person.Contact;" FirstName …
atricapilla
  • 2,400
  • 10
  • 35
  • 38
29
votes
3 answers

Run sqlcmd without having SQL Server installed

I'm working on a Java program that is calling sqlcmd. It works perfectly on a computer with SQL server installed, but not at all on a computer that doesn't. I want to include the necessary exe and dll files to run this command in my jar. Anyone…
chama
  • 5,537
  • 13
  • 57
  • 72
29
votes
4 answers

SqlServer 08: Query to list all databases in an instance?

How do I list all the databases for a given sql server 08 instance using sqlcmd?
Dane O'Connor
  • 67,996
  • 36
  • 114
  • 164
29
votes
3 answers

How to terminate sqlcmd immediately after execution completed?

I create a process in C# to execute sqlcmd /S /d /E /i to run sql script that create tables, views, stored procedures. The problem is the process does not terminate after the execution is completed. I want to know how can I…
Anonymous
  • 8,708
  • 20
  • 80
  • 127
27
votes
3 answers

Docker + mssql-server-linux: How to launch .sql file during build (from Dockerfile)

I am trying to create my own Docker image with MSSQL DB for development. It's based on microsoft/mssql-server-linux image. During the build I want to copy some .sql files into the container and then run these scripts (to create DB schemas, tables,…
Tomáš Fábry
  • 421
  • 1
  • 4
  • 7
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
  • 21,448
  • 45
  • 133
  • 206
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,561
  • 3
  • 14
  • 20
1
2 3
72 73