Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Script difference between two databases Expand / Collapse
Author
Message
Posted Thursday, November 6, 2008 12:48 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, November 4, 2009 1:35 AM
Points: 19, 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.
Post #597928
Posted Thursday, November 6, 2008 10:52 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 11:11 AM
Points: 10,282, Visits: 13,264
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

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
Post #598386
Posted Thursday, November 6, 2008 11:49 AM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Wednesday, October 15, 2014 9:14 AM
Points: 9,294, Visits: 9,483
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."
Post #598427
Posted Thursday, November 6, 2008 10:55 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, November 4, 2009 1:35 AM
Points: 19, 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.
Post #598675
Posted Friday, November 7, 2008 6:01 AM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Wednesday, October 15, 2014 9:14 AM
Points: 9,294, Visits: 9,483
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."
Post #598844
Posted Friday, November 7, 2008 6:10 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, November 4, 2009 1:35 AM
Points: 19, 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.
Post #598853
Posted Friday, November 7, 2008 6:46 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: Administrators
Last Login: Yesterday @ 12:34 PM
Points: 31,181, Visits: 15,626
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
Post #598887
Posted Friday, November 7, 2008 8:08 AM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Wednesday, October 15, 2014 9:14 AM
Points: 9,294, Visits: 9,483
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."
Post #598977
Posted Saturday, March 28, 2009 11:44 AM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Wednesday, October 15, 2014 9:14 AM
Points: 9,294, Visits: 9,483
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."
Post #685596
Posted Wednesday, December 22, 2010 3:00 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, March 23, 2011 8:12 AM
Points: 1, Visits: 8
I'm not the one who asked for the code, but I used it anyway

Worked great. Thanks!

Post #1038521
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse