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
5
votes
1 answer

The file specified for :r command was not found

In MS SQL Server Management Studio in SQLCMD Mode I use this script to run another script: :setvar path "C:\workspace\scripts\scripts\" :r $(path)'VERSIONS.sql' But it gets this error: A fatal scripting error occurred. The file specified for :r…
Graham
  • 5,488
  • 13
  • 57
  • 92
5
votes
2 answers

SQLCMD scripting error

i am using SQLCMD to run a .sql file which is 270 MB. The script file (.sql) was generated using Red Gate SQL Data Compare synchronization wizard. I cannot run it from SSMS because of insufficient memory. I log into the server and go to command…
user176047
  • 311
  • 4
  • 20
5
votes
2 answers

Powershell Invoke-Sqlcmd Login Failed

I am trying to run a sql query from powershell for Windows SQL Server 2008 R2. It looks like the code is failing to pass SQL Server Authentication credentials to run the sqlcmd correctly. I am running this first part directly from powershell, not as…
snoop
  • 379
  • 3
  • 8
  • 21
5
votes
2 answers

ForEach Property in Get-Member

I have the following snippet of code in my powershell script $sqlHDR = "SELECT * FROM OPENROWSET ('Microsoft.Jet.OLEDB.4.0','Excel 8.0;Database=" + $dir + $m + ".xls;HDR=NO','SELECT * FROM [" + $y + "`$B1:Z1]')" $doHDR = invoke-sqlcmd -query…
Ryan Gillies
  • 413
  • 4
  • 8
  • 19
5
votes
3 answers

Export SQL query result to CSV

I'm using sqlcmd to export a query result with two columns to csv. The simple query is: SELECT DISTINCT CustomerGuid, CustomerPassword FROM ServiceOrder ORDER BY CustomerGuid When I open the exported csv in Excel both customer and…
Morten Laustsen
  • 527
  • 4
  • 13
  • 25
5
votes
2 answers

Getting contents of XML File into a variable using sqlcmd ":r"

I have an XML file which I want to use to load static data into my database. Preferably, I would like to use the sqlcmd ":r" to do a text replace so that the xml can be treated as hard coded data in my sql script. With hard coded data, my script…
scourge192
  • 1,909
  • 16
  • 22
5
votes
4 answers

Disable SQLCMD Syntax Check

We sometimes write dataport scripts that rename tables or columns and have other complex logic. We also have SQL in IF statements referencing those old columns or tables. We have a standard to use IF statements to make our SQL Scripts multi run…
Ryu
  • 8,641
  • 10
  • 65
  • 98
5
votes
6 answers

Microsoft SQL Server Native Client 10.0 Login timeout expired

I have a freshly installed SQL Server 2008 R2 Express. I'm trying to run SQLCMD locally (please do take note locally) but I am receiving the error: Named Pipes Provider: Could not open a connection to SQL Server [2] Sqlcmd: Error: Microsoft SQL…
Xian Garcia
  • 51
  • 1
  • 1
  • 5
5
votes
4 answers

cmd.ExecuteNonQuery value of i integer is going -1 from 0

Here i create table in database dynamically. User enters name as his wish and selects language radiobutton. So problem is after executing cmd.ExecuteNonQuery value of i integer is going -1 from 0. And shows that table couldnt be created but when i…
4
votes
1 answer

How do I name the output textfile to YYYYMMDD based on the system date?

How do I name the output textfile to YYYYMMDD based on the system date? sqlcmd -S DataBBB -i c:\scripts\followup.sql -o %DATE:~4,2%_%DATE:~7,2%_%DATE:~-4%.txt -s ; -W -u Now the output text file is 01_31_2012.txt. How can I change it to…
waterbottle
  • 85
  • 2
  • 11
4
votes
0 answers

sqlcmd command to SET IDENTITY_INSERT ON

I am creating a batch file (.bat) with sqlcmd command containing sql insert query. But in which table it will insert data that has one IDENTITY column. Can anyone please help me with the sqlcmd command to SET IDENTITY_INSERT ON?
user1018213
  • 2,095
  • 4
  • 16
  • 10
4
votes
2 answers

How to make a OR statement in a batch file?

Hi want to exclude 2 or more files from my batch file. My batch file executes all SQL files in that folder. Here is the code: ECHO %USERNAME% started the batch process at %TIME% >output.txt FOR %%G IN (*.sql) DO ( //IF would go here to skip…
Willem
  • 9,166
  • 17
  • 68
  • 92
4
votes
1 answer

Database Project and SQLCMD doing a comparison

We currently have a Database Project, which we wrote a WIX Installer for to deploy it, using VSDBCMD means we are able to do a comparison against the current database, and do an update or fresh install when required. However we have found some…
Lex
  • 879
  • 3
  • 16
  • 27
4
votes
2 answers

sqlcmd running into unicode conversion issues on SLES12SP5

I have a SLES12-SP5 docker container running and I followed the instructions here : https://learn.microsoft.com/en-us/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server?view=sql-server-ver15#suse17 to install…
Pranav Raj
  • 781
  • 1
  • 8
  • 19
4
votes
1 answer

Dealing with SQLCMD :r in a post-deployment script

We use a Visual Studio Database Project for the source control of the schema of our database. We also want to script certain data (configuration values etc.) from the database, so we use a custom command line utility for dumping data into insert…
Sandor Davidhazi
  • 888
  • 1
  • 9
  • 20