5

I am concatenating many sql statements and am running into the following error. "Incorrect syntax near GO" and "Incorrect syntax near "- It seems that when i delete the trailing space and the go and the space after the go, and then CTRL+Z to put back the GO this makes the error go away? its pretty weird why?? How could I code it in Python, thanks

')
END TRY
BEGIN CATCH
print ERROR_MESSAGE()
END CATCH
GO
uniXVanXcel
  • 807
  • 1
  • 10
  • 25
  • http://stackoverflow.com/a/25681013/5552667 – ZLK Apr 26 '17 at 04:27
  • 1
    `GO` is not SQL. Replace `GO` with `;`. From [Microsoft Docs](https://learn.microsoft.com/en-us/sql/t-sql/language-elements/sql-server-utilities-statements-go)`GO is not a Transact-SQL statement; it is a command recognized by the sqlcmd and osql utilities and SQL Server Management Studio Code editor.` – bansi Apr 26 '17 at 04:27
  • it does not work already tried. thanks.. sometimes you do need the GO anyways, but yea semicolon does not do the trick.thnk you – uniXVanXcel Apr 26 '17 at 04:28
  • 1
    check [Executing batch T-SQL Scripts containing GO statements](https://smehrozalam.wordpress.com/2009/05/12/c-executing-batch-t-sql-scripts-with-go-statements/) this is C# you can use the logic though. – bansi Apr 26 '17 at 04:35
  • @bansi actually i undestand now your reference. but here is the thing, i actually am creating a concatenation of multiple sql files into a single Main one. then iam going to sql server to execute that file. i am not certain how that would help unless i had a loop in sql server which would unpack each chunk of Go sql statements one after the other. u see my point? – uniXVanXcel Apr 26 '17 at 05:24
  • The `GO` statement will only work if you execute the sql from Management Studio. Management studio treats `GO` as an end of block. – bansi Apr 26 '17 at 06:35
  • yes that's what I am doing – uniXVanXcel Apr 26 '17 at 06:36

1 Answers1

10

As already mentioned in comments, GO is not part of the SQL syntax, rather a batch delimiter in Management Studio.

You can go around it in two ways, use Subprocess to call SqlCmd, or cut the scripts within Python. The Subprocess + SqlCmd will only really work for you if you don't care about query results as you would need to parse console output to get those.

I needed to build a database from SSMS generated scripts in past and created the below function as a result (updating, as I now have a better version that leaves comments in):

def partition_script(sql_script: str) -> list:
    """ Function will take the string provided as parameter and cut it on every line that contains only a "GO" string.
        Contents of the script are also checked for commented GO's, these are removed from the comment if found.
        If a GO was left in a multi-line comment, 
        the cutting step would generate invalid code missing a multi-line comment marker in each part.
    :param sql_script: str
    :return: list
    """
    # Regex for finding GO's that are the only entry in a line
    find_go = re.compile(r'^\s*GO\s*$', re.IGNORECASE | re.MULTILINE)
    # Regex to find multi-line comments
    find_comments = re.compile(r'/\*.*?\*/', flags=re.DOTALL)

    # Get a list of multi-line comments that also contain lines with only GO
    go_check = [comment for comment in find_comments.findall(sql_script) if find_go.search(comment)]
    for comment in go_check:
        # Change the 'GO' entry to '-- GO', making it invisible for the cutting step
        sql_script = sql_script.replace(comment, re.sub(find_go, '-- GO', comment))

    # Removing single line comments, uncomment if needed
    # file_content = re.sub(r'--.*$', '', file_content, flags=re.MULTILINE)

    # Returning everything besides empty strings
    return [part for part in find_go.split(sql_script) if part != '']

Using this function, you can run scripts containing GO like this:

    import pymssql

    conn = pymssql.connect(server, user, password, "tempdb")
    cursor = conn.cursor()
    for part in partition_script(your_script):
        cursor.execute(part)

    conn.close()

I hope this helps.

Slawomir Szor
  • 323
  • 3
  • 9