The SQLCMD GO

  • Comments posted to this topic are about the item The SQLCMD GO

  • DOH!

    I read that article right up to the line preceeding the one with the pertinent information.

    Now I must finish my coffee!


    Just because you're right doesn't mean everybody else is wrong.

  • I get an error when I use !!GO:

    'Go' is not recognized as an internal or external command,
    operable program or batch file.

    But i can use:

    GO 2

    I'm using SSMS 17.9 with SS2014

    The three biggest mistakes in life...thinking that power = freedom, sex = love, and data = information.

  • IMHO wrote:

    I get an error when I use !!GO:

    'Go' is not recognized as an internal or external command,
    operable program or batch file.

    But i can use:

    GO 2

    I'm using SSMS 17.9 with SS2014

    Have you enabled SQLCMD mode?


    Just because you're right doesn't mean everybody else is wrong.

  • Yeah, definitely in SQLCMD mode.  I use it alot. I tried

    !! GO

    as well.

    The three biggest mistakes in life...thinking that power = freedom, sex = love, and data = information.

  • IMHO wrote:

    Yeah, definitely in SQLCMD mode.  I use it alot. I tried

    !! GO

    as well.

    Confirmed. I went ahead and tried the same as you. In SSMS you get that error, and it makes a lot of sense, given that GO isn't a valid operating system command. I tried !!DIR, and that returns a directory listing, as expected.

    Using the SQLCMD command line tool is a bit different. The !!GO is simply ignored, as far as I can tell. As a matter of fact, near as I can tell, !! commands are ignored completely by the command line tool.

    So it would appear the documentation is incorrect, or doesn't apply to SQL Server 2016 or earlier, and so the answer I chose was - in practice - correct.

    • This reply was modified 4 years, 1 month ago by  Rune Bivrin.


    Just because you're right doesn't mean everybody else is wrong.

  • Thanks for verifying, Rune!  I wasn't familiar with the

    !!DIR

    command, so thanks for that too.

    The three biggest mistakes in life...thinking that power = freedom, sex = love, and data = information.

  • A semicolumn is missing after the two exclamation mark.

    Here's from BOL:

    Supported SQLCMD Syntax

    The Database Engine Query Editor supports the following SQLCMD script keywords:

    [!!:]GO[count]

     

  • That's a colon, and you're correct. Updated the answers.

  • Carlo Romagnano wrote:

    A semicolumn is missing after the two exclamation mark.

    Here's from BOL:

    Supported SQLCMD Syntax

    The Database Engine Query Editor supports the following SQLCMD script keywords:

    [!!:]GO[count]

    Except that's a colon, not a semi-colon (a bit of a habitual spelling on your behalf, methinks...). And that doesn't help. Colon or no colon, it still doesn't work according to the description.

    Try this

    PRINT 'hej';
    DECLARE @x CHAR(1) = 'X'
    !!:GO
    PRINT @x

    That doesn't yield the error "Must declare the scalar variable "@x".", which it should if !!:GO was equivalent to GO.


    Just because you're right doesn't mean everybody else is wrong.

  • I get the same issue as Rune.  It appears to be ignoring the count as well.

    PRINT 'What Up?'
    GO 3

    Messages:
    Beginning execution loop
    WhatUP?
    WhatUP?
    WhatUP?
    Batch execution completed 3 times.

    PRINT 'What Up?'
    !!:GO 3

    Messages:
    What Up?

    The three biggest mistakes in life...thinking that power = freedom, sex = love, and data = information.

  • Sorry for [semi]colon 🙂

    It's all right for error, because GO is a batch separator: @x is declared in the first batch and not in the second. It's the same as selecting only the last line and execute it (it raises error).

    PRINT 'hej';

    DECLARE @x CHAR(1) = 'X'

    !!:GO

    PRINT @x

     

  • Carlo Romagnano wrote:

    Sorry for [semi]colon 🙂

    It's all right for error, because GO is a batch separator: @x is declared in the first batch and not in the second. It's the same as selecting only the last line and execute it (it raises error).

    PRINT 'hej';

    DECLARE @x CHAR(1) = 'X'

    !!:GO

    PRINT @x

    Well, the issue is that I DON'T get an error, which I should. So !!:GO is quietly ignored. On closer thought, that makes some kind of sense, since in CMD, a colon is used to signify a label, which does nothing on its own.


    Just because you're right doesn't mean everybody else is wrong.

  • From the article linked in the question:

    SQL Server Management Studio uses Microsoft.NET FrameworkSqlClient for execution in regular and SQLCMD mode. When run from the command line, SQLCMD uses the OLE DB provider. Because different default options may apply, it is possible to get different behavior while executing the same query in SQL Server Management Studio SQLCMD Mode, and in the SQLCMD utility.

    So I think those rules only apply to using it from SQLCMD, not specifically from SSMS.

    Fun (aka wierd) things I noticed too while trying this was that it seems like running "!!:<anything>" is completely ignored in SSMS.  For example:

    !!:hello world

    is ignored and you get "Commands completed successfully", which was not what I expected.  I tested that on SQL 2008 R2 SP3.

    So I think Rune Bivrin hit the nail on the head when he said "a colon is used to signify a label, which does nothing on its own".

    The above is all just my opinion on what you should do. 
    As with all advice you find on a random internet forum - you shouldn't blindly follow it.  Always test on a test server to see if there is negative side effects before making changes to live!
    I recommend you NEVER run "random code" you found online on any system you care about UNLESS you understand and can verify the code OR you don't care if the code trashes your system.

Viewing 14 posts - 1 through 13 (of 13 total)

You must be logged in to reply to this topic. Login to reply