Technical Article

Update 2 databases with 1 script

,

This script was developed to manage a pair of databases that were basically the same, but very different in the schema/metadata content. Unfortunately - they both had the same database name, and were in 2 very specific areas (our main SQL Server and a bunch of hardware devices that were running a very watered down version of the database on Embedded Windows, which is basically WinXP and SQL Server 2005 Express). The environment as a whole is on SQL Server 2005 with SP3 (at the time of this offering, SP4 had not been tested for this project, and thus was not a part of any field/dev test).

 

So essentially - the script works as follows:

 

- Each section of the UPDATE script has 2 areas. 1 for one database and 1 for the other (specified with the values of 'c%' and 't%' in my example.

 

- Simply add in your DDL/DML updates in a specific VERSION section that you would like to have identified for that release.

 

- Whether or not a VERSION release area has content in it or not, your database is going to claim to have received that update. In my example you see 2 sections. This was done for demonstrating how easy it is to simply add another VERSION section, and the logic needed in each.

 

Please feel free to modify this as you like to make it work specifically for you, as well as better for best coding practices/logic.

 

I want to thank both Pam Brisjar and Geoff Russell for thier assistance in getting the logic for this put together, correctly.

 

 

* * * * *EDIT* * * * *

I unfortunately did not properly include the 2 tables and 1 DML trigger needed for this script to work. Please see the discussion section for those DDL pieces. Thanks, and my apologies.

--The following script has the ability to update both the Cloud and Client versions of the
--database. Simply apply changes in the appropriate VERSION section for the specific
--database, and then execute it against that database. Thank you G. Russell for the logic rewrite.
-- ~RWY 03/03/2011.

USE 'Database Name'
GO

--DROP and CREATE Function to determine the schema version.
IF EXISTS (SELECT 1 FROM sysobjects WHERE NAME = 'determine_current_schema_version' AND TYPE = 'FN')
    DROP FUNCTION determine_current_schema_version
GO

CREATE FUNCTION determine_current_schema_version() RETURNS NVARCHAR(10)

 BEGIN
 DECLARE @v_curTextValue NVARCHAR(10)
 DECLARE @v_Count INT
 SET @v_Count = (SELECT COUNT(*) FROM dbo.VERSION WHERE TheKey = 'SchemaVersion') 
 IF @v_Count = 1
 BEGIN
 SET @v_curTextValue = (SELECT TheTextValue FROM dbo.VERSION WHERE TheKey = 'SchemaVersion')
 END
 ELSE
 BEGIN
 SET @v_curTextValue = (SELECT TheTextValue FROM dbo.VERSION WHERE TheKey = 'SchemaVersion' AND TheValue=1)
 END
 RETURN @v_curTextValue
 END
GO

--CREATE variable to hold schema version
DECLARE @iSchemaVersion NVARCHAR(10)
SELECT @iSchemaVersion = dbo.determine_current_schema_version()

--@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
--*****************************************************VERSION 001**************************************
--@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IF CONVERT(INT, SUBSTRING(@iSchemaVersion,2,2000)) < 1 AND @iSchemaVersion NOT LIKE 'c%'
BEGIN
        --start updates here

/*/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
 THIS SECTION IS FOR DROP/CREATE/ALTER TABLE & COLUMN CODE ENTRIES
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\*/
/*/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
 THIS SECTION IS FOR INSERTING & UPDATING ROW ENTRIES
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\*/
/*/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
 THIS SECTION IS FOR FOREIGN KEY RELATIONSHIP CODE ENTRIES
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\*/
/*/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
 THIS SECTION IS FOR DML TRIGGERS
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\*/
/*/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
 THIS SECTION IS FOR UNIQUE INDEX CODE ENTRIES
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\*/
--update the schema version
    UPDATE VERSION SET TheTextValue = 't001' WHERE TheKey = 'SchemaVersion' AND TheValue=0
END
        --end updates here

    

IF CONVERT(INT, SUBSTRING(@iSchemaVersion,2,2000)) < 1 AND @iSchemaVersion NOT LIKE 't%'
BEGIN
        --start updates here

/*/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
 THIS SECTION IS FOR DROP/CREATE/ALTER TABLE & COLUMN CODE ENTRIES
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\*/
/*/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
 THIS SECTION IS FOR INSERTING & UPDATING ROW ENTRIES
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\*/
/*/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
 THIS SECTION IS FOR FOREIGN KEY RELATIONSHIP CODE ENTRIES
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\*/
/*/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
 THIS SECTION IS FOR DML TRIGGERS
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\*/
/*/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
 THIS SECTION IS FOR UNIQUE INDEX CODE ENTRIES
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\*/--update the schema version
    UPDATE VERSION SET TheTextValue = 'c001' WHERE TheKey = 'SchemaVersion' AND TheValue=1
END
        --end updates here
--@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
--*****************************************************VERSION 001**************************************
--@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

--@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
--*****************************************************VERSION 002**************************************
--@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IF CONVERT(INT, SUBSTRING(@iSchemaVersion,2,2000)) < 2 AND @iSchemaVersion NOT LIKE 'c%'
BEGIN
        --start updates here

/*/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
 THIS SECTION IS FOR DROP/CREATE/ALTER TABLE & COLUMN CODE ENTRIES
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\*/
/*/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
 THIS SECTION IS FOR INSERTING & UPDATING ROW ENTRIES
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\*/
/*/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
 THIS SECTION IS FOR FOREIGN KEY RELATIONSHIP CODE ENTRIES
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\*/
/*/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
 THIS SECTION IS FOR DML TRIGGERS
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\*/
/*/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
 THIS SECTION IS FOR UNIQUE INDEX CODE ENTRIES
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\*/
--update the schema version
    UPDATE VERSION SET TheTextValue = 't002' WHERE TheKey = 'SchemaVersion' AND TheValue=0
END
        --end updates here

    

IF CONVERT(INT, SUBSTRING(@iSchemaVersion,2,2000)) < 2 AND @iSchemaVersion NOT LIKE 't%'
BEGIN
        --start updates here

/*/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
 THIS SECTION IS FOR DROP/CREATE/ALT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/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\*/
--update the schema version
    UPDATE VERSION SET TheTextValue = 'c002' WHERE TheKey = 'SchemaVersion' AND TheValue=1
END
        --end updates here
--@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
--*****************************************************VERSION 002**************************************
--@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

Rate

2.67 (3)

You rated this post out of 5. Change rating

Share

Share

Rate

2.67 (3)

You rated this post out of 5. Change rating