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
0
votes
2 answers

How to bind the -v parameter of sqlcmd from a PowerShell

The following code, setting the -v parameter directly is working $sqlcmd = @(Resolve-Path "$env:ProgramFiles\Microsoft SQL Server\*\Tools\binn\SQLCMD.EXE")[0] $path1 = 'D:\somescript.sql' & $sqlcmd -b -S NB-BK\SQLEXPRESS -d BK_Prod -U sa -P…
bernd_k
  • 11,558
  • 7
  • 45
  • 64
0
votes
0 answers

sqlcmd runs into error while parsing my .sql file

I am invoking sqlcmd as follows: sqlcmd -S SERVERNAME\SQLEXPRESS -i "C:\Documents and Settings\user\Desktop\data\bigDB.sql" And I get an error while the script is executing: Sqlcmd: Error: Syntax error at line 86626 near command '%' in file…
Utpal Mattoo
  • 890
  • 3
  • 17
  • 41
0
votes
1 answer

Trying to create a batch file exporting using 'sqlcmd' to a CSV

Possible Duplicate: How to export data as CSV format from SQL Server using sqlcmd? I'm trying to create a .bat file to query from a database using sqlcmd. This is what I got so far using sources online: sqlcmd -S servername\sqlexpress -d DNAME -U…
ZLeOLT
  • 3
  • 3
0
votes
1 answer

SQLCMD.EXE generates ugly report. How to format it?

I did batch to run SQL query like use [AxDWH_Central_Reporting] GO EXEC sp_spaceused @updateusage = N'TRUE' GO It displays 2 tables and generates some ugly report with some kind of 'P' unneeded letters... See below Changed database context to…
Juri Bogdanov
  • 295
  • 1
  • 11
  • 19
0
votes
1 answer

Command prompt errorlevel issue after running sqlcmd

My code is below: CALL:ECHOCOLOR "Executing on server:[S059D526\TCH] db:CS filename:[9990294_UPDATE_Concessions.sql]." Cyan SET CURRENT_SCRIPT="9990294_UPDATE_Concessions.sql" if not exist "9990294_UPDATE_Concessions.sql" ( CALL:ECHOCOLOR…
dsmaevich
  • 1
  • 2
0
votes
3 answers

Can sqlcmd.exe path be configured?

I want to change the location of sqlcmd.exe from its default place to my desired one. Can I place it in my project directory for example?
Fraz Sundal
  • 10,288
  • 22
  • 81
  • 132
0
votes
1 answer

Creating a csv with sqlcmd

I'm trying to create a csv using SQL Command. I am using a simple table as a test. I've written this so far: DECLARE @sql VARCHAR(1000), @cmd VARCHAR(100), @sqlCommand VARCHAR(1000) Set @cmd = 'Select * From…
cloud311
  • 3,101
  • 5
  • 20
  • 20
0
votes
1 answer

How to execute .sql script (sybase) file using cmd

I am having .sql (sybase) file which creates the table i.e. "Create_table_mytable.sql" But before creating the table it check of the existing table and drops the same. here is how scripts looks like: - setuser 'dbo' go IF EXISTS (SELECT 1…
Anil Purswani
  • 1,857
  • 6
  • 35
  • 63
-1
votes
0 answers

Batch script to parse output of sqlcmd and get only one value

I am trying to find the space used by a database through the sqlcmd utility. I used sp_spaceused command to get the details of the database size. Now i want to store only the available size (i.e unallocated space) to a variable . I think the output…
-1
votes
1 answer

RESTORE DATABASE with dashes-in-its-name with sqlcmd

Given: Backup from Windows SQL Server 2022, containing a database with dashes in its name 1> RESTORE filelistonly FROM DISK = '/tmp/my-awesome-database_FULL_20230810_000209.bak'; 2> go LogicalName …
snuk182
  • 1,022
  • 1
  • 12
  • 28
-1
votes
1 answer

OPENJSON() returns 0 rows from SQLCMD, but returns many rows in SSMS

As the title says, I have a set of SQL queries using OPENJSON() that work beautifully in SQL Server Management Studio, but when ran from the Command Prompt using SQLCMD, the queries return 0 rows. Example: INSERT INTO [common].[dbo].[distrcd]…
-1
votes
2 answers

How to get the list of file present in folder and pass the file name dynamically to loop to get the content present in file using batch script

In a directory, I have a multiple .txt file. For example: abc.txt bcd.txt def.txt Now in each of these file I have credential information like: Hostname: 10.87.99.09 username: qwerty password: ytrewq These are the content present in the each of…
saurabh704
  • 63
  • 6
-1
votes
1 answer

Script to change the SQL Default database location in windows

Can someone help me with any script like windows script, BAT script or SQLcmd script for changing the default database location of SQL. I am using Windows
-1
votes
1 answer

How to execute these series of commands in batch file?

How can I execute these series of commands in batch file? It only executes the first line which is the sqlcmd -S ********\SQLEXPRESS -U sa -P *****: sqlcmd -S ********\SQLEXPRESS -U sa -P ***** USE Database GO SELECT * FROM TBLPERSON GO
-1
votes
1 answer

Linux, Bash Script - Loop through sqlcmd connection SELECT results

Linux, Bash Script. I have created a query as below, which works sqlcmd -S [dbname].database.windows.net -d [database name]-U [username ]-P [password] -Q "SELECT * FROM dbo.microiopenvpn WHERE mode='create'" How do I then loop over results. The…
James_Inger
  • 83
  • 2
  • 10