I have an application which will manage releases of DDL changes and TSql Executable statements to a SQL server database.
Basic workflow: Developer submits .sql file, file is collected, SQL reviewed and assigned a step in a release cycle, release is executed in the database using a stored procedure and it will cycle through the steps executing the SQL within a single SQL transaction using sp_EXECUTESQL. If any errors occur the transaction does not commit the DDL changes. This process manages SQL from a single database to multiple database on the same instance.
The problem I have is that when the SQL scripts are submitted they contain “GO” statements which sp_EXECUTESQL does not support and throws the “Incorrect syntax near ‘GO'” error. I can split and break up most transactions by parsing on the ‘GO’ keyword but this will not work for items that are in other databases. Once I have an ALTER of some sort against another database I need the ‘GO’. e.g. the following hase ot be executed together, cannot be split and executed as two statements:
USE [MyDatabaseOtherThanOneIAmExecutingFrom]
GO
Alter PROCEDURE [dbo].[DoSomething]
...
Syntactically the following statement won’t work so requesting the dev’s to change their sql to prefix with a db name would only cover non DDL SQL:
Alter PROCEDURE [MyDatabaseOtherThanOneIAmExecutingFrom].[dbo].[DoSomething]
Original requirement was to stay within the database to perform these deployment actions so writing a short one off application to execute the batches from .Net using SqlCommand was not an option.
Is there another option to handle this within the database or do I need to extend outside and create an application to manage the SQL steps execution?
You can nest sp_executesql calls. It’s a bit ugly, but it works, and allows you to execute DDL against other databases: