Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you make SQLCMD immediately run each statement in a script without the use of "GO"?

When running a script in SQLCMD, is there a setting, parameter or any other way of making it effectively run a GO command at the end of each SQL statement?

This would prevent the need to insert periodic GO commands into very large scripts.

like image 956
TheQuickBrownFox Avatar asked Feb 09 '11 11:02

TheQuickBrownFox


People also ask

Do you have to use go in SQL?

In the command line, you need to use GO because that way, you know that the T-SQL statement ended and you know that you can execute it. For example, this T-SQL query will show the databases. The query requires a GO at the end to inform to sqlcmd that it is the end of the batch.

What is the purpose of using the SQLCMD command?

The sqlcmd utility is a command-line utility for ad hoc, interactive execution of Transact-SQL statements and scripts and for automating Transact-SQL scripting tasks.


2 Answers

Hmmm, I guess this is not going to help but is an answer to your question:

http://msdn.microsoft.com/en-us/library/ms162773.aspx

-c cmd_end Specifies the batch terminator. By default, commands are terminated and sent to SQL Server by typing the word "GO" on a line by itself. When you reset the batch terminator, do not use Transact-SQL reserved keywords or characters that have special meaning to the operating system, even if they are preceded by a backslash.

Especially the last sentence sounds daunting....

If all your lines end in ; and there is no ; any where else (in text fields for example) try

sqlcmd -c ;
like image 58
rene Avatar answered Sep 20 '22 03:09

rene


No.

SQLCMD would have to implement a full T-SQL parser itself in order to determine where the statement boundaries are. Whereas as it currently exists, all it has to do is find lines with "GO" on them.

Determining statement boundaries can be quite tricky, given that ";" terminators are still optional for most statements.

like image 31
Damien_The_Unbeliever Avatar answered Sep 18 '22 03:09

Damien_The_Unbeliever