How to Script Replication objects using Replication Management Objects (RMO)

  • I need to script to file all the statements necessary to re-create existing Publications, and to delete existing publications. I had this working in SQL 2000 using DMO, I am struggling a bit with the new RMO. My problem is with the ScriptOptions. I need one file for each publication that has the sp_addpublication, the snapshot, the add article and add subscription. The exact way is it produced with the GUI.

    here is what I have so far. I siumply get the ap_addpublication line with nothing else. I have tried the IncludeAll option and I get nothing.

    ' Create connection to local server

    Dim ServerConnection = New ServerConnection("MyServer")

    ''Dim File As File

    Try

    ' connect to publisher

    ServerConnection.LoginSecure = True

    ServerConnection.Connect()

    Dim ReplicationServer = New ReplicationServer(ServerConnection)

    Dim ReplicationDatabases = ReplicationServer.ReplicationDatabases

    Dim ReplicationDatabase As ReplicationDatabase

    Dim TransactionalPublication As TransPublication

    For Each ReplicationDatabase In ReplicationDatabases

    If ReplicationDatabase.HasPublications Then

    For Each TransactionalPublication In ReplicationDatabase.TransPublications

    FileName = "c:\temp\" & TransactionalPublication.Name & ".sql"

    File.WriteAllText(FileName, TransactionalPublication.Script(ScriptOptions.Creation))

    Next

    End If

    Next

    Catch ex As Exception

    Finally

    ServerConnection.Disconnect()

    End Try

  • How many do you have in total? Have you tried to use the GUI for just one, to see what scripts you can extract? It could be that there's a naming convention that's in error, which prevents the other scripts from being extracted? Or you may need to explicitly pull each of the different stored procedures with a separate line of code?

  • I have multiple publications some are Transactional and some are Merge replication. I could manually script from SQL Management Studio. But I want to automate the process because there are multiple replication setup around 50+ in production environment. As part of a Disaster plan I want to script it out every day and store the script in a network share to deployed in DR site.

    Thanks

  • me_balaji (8/25/2008)


    I need to script to file all the statements necessary to re-create existing Publications, and to delete existing publications. I had this working in SQL 2000 using DMO, I am struggling a bit with the new RMO. My problem is with the ScriptOptions. I need one file for each publication that has the sp_addpublication, the snapshot, the add article and add subscription. The exact way is it produced with the GUI.

    here is what I have so far. I siumply get the ap_addpublication line with nothing else. I have tried the IncludeAll option and I get nothing.

    ' Create connection to local server

    Dim ServerConnection = New ServerConnection("MyServer")

    ''Dim File As File

    Try

    ' connect to publisher

    ServerConnection.LoginSecure = True

    ServerConnection.Connect()

    Dim ReplicationServer = New ReplicationServer(ServerConnection)

    Dim ReplicationDatabases = ReplicationServer.ReplicationDatabases

    Dim ReplicationDatabase As ReplicationDatabase

    Dim TransactionalPublication As TransPublication

    For Each ReplicationDatabase In ReplicationDatabases

    If ReplicationDatabase.HasPublications Then

    For Each TransactionalPublication In ReplicationDatabase.TransPublications

    FileName = "c:\temp\" & TransactionalPublication.Name & ".sql"

    File.WriteAllText(FileName, TransactionalPublication.Script(ScriptOptions.Creation))

    Next

    End If

    Next

    Catch ex As Exception

    Finally

    ServerConnection.Disconnect()

    End Try

    Try calling "TransactionalPublication.LoadProperties" BEFORE scripting!


    * Noel

  • Thanks for a quick response.

    I tried both TransactionalPublication.Load() and TransactionalPublication.LoadProperties. No luck.

    I still couldn't figure out a easy way to do it.

    Thanks

  • How is scriptoptions defined? That may give you a key, if you can do something 'other' than all - for instance, list each component separately in the central part of your code? Not as 'simple' or as 'elegant' as you might like, but certainly explicit! (I haven't used VB in 5 years, so I'm rusty)

  • I treid all the options available in scriptoptions. Only EnableReplicationDB or Creation works. All others just return me a blank space. No where I could find a reference or example. Please help.

  • I finally found the solution by myself.

    ScriptOptions.Creation Or ScriptOptions.IncludeAll Xor ScriptOptions.IncludeReplicationJobs

    do the trick.

    TransactionalPublication.Script(ScriptOptions.Creation Or ScriptOptions.IncludeAll Xor ScriptOptions.IncludeReplicationJobs)

    generates the script. I was missing bitwise operators (Or /Xor).

    Thanks everyone for your time.

  • Thanks for posting the solution back.


    * Noel

  • That was for Publications and Subscriptions..do you know the way to do it for Distributor and Publishers

Viewing 10 posts - 1 through 9 (of 9 total)

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