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

How do I run a script using a BAT file?

I would like to have a BAT file open a sql server script. Currently I have this code in the sql file: declare @path varchar(255), @mydb varchar(50) SELECT @mydb = 'timeclockplus' select @path = 'C:\Program Files\Microsoft SQL…
Alex Gordon
  • 57,446
  • 287
  • 670
  • 1,062
9
votes
1 answer

sqlcmd output file remove header and footer

Using SQLCMD, I can output the query result to a text file, now, the text file has header: And footer: How do I remove them during query? My query, inside a sql file: SELECT internal_no, item_no, dspl_descr, rtl_prc FROM PLU My SQLCMD…
jaa2013
  • 221
  • 2
  • 3
  • 11
9
votes
1 answer

How to set a sqlcmd output to a batch variable?

I'm trying to set the output of a sqlcmd query to a variable in a batch file. Here's my query: sqlcmd -S -d -Q "select max(Column1)+1 from Table1" This gives me exactly what I would expect and what I want: ----------- …
JohnN
  • 968
  • 4
  • 13
  • 35
9
votes
5 answers

I need best practice in T-SQL Export data to CSV (with header)

What I need to do is export data into CSV file using T-SQL. And I'm very confused about there are many ways can do it, I don't know to choose which one, please help me to confirm the bollowing: As I know there are about 3 methods, and I want you…
Guoliang
  • 885
  • 2
  • 12
  • 20
9
votes
6 answers

Display DataType and Size of Column from SQL Server Query Results at Runtime

Is there a way to run a query and then have SQL Server management studio or sqlcmd or something simply display the datatype and size of each column as it was received. Seems like this information must be present for the transmission of the data to…
Blake Taylor
  • 9,217
  • 5
  • 38
  • 41
8
votes
1 answer

Cannot set IDENTITY_INSERT in batch

Case Currently I'm working on database seeding script, executed with sqlcmd. For example this script sample: IF (SELECT COUNT(*) FROM Genders)=0 BEGIN PRINT N'Seeding table Genders...' SET IDENTITY_INSERT Genders ON GO INSERT INTO…
Herman Cordes
  • 4,628
  • 9
  • 51
  • 87
8
votes
6 answers

Drop DB using MSBuild even if in use

I am using the following command in my MSBuild file to drop a database sqlcmd -E -S -Q "DROP DATABASE " But sometimes I get the error Cannot drop database because it is currently in use. What command should I use so that the…
Achinth Gurkhi
  • 2,136
  • 4
  • 24
  • 45
8
votes
4 answers

I need dump table from SQL Server to csv in utf-8

This works but not in utf-8: sqlcmd -S Server -d DB -E -s, -W -i "C:\Localdata\test.sql" | findstr /V /C:"-" /B > "C:\Localdata\Test.csv" Would anybody like to help fix this into utf-8?
JoezCodes
  • 157
  • 2
  • 15
8
votes
2 answers

trying to run sqlcmd fails, unable to establish connection?

When I try to run sqlcmd (from the cmd prompt) I get the following error: HResult 0x2, Level 16, State 1 Named Pipes Provider: Could not open a connection to SQL Server [2]. Sqlcmd: Error: Microsoft SQL Server Native Client 10.0 : A…
meds
  • 21,699
  • 37
  • 163
  • 314
8
votes
4 answers

Need help to write bat file that execute sql scripts in (sql server 2008 and another 3 files.?

I am sure these has been asked before but cannot find clear instruction how to create a batch file lets call it "Update Database" this batch file should Execute sql scripts located in different folders Execute another 3 bat files. Any quick…
user451259
  • 1,003
  • 2
  • 12
  • 13
8
votes
7 answers

What's the best way to start and stop SQL Server from the command line?

I need to start and stop SQL Server from the command line. I am willing to write a small C# program if necessary, but I suspect that there's something (PowerShell, maybe?) that currently exists that does this gracefully. Thank you.
rp.
  • 17,483
  • 12
  • 63
  • 79
8
votes
1 answer

How to make SSDT Profile.xml SqlCmdVariable an empty string or optional

I am using SSDT (and sqlproj) for our MSSQL projects. We have a few variables we need to set when publishing to each environment. This works great on most of our environments where we assign values to all of the variables, but when we publish to our…
Saan
  • 544
  • 5
  • 19
8
votes
2 answers

Executing Publish Scripts with SQLCMD doesn't return

I am having an issue running SQL scripts generated by the Publish > Generate Scripts dialog on a SQL Server Database Project (Visual Studio 2013). The generated script executes fine and usually with 2 - 10 seconds if run from the SQL Management…
Curtis Ford
  • 151
  • 1
  • 1
  • 6
8
votes
3 answers

SQLCMD include all scripts in folder

In my Post Deployment Script, I would like to include all script files in a folder using a wildcard like this: :r .\$(ReleaseName)\*.sql Is there a way to do this? I can't find any..
SAS
  • 3,943
  • 2
  • 27
  • 48
8
votes
1 answer

Powershell SqlCmd - Using result values without defining variables

I am wondering if there is any way to use values for a dataset in a subsequent query. Here is an example: Works: $Result = Invoke-Sqlcmd -query "SELECT Id, FirstName, LastName FROM Person" foreach($item in $Result){ $ID= $item.Id …
KickinMhl
  • 1,218
  • 3
  • 14
  • 32