Click here to Skip to main content
Click here to Skip to main content

Smo Scripting Based on Dependency

, 19 May 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
Prepare script for specific objects

Introduction

Sometimes, we need to script some particular objects in a database. What we know is we do not know what is the dependency between them.

Do Some Basics

Give a quick look at the following links:

Use the Code

The following snippet is used to generate scripts based on dependency and moreover, it just scripts the mentioned objects only, not all the dependent objects.

Private Sub ScriptObjects(ByVal objval() As String)
        Dim Dw As New DependencyWalker
        Dim db As Database = sm.Database
        Dim ObjTbl As DataTable = db.EnumObjects()
        Dim Selectedurn As New UrnCollection
        Dim DepWalk As DependencyWalker
        Dim DepTree As DependencyTree
        Dim DepColl As DependencyCollection
        Dim Drows() As DataRow
        For Each objName As String In objval
            If objName.Length = 0 Then Continue For
            Drows = ObjTbl.Select("Name ='" + objName + "'")
            If Drows.Length > 0 Then
                Selectedurn.Add(New Urn(Drows(0)("Urn").ToString))
            End If
        Next
        DepWalk = New DependencyWalker(db.Parent)
        DepTree = DepWalk.DiscoverDependencies(Selectedurn, True)
        DepColl = DepWalk.WalkDependencies(DepTree)
        Dim sb As New StringBuilder
        Dim scriptwriter As New Scripter
        scriptwriter.Server = sm.Database.Parent
        Selectedurn.Clear()
        For Each depnd As DependencyCollectionNode In DepColl
            Dim obName As String = depnd.Urn.GetAttribute("Name")
            If objval.Contains(obName.ToUpper) Then
                Selectedurn.Add(depnd.Urn)
                'sm.ScriptObject(sb, Selectedurn, scriptwriter)
                scriptwriter.Script(Selectedurn)
                Selectedurn.Clear()
            End If
        Next
        Using Sr As New StreamWriter(String.Format("{0}\GeneratedScript.sql", sm.GetBaseDirectory()))
            Sr.WriteLine(sb)
        End Using
    End Sub 

Points of Interest

The following bit of code makes me happy to script the specified objects only:

depnd.Urn.GetAttribute("Name") 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Thava Rajan
Software Developer (Senior)
India India
No Biography provided
Follow on   LinkedIn

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141223.1 | Last Updated 20 May 2014
Article Copyright 2014 by Thava Rajan
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid