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

What is the meaning of exit code -100 from sqlcmd

I have a C# console app, which call sqlcmd utiltiy, and get the exit code as -100. Code looks like below; private int RunSP() { int exitCode = 1; Log.Log("Creating Stored Procedure: " + Config.ExternalSPname); …
Joe Park
  • 77
  • 1
  • 6
0
votes
1 answer

How to run a SQL Server 2008 R2 Database Script using sqlcmd in C#?

i am new to run SQL Scripts using sqlcmd in C#. i saw some code in Internet but i am not understanding how it Works. string path = string.Empty; OpenFileDialog opd = new OpenFileDialog(); opd.Filter = "sql files|*.sql"; …
Anjali
  • 1,680
  • 4
  • 26
  • 48
0
votes
3 answers

How do i remove the header Dashed underline from a file generated by SQLCMD in a batch file?

How do i remove the header underline from a file generated by SQLCMD in a batch file? I am trying to automate a process that puts a file in a directory to be processed by another app and it chokes on the dashed line under the header Example: 1.…
JerryG
  • 3
  • 1
  • 3
0
votes
1 answer

Write a batch to check and return values from SQL and then run scripts determined by the return

Is it possible to write a batch file that will check and return a value from SQL and run a particular set of scripts depending of the return value from SQL. For example, If this returns a version 5 value: sqlcmd -U *** -P *** -S127.0.0.1 -Q…
boomcubist
  • 770
  • 1
  • 7
  • 15
0
votes
1 answer

SELECT OBJECT_DEFINITION(object_id) FROM sys.objects returns NULL for Tables only

Using the following sp I plan to export my object definitions to text files, and then import them into SVN. The sp works fine for stored procedures and views. However, when used for user tables it returns me a correctly named file for each table,…
0
votes
1 answer

csv output from windows batch + sqlcmd only returns first column

i have looked all over the internet and cant seem to find a solution to this problem. i am trying to output query results as a CSV through using a combination of sqlcmd and windows batch. here is what i have so far: sqlcmd.exe -S %DBSERVER% -U…
0
votes
2 answers

Need to learn how to use SQLCMD to export a query from SAP B1

I use SAP B1 at work and want to export a CSV file in a batch file so that I can schedule the export every 15 minutes to be uploaded to a website. This is what I have at the moment: SQLCMD -S SERVER-VMSQL -d SBO_COMPANYNAME -U sa -P adminpassword -Q…
Rick
  • 45
  • 7
0
votes
1 answer

Can you use SQLCMD to generate scripts from 2012 to run in 2008?

I need to take some databases from 2012 back to 2008 and I have done most using the generate scripts method (select 2008 as one of the options and schema and data). When I hit a large database, I get an error about Insufficient Memory to continue…
0
votes
0 answers

SQL Distinct in INNER JOIN

I´ve to create a SELECT over many tables. The output has to be specified as it is (output via SQLCMD in a file) The problem is, that there are too many lines in the output. the code (from MS SQL Server Management Express): SELECT DISTINCT …
0
votes
2 answers

Running SQL script script/s on multiple databases at the same time (Parallel execution)

I currently use sqlcmd to execute scripts on a database within my deployment process. Now with multiple database one solution is to use this very script in a batch or foreach database. The problem with this, it applies the script to the database one…
Geddon
  • 1,266
  • 1
  • 11
  • 31
0
votes
1 answer

Executing batch with parameters using Process

I am trying to execute a batch file from C# code using Process class. Batch gets to parameters, passed as quoted strings into ProcessStartInfo.Arguments. Everything is working great, besides the fact that when batch is executed its pre-pended with a…
Sean Feldman
  • 23,443
  • 7
  • 55
  • 80
0
votes
1 answer

How can I make a win32 command (sqlcommand) print its output to the console in PowerShell

I have a script that eventually calls this line & sqlcmd.exe -S $DbHost -d $DbSchema -Q "do some crazy db change here" Where "do some crazy db change here" will eventually be replaced by a dynamic bit of SQL/script. When I run this whether it…
benstpierre
  • 32,833
  • 51
  • 177
  • 288
0
votes
1 answer

How do I restore only data and log files via script, ignoring other files

I'm super lazy so I'm spending hours writing a batch file to handle restoring a database to a sql server and making a whole bunch of changes to it after that happens. Different people send me the databases that I'm using. I only need the database…
Konraden
  • 35
  • 4
0
votes
1 answer

Loop through a list of databases and run SQL file(s)

I have a number of databases on which I should run the same SQL file. The database names can be found in a table in another database. I was wondering if I can automate this somehow to avoid changing database manually and running the script. I'm…
Azimuth
  • 2,599
  • 4
  • 26
  • 33
0
votes
2 answers

Cannot create a Powershell script with both Invoke-SqlCmd and Invoke-RestMethod?

Powershell v3 adds Invoke-WebRequest and Invoke-RestMethod, but I cannot get Invoke-SqlCmd to work from that version. If I drop back to Powershell v2, I can Invoke-SqlCmd, but now Invoke-WebRequest and Invoke-RestMethod are no longer available. I…