Scripting views / stored procedures in the order of Dependency

Any smart way to script views / stored procedure in the order of dependencie? example:

Views: A, B

Dependencies: A depends on B

By default SMO is scripting them in the alphabetical order so the create view A first and create view B follows it which makes the script un usable as create view A fails as view B doesn't exists in the database.

The way i am using to get around this setuation is, using the bellow code (asume that we are passing a list of View objects:

staticvoid SortViews(Database sourceDB,List<SqlSmoObject> smoObjects)

{

DependencyWalker dw;

DependencyTree dt;

dw =newDependencyWalker(sourceDB.Parent);

dt =newDependencyTree(dw.DiscoverDependencies(smoObjects.ToArray(),true));

smoObjects.Clear();

foreach (DependencyCollectionNode din dw.WalkDependencies(dt))

if (d.Urn.Type =="View")

smoObjects.Add(sourceDB.Views[d.Urn.GetNameForType(d.Urn.Type)]);

}

Is there any other smart / simple way to do this?

[3108 byte] By [LakshmanaKumarK] at [2007-12-24]
# 1

Hello Lakshmana,

Have you had any problems with StackOverflowExceptions when using this method if there is a cyclical dependency in your objects (see my post from yesterday)?

dba72 at 2007-10-8 > top of Msdn Tech,SQL Server,SQL Server SMO/DMO...
# 2
Sorry, i never tried this.
LakshmanaKumarK at 2007-10-8 > top of Msdn Tech,SQL Server,SQL Server SMO/DMO...

SQL Server

Site Classified