Script difference between two databases

  • I need to compare two databases and update one of them. Suppose a table "Table A" is available in both databases but some columns are missing in the other table and I want to create an update sql script to update one database.

    I can add columns to the database to update and capture script by changing the execution mode, but the question is:

    How can I clone the column from one table to the other. I need to add the column as is with all constraints(i.e.) the exact copy of the other column.

    Any method of scripting the differences such that the resulting script with update the other table or all objects in the other database thus resulting in exactly the same databases.

    Any help will be appreciated.

  • The best idea is to get RedGate SQLCompare or Apex SQL's product I think it is SQLDiff.

  • Are you asking in this forum because you wanted to know how to do this with client code using SMO?

    [font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
    Proactive Performance Solutions, Inc.
    [/font]
    [font="Verdana"] "Performance is our middle name."[/font]

  • Thanx for the response.

    Yes, I have an application already but I need to take the exact copy of the column from one database to the other. If I have to set scripting options which options.

    The resulting databases should be identical schematically.This must be like cloning the column but somehow change the table to which it belongs and add it to that table and capture the Alter table script to run on the database you want to update.

  • OK, since you did not say "Yes" to SMO, I am going to assume that SQL script is OK.

    Is the column at the end or in the middle of the column list? And is it involved in any indexes or keys?

    [font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
    Proactive Performance Solutions, Inc.
    [/font]
    [font="Verdana"] "Performance is our middle name."[/font]

  • Sorry for not answering straight.

    I'm using SMO and vb.net (Even c# can do). I want to loop through the objects of a table then add the column to the other table if it doesn't exist in the other table.

  • You can search information_schema.columns in both systems, order by name, and the compare to see which columns are in the tables, or which ones aren't.

    Then you can use an ALTER TABLE command (or the SMO equivalent) to change the table.

    I'm not sure why you would put this in an application. Are you changing schema very often or is this just an exercise? The issues are that there could be defaults, rules, constraints, etc. imposed on a column. It's a simple process, but it has a lot of moving parts and it's easy to make mistakes. That's why a tool from someone like Apex or Red Gate is worth a few hundred dollars.

  • OK, well then, the only way that I can see to do it with SMO is through a manual semi-shallow copy, like this:

    Imports Microsoft.SqlServer

    Imports Microsoft.SqlServer.Management

    Imports Microsoft.SqlServer.Management.Common

    Imports Microsoft.SqlServer.Management.Smo

    Imports Microsoft.SqlServer.Server

    Imports system.Data

    Module Util

    Public Sub ColumnDef_Copy(ByVal SourceTable As Smo.Table _

    , ByVal TargetTable As Smo.Table _

    , ByVal ColName As String)

    'copies a column definition from one table to another an then updates the

    ' target tables defintion in the database.

    '

    '2008-05-07 BYoung Created.

    '

    'NOTE: untested.

    Dim SourceCol As Smo.Column = SourceTable.Columns(ColName)

    Dim TargetCol As New Smo.Column(TargetTable, ColName)

    'set the target column's properties using a Shallow Copy:

    With SourceCol

    TargetCol.DataType = .DataType

    TargetCol.Collation = .Collation

    If .Computed Then

    TargetCol.Computed = .Computed

    TargetCol.ComputedText = .ComputedText

    TargetCol.IsPersisted = .IsPersisted

    End If

    TargetCol.Default = .Default

    TargetCol.DefaultSchema = .DefaultSchema

    For Each xp As Smo.ExtendedProperty In .ExtendedProperties

    TargetCol.ExtendedProperties.Add( _

    New Smo.ExtendedProperty(TargetCol, xp.Name, xp.Value))

    Next

    If .Identity Then

    TargetCol.Identity = .Identity

    TargetCol.IdentityIncrement = .IdentityIncrement

    TargetCol.IdentitySeed = .IdentitySeed

    End If

    TargetCol.NotForReplication = .NotForReplication

    TargetCol.Nullable = .Nullable

    TargetCol.RowGuidCol = .RowGuidCol

    TargetCol.Rule = .Rule

    TargetCol.RuleSchema = .RuleSchema

    End With

    'Find the column's position

    Dim ord As Integer = 0

    While SourceTable.Columns.Item(ord) IsNot SourceCol

    ord += 1

    End While

    TargetTable.Columns.Add(TargetCol, ord)

    TargetTable.Alter()

    End Sub

    End Module

    You will have to test this yourself.

    [font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
    Proactive Performance Solutions, Inc.
    [/font]
    [font="Verdana"] "Performance is our middle name."[/font]

  • thulani: How did this work out for you?

    [font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
    Proactive Performance Solutions, Inc.
    [/font]
    [font="Verdana"] "Performance is our middle name."[/font]

  • I'm not the one who asked for the code, but I used it anyway 🙂

    Worked great. Thanks!

  • Glad I could help, brian.

    [font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
    Proactive Performance Solutions, Inc.
    [/font]
    [font="Verdana"] "Performance is our middle name."[/font]

  • Hi Barry,

    Can you send me this code? Id love to try it out!

    Thanks

    G

  • Thank you!!!!

Viewing 13 posts - 1 through 12 (of 12 total)

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