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

SQL batch variable

I need to automate a query so I can perform it on all servers on a network. The thing is more servers are added constantly, so I need to keep it dynamic. On a table I have a list of the currently active servers, but they are repeated several times…
gabrielbaca
  • 123
  • 1
  • 2
  • 10
0
votes
1 answer

Powershell & nice formatting of the output file

I wrote a small powershell script that reads name of sql server instances from text-file then loops them and output results of the query to the text file. foreach ($Instance in Get-Content C:\temp\SQLServers.txt) { $Instance; $File…
Helen Dyakonova
  • 129
  • 1
  • 4
0
votes
0 answers

Sqlcmd Error: Sqlcmd: The -E and the -U/-P options are mutually exclusive

I am trying to execute several sql files located in a folder by executing [Main.bat] which contains below mentioned command- for %%G in (*.sql) do sqlcmd /S [Server] /d [database name] -U [username] -P [Password] -E -i"%%G" pause But facing this…
Praveenks
  • 1,436
  • 9
  • 40
  • 79
0
votes
2 answers

Is there batch script command where I can change variable values in a .sql file?

I am creating a batch file where I am restoring a database from an IP address and then executing a couple .sql files onto the database. In a couple of the .sql files there are variables declared and set. But this process has to be done on many…
Alkey29
  • 189
  • 1
  • 5
  • 20
0
votes
1 answer

cannot make sqlcmd work after runas

I followed a few examples here, but I still can not get my sqlcmd work after runas. I can make it work with two steps: Step one: Use runas to login into a new login and open a command prompt: runas.exe /savecred /user:DOMAIN_NAME\login_name…
dave
  • 296
  • 1
  • 12
  • 26
0
votes
1 answer

Batch file sqlcmd to backup database failing

I'm trying to create a batch file that will execute a sql script which will backup a database. The script looks like this: USE $(DatabaseName); DECLARE @DBName varchar(50), @DateLabel varchar(50), @FileName …
M Kenyon II
  • 4,136
  • 4
  • 46
  • 94
0
votes
1 answer

How to work with (SQLPlus) in SQL Server 2008?

I just finished the conception of a simple Java project and my client wants to use SQL Server as a Database, i downloaded SQL Server 2008, i tried to use SQLPlus to execute the queries but i didn't find any tutorial in the internet on how to use…
Zakaria Marrah
  • 755
  • 6
  • 15
  • 28
0
votes
0 answers

dbmail email from cmd

Is it possible to send email attachment using dbmail from cmd? I'm using a batch file to send email attachment using dbmail but getting an error. Here is my code: sqlcmd -q "EXEC msdb.dbo.sp_send_dbmail @profile_name = 'ProfileName', …
Liton Uddin
  • 85
  • 1
  • 11
0
votes
3 answers

Excel isn't reading sql exported csv properly

I have a batch file that calls sqlcmd to run a command and then export the data as a csv. When viewed in a cell the trasancted date for example shows 35:30.0 but if you click on it the formula bar shows 1/1/1900 2:45:00 PM. I need the full timestamp…
mhopkins321
  • 2,993
  • 15
  • 56
  • 83
0
votes
3 answers

sqlcmd goto isn't quite working—ends script on error

I am using the sqlcmd tool with SQL Server to execute scripts. The scripts add/update records. I need sqlcmd to stop executing and give a non 0 return value if the script throws an error. I thought the following would work but it does not. …
runxc1 Bret Ferrier
  • 8,096
  • 14
  • 61
  • 100
0
votes
1 answer

Split and Export a Database Table Based on Size

All, I have developed an export mechanism to allow the selected tables from a givem database to be exported to .csv files; I can do this using bcp or sqlcmd. For large tables (> 1-2GB), I want to split the tables into several .csv files of a…
MoonKnight
  • 23,214
  • 40
  • 145
  • 277
0
votes
5 answers

Variables declaration in Create Database Script

I have a large database creation query the beginning of which is as follows: USE Master GO IF EXISTS(SELECT * FROM sys.sysdatabases where name = 'MyDatabase') DROP DATABASE MyDatabase GO CREATE DATABASE MyDatabase GO USE MyDatabase GO I want…
NebDaMin
  • 638
  • 2
  • 10
  • 32
0
votes
2 answers

How to convert this sqlcmd script to an Invoke-Sqlcmd script?

I have a SQL script designed to be executed by sqlcmd, and a Command script that executes sqlcmd with the correct parameters. I want to convert the Command script to a PowerShell script that uses Invoke-Sqlcmd instead of sqlcmd. The SQL script, the…
Iain Samuel McLean Elder
  • 19,791
  • 12
  • 64
  • 80
0
votes
2 answers

Powershell : sqlcmd error handling with multiple files

We have folder with multiple sql files. How to catch error with particular file and log it that file name. We are using following script to execute…
Kumar_2002
  • 584
  • 1
  • 5
  • 14
0
votes
1 answer

WiX. Custom Action rum sqlcmd and processing error

I have a custom action
kenai
  • 105
  • 2
  • 8