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
167
votes
12 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
  • 187,153
  • 97
  • 222
  • 204
112
votes
9 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,827
  • 4
  • 23
  • 32
96
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
  • 27,301
  • 27
  • 95
  • 148
65
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
  • 18,970
  • 18
  • 64
  • 82
62
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,764
  • 2
  • 28
  • 38
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…
39
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
  • 3,221
  • 7
  • 27
  • 31
39
votes
19 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,560
  • 12
  • 36
  • 39
38
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
  • 755
  • 6
  • 15
  • 28
37
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
  • 75,180
  • 37
  • 119
  • 173
33
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
  • 481
  • 1
  • 5
  • 7
33
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
  • 9,366
  • 22
  • 83
  • 133
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,973
  • 14
  • 61
  • 77
28
votes
11 answers

Sqlcmd to generate file without dashed line under header, without row count

Using the following sqlcmd script: sqlcmd -S . -d MyDb -E -s, -W -Q "select account,rptmonth, thename from theTable" > c:\dataExport.csv I get an csv output file containing acctnum,rptmonth,facilname -------,--------,--------- …
Lill Lansey
  • 4,775
  • 13
  • 55
  • 77
27
votes
5 answers

How to use sqlcmd to create a database

I have a .sql script and I want to build a database from it. How to do it in sqlcmd? I know it's like: CREATE DATABASE dbName GO But how to specify the .sql script and location to build the database?
Cobold
  • 2,563
  • 6
  • 34
  • 51
1
2 3
77 78