SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Script difference between two databases


Script difference between two databases

Author
Message
thulani.moyana
thulani.moyana
SSC Journeyman
SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)

Group: General Forum Members
Points: 79 Visits: 64
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.
Jack Corbett
  Jack Corbett
SSCoach
SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)

Group: General Forum Members
Points: 18582 Visits: 14898
The best idea is to get RedGate SQLCompare or Apex SQL's product I think it is SQLDiff.



Jack Corbett

Applications Developer

Don't let the good be the enemy of the best. -- Paul Fleming
At best you can say that one job may be more secure than another, but total job security is an illusion. -- Rod at work

Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
How to Post Performance Problems
Crosstabs and Pivots or How to turn rows into columns Part 1
Crosstabs and Pivots or How to turn rows into columns Part 2
RBarryYoung
RBarryYoung
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14810 Visits: 9518
Are you asking in this forum because you wanted to know how to do this with client code using SMO?

-- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoung
Proactive Performance Solutions, Inc.
"Performance is our middle name."
thulani.moyana
thulani.moyana
SSC Journeyman
SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)

Group: General Forum Members
Points: 79 Visits: 64
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.
RBarryYoung
RBarryYoung
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14810 Visits: 9518
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?

-- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoung
Proactive Performance Solutions, Inc.
"Performance is our middle name."
thulani.moyana
thulani.moyana
SSC Journeyman
SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)

Group: General Forum Members
Points: 79 Visits: 64
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.
Steve Jones
Steve Jones
SSC Guru
SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)

Group: Administrators
Points: 62885 Visits: 19111
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.

Follow me on Twitter: @way0utwest
Forum Etiquette: How to post data/code on a forum to get the best help
My Blog: www.voiceofthedba.com
RBarryYoung
RBarryYoung
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14810 Visits: 9518
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.

-- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoung
Proactive Performance Solutions, Inc.
"Performance is our middle name."
RBarryYoung
RBarryYoung
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14810 Visits: 9518
thulani: How did this work out for you?

-- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoung
Proactive Performance Solutions, Inc.
"Performance is our middle name."
brian.manlove
brian.manlove
Forum Newbie
Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)

Group: General Forum Members
Points: 7 Visits: 8
I'm not the one who asked for the code, but I used it anyway :-)

Worked great. Thanks!
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search