database schema migration

  • Dees anyone know of a tool that will create a database schema creation script, from a database that will create all the tables, views, and constraints in the correct order? EM does not seem to do a very good job of creating a schema creation script in the correct order.

    Gregory Larsen, DBA

    If you looking for SQL Server Examples check out my website at http://www.geocities.com/sqlserverexamples

    Gregory A. Larsen, MVP

  • What do you mean with the correct order?

  • SQL Compare from RedGate is decent. Havent tried it on anything reallllly complicated, but so far its done well for me.

    Andy

    http://qa.sqlservercentral.com/columnists/awarren/

  • Basically the views are not created in the correct order. We have views that use views so probably this has something to do with it. So if you have the following a new database:

    Create table a(a int)

    go

    create view vb as select a from a

    go

    create view va as select a from vb

    Then you create the all the view scripts

    builds a script that create view va first, then vb, and the script errors.

    Does RedGate create these views in the right order?

    Gregory Larsen, DBA

    If you looking for SQL Server Examples check out my website at http://www.geocities.com/sqlserverexamples

    Gregory A. Larsen, MVP

  • You can download a trial from their site. Nice guys there. I created a new db and ran your script, then used SQLCompare to generate the following script:

    --

    -- You must execute this SQL statement on

    -- EG\ONE.pubs (EG\ONE.pubs scripted Wednesday, September 25, 2002, 11:23:43 PM)

    -- to make it the same as

    -- EG\ONE.testscript (EG\ONE.testscript scripted Wednesday, September 25, 2002, 11:23:42 PM)

    --

    -- You are advised to check the script manually and run it bit by bit.

    -- Also, BACK UP YOUR DATABASE before running this script

    --

    GO

    IF EXISTS (SELECT * FROM tempdb..sysobjects WHERE id=OBJECT_ID('tempdb..#tmpErrors')) DROP TABLE #tmpErrors

    GO

    CREATE TABLE #tmpErrors (Error int)

    GO

    SET XACT_ABORT ON

    GO

    BEGIN TRANSACTION

    GO

    SET TRANSACTION ISOLATION LEVEL SERIALIZABLE

    GO

    --

    -- Script for dbo.a

    -- Foreign keys etc. will appear at the end--

    PRINT 'Updating dbo.a'

    GO

    CREATE TABLE [dbo].[a]

    (

    [a] [int] NULL

    )

    --

    -- Script for dbo.vb

    -- Foreign keys etc. will appear at the end--

    PRINT 'Updating dbo.vb'

    GO

    SET QUOTED_IDENTIFIER ON

    GO

    IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION

    GO

    IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END

    GO

    SET ANSI_NULLS ON

    GO

    IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION

    GO

    IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END

    GO

    create view vb as select a from a

    GO

    IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION

    GO

    IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END

    GO

    SET QUOTED_IDENTIFIER OFF

    GO

    IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION

    GO

    IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END

    GO

    SET ANSI_NULLS ON

    GO

    IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION

    GO

    IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END

    GO

    --

    -- Script for dbo.va

    -- Foreign keys etc. will appear at the end--

    PRINT 'Updating dbo.va'

    GO

    SET QUOTED_IDENTIFIER ON

    GO

    IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION

    GO

    IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END

    GO

    SET ANSI_NULLS ON

    GO

    IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION

    GO

    IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END

    GO

    create view va as select a from vb

    GO

    IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION

    GO

    IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END

    GO

    SET QUOTED_IDENTIFIER OFF

    GO

    IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION

    GO

    IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END

    GO

    SET ANSI_NULLS ON

    GO

    IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION

    GO

    IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END

    GO

    GO

    IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION

    GO

    IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END

    GO

    IF EXISTS (SELECT * FROM #tmpErrors) ROLLBACK TRANSACTION

    GO

    IF @@TRANCOUNT>0 BEGIN

    PRINT 'The database update succeeded'

    COMMIT TRANSACTION

    END

    ELSE PRINT 'The database update failed'

    GO

    DROP TABLE #tmpErrors

    GO

    Andy

    http://qa.sqlservercentral.com/columnists/awarren/

  • Looks like RedGate might just to the trick, and create the schema correctly.

    Gregory Larsen, DBA

    If you looking for SQL Server Examples check out my website at http://www.geocities.com/sqlserverexamples

    Gregory A. Larsen, MVP

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply