SQL Deploy

The SQL Deploy tool is a windows command line tool written using the .NET framework.

It reads an XML document to execute a file only once (schema), or every time (package) the program is run.

It takes in connection info to connect to the database (server, catalog, credentials), and the path to the XML document.

The basic XML layout is:

<Database>
 <DatabaseName>NameOfCatalog</DatabaseName>
 <Schemas>
  <!-- place files that should only be run once in this section -->
  <File>Schemas\SchemaFile001.sql</File>
  <File>Schemas\SchemaFile002.sql</File>
 </Schemas>
 <Packages>
  <!-- place files that will run every time in this section -->
  <File>packages\DeprecatedPackages.sql</File>

  <!-- Views -->
  <!--<File>packages\Views\dbo.View.sql</File>-->

  <!-- Functions -->
  <!--<File>packages\Functions\dbo.function.sql</File>-->

  <!-- Stored Procedures -->
  <File>packages\StoredProcedures\dbo.StoredProc.sql</File>

  <!-- Triggers -->
  <!--<File>packages\Triggers\dbo.Trigger.sql</File>-->

 </Packages>
</Database>

And the basic file structure should look something like:

[Database Directory]
| [Schemas]
| | SchemaFile001.sql
| | SchemaFile002.sql
| [Packages]
| | [Functions]
| | | FunctionFile01.sql
| | [StoredProcedures]
| | | StoredProcFile01.sql
| | [Triggers]
| | | TriggerFile01.sql
| | [Views]
| | | ViewFile01.sql
| | DeprecatedPackages.sql
| DB.xml

And an example SQL Package file would look like this:

--Conditionally Drop The Object
IF EXISTS (SELECT * FROM sys.sysobjects o INNER JOIN sys.schemas s ON o.uid = s.schema_id WHERE o.name = N'StoredProcedure001' AND o.type = 'P' AND s.name = 'dbo')
BEGIN
    PRINT 'Dropping Stored Procedure dbo.StoredProcedure001'
    DROP PROCEDURE dbo.StoredProcedure001
END
GO

PRINT 'Creating Stored Procedure dbo.StoredProcedure001'
GO

--Create The Object
CREATE PROCEDURE dbo.StoredProcedure001
AS
BEGIN
    SET NOCOUNT ON;

    SELECT  *
    FROM    dbo.Table01

END
GO

PRINT 'Stored Procedure dbo.StoredProcedure001 Created'
GO