Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to terminate sqlcmd immediately after execution completed?

I create a process in C# to execute sqlcmd /S <servername> /d <dbname> /E /i to run sql script that create tables, views, stored procedures.

The problem is the process does not terminate after the execution is completed. I want to know how can I terminate sqlcmd immediately after the operation is completed.

Is there any input argument to sqlcmd that I can specify to terminate the process right away or can I do it directly in C#?

Update

This is the C# code that I use to execute the process.

foreach (var file in files) {     ////var begin = DateTime.Now;     ////context.TrackBuildWarning(string.Format("Start exec sql file at {0}.", DateTime.Now));      Process process = new Process();     process.StartInfo.UseShellExecute = false;     process.StartInfo.RedirectStandardOutput = true;     process.StartInfo.RedirectStandardError = true;     process.StartInfo.CreateNoWindow = true;     process.StartInfo.FileName = "sqlcmd.exe";     process.StartInfo.Arguments = string.Format("-S {0} -d {1} -i \"{2}\" -U {3} -P {4}", sqlServerName, databaseName, file, sqlUserName, sqlPassword);     process.StartInfo.WorkingDirectory = @"C:\";     process.Start();     process.WaitForExit(); // if I comment out this code it will execute much faster      ////context.TrackBuildWarning(string.Format("Finished exec sql file at {0} total time {1} milliseconds.", DateTime.Now, DateTime.Now.Subtract(begin).TotalMilliseconds)); } 

As you can see, I have comment that if I remove (or comment) out process.WaitForExit() it will execute A LOT faster.

like image 996
Anonymous Avatar asked Oct 05 '12 03:10

Anonymous


People also ask

How do I exit SQLCMD?

To exit sqlcmd, type EXIT or QUIT at the start of a new line. To clear the statement cache, type :RESET. Typing ^C causes sqlcmd to exit. ^C can also be used to stop the execution of the statement cache after a GO command has been issued.

How do I stop a SQL script execution?

The noexec method Another method that works with GO statements is set noexec on (docs). This causes the rest of the script to be skipped over. It does not terminate the connection, but you need to turn noexec off again before any commands will execute.

How can I tell if SQLCMD is working?

You can test SQLCMD.exe from a Powershell console, running as the Delphix OS user, on the Windows host where Delphix Connector service is running.

What is the purpose of using SQLCMD Q command?

The sqlcmd utility lets you enter Transact-SQL statements, system procedures, and script files through a variety of available modes: At the command prompt. In Query Editor in SQLCMD mode.


1 Answers

Just use -Q (uppercase).

This command selects 'asd' and quits immediately: sqlcmd -S (local) -Q "select('asd')"

like image 165
Filipe Borges Avatar answered Sep 23 '22 21:09

Filipe Borges