11

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.

TheQuickBrownFox
  • 10,544
  • 1
  • 22
  • 35

2 Answers2

9

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.

Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448
  • I suppose that settles it. Unless anyone knows a way to do a "GO" whenever a line ends with a semicolon, which would still effectively solve my problem. – TheQuickBrownFox Feb 09 '11 at 15:41
  • 1
    @TheQuickBrownFox - unfortunately not. It's not well documented on the page that rene linked to, but the batch terminator has to be at the start of a line (except for leading whitespace). Of course, if you've got a decent text editor, you can edit your script to replace ";" with ";\nGO\n" – Damien_The_Unbeliever Feb 09 '11 at 15:47
3

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 ;
rene
  • 41,474
  • 78
  • 114
  • 152
  • I've reworded the title so it better fits the full question. Sorry if this makes your answer make less sense. I thought I had changed it when I originally posted the question but it reverted somehow. – TheQuickBrownFox Feb 09 '11 at 14:10
  • @TheQuickBrownFox Can you add a couple of lines that show us how your script looks like? If it has a white line every where maybe -c without anything might do the trick – rene Feb 09 '11 at 14:33
  • 1
    It is just INSERT statements spanning multiple lines and ending with semicolons. Each statement is separated by a single line break (i.e. there are no blank lines). It does not seem to be possible to use the -c parameter with "nothing". – TheQuickBrownFox Feb 09 '11 at 15:01
  • @TheQuickBrownFox try this then -c ; – rene Feb 09 '11 at 15:10
  • Thanks. I tried it. It requires an extra semicolon on its own line to run the command, which is not the desired effect. I think what I want is impossible. – TheQuickBrownFox Feb 09 '11 at 15:39