Question

We have an updater application which executes SQL scripts of over 30,000 lines of code on a SQL Server database. In this script is a large number of GO statements. The ADO components which I'm used to using do not support the GO statement. We've always used the OSQL command-line when updating the server, which is tricky to catch the problems (outputs a text file which we then have to parse to look for errors and results). It would be much more ideal if something in Delphi was able to find those GO statements and execute each block at a time.

I do understand that the GO statement is specific to MS tools which are able to execute them. Again the script is over 30,000 lines of code, with at least 500 GO statements. We use the same script in SQL Management Studio and OSQL for the updater. But OSQL just isn't giving us the results we need - it's a sloppy way of executing such a script.

Is there any existing SQL Server compatible tool for Delphi which recognizes GO statements? I'm not talking necessarily about a parser (although I'm sure parsing would be involved), I'm talking about a ready-made utility to execute SQL Scripts which contain GO statements and return the results of each script block.

For example, one block might create a table, and then GO, and then create a stored procedure for that table and another GO. The code is so complex that implementing it in the code like this question is quite out of the question.

UPDATE

I have successfully written a small bit of working code that does this, however it does not take into consideration a GO statement within a comment block.

Code found here

I'm not asking anything further in this question, just thought I'd share where I am with this.

Was it helpful?

Solution

Loading the script into a TStringList would work. looking for the word GO on a line by itself (which avoids embedded text containing GO) as the end of each block or the script itself.

You use the GO line's index - 1 to mark the end of the prior block, and the GO line index + 1 as the start of the next block. If the GO index is equal to the StringList.Count - 1, you've reached then end of the script. You can start a transaction before processing the blocks, and roll that transaction back if an exception is raised.

OTHER TIPS

This is actually a good question, with no easy answer.

It's worth emphasizing this is a Microsoft issue, not SQL, and no Delphi.

This has been discussed many times on SO, but there's no "one size fits all" answer:

My two main suggestions:

  • Read the script file, parse it a line at a time, and run an "execute" after every line containing only "go"

  • Do a ShellExecute() (or equivalent) of SqlCmd.exe with your script and logon parameters.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top