More DMOFebruary 25, 2001 In my previous article (Intro to DMO), I described how to use DMO to connect to SQL Server and do some basic tasks, such as performing a backup. This article will introduce some additional methods that are very useful when automating administrative tasks. All code has been tested on SQL 2000, but should work fine with SQL 7. ExecuteImmediate Method The ExecuteImmediate gives you the ability to execute T-SQL or stored procedures from within your DMO script. If you use ExecuteImmediate as a method of a database object, you get the same effect as if you had executed a "Use Database" in QueryAnalyzer. You can also use ExecuteImmediate as a method of the server object, in which case the database is always the master. Assuming you've already established a connection to your server, this sample code will update statistics on all objects in all databases.
ExecuteWithResults Method and the QueryResults
Object ExecuteWithResults works
just like the ExecuteImmediate method, except you have to assign the results of
the method to a QueryResults object. The QueryResults is my least favorite
object. Instead of returning an ADO recordset, or at least a true object that
would support for/each interation, it is essentially an array. To make matters
worse, you have to use different methods to retrieve column values depending on
the column datatype. Still, it is good enough for most admin tasks and allows
you to work solely within DMO without having to have any knowledge of other
object models.
Script Method Think of the scripting
options available in Enterprise Manager. You can reproduce them all in DMO,
plus some! In this example I'm creating one script per database containing all
of it's views. I'm using the appendtofile flag so that each time I script an
object, it doesn't overwrite the previous script. The primaryobject flag is the
one that tells DMO to generate the DDL for the object. This example also makes use
of the CommandShellImmediate method - which directly corresponds to xp_cmdshell.
Take a look also at the nested loops, the outer one for the databases, the
inner for the views - objects make this kind of looping incredibly easy.
ListAvailableSQLServers Method This method returns a NameList object - a collection object in which the members are not strongly typed. In order to iterate the collection using the for/each syntax, you can use a variable of type variant for the member object. In this example I'm showing how you can retrieve the number of databases for each SQL Server that is visible on the network.
Wrap Up Remember, you can use DMO
from any COM compliant language. Swynk reader Gregg Murray writes:
"You can create great administrative web pages that
tell you lots of info about a multitude of jobs, servers, disk space warnings,
etc... All on one freeform ASP page. This gives you the ability to create
really friendly "dashboards" that can help you perform common
administrative tasks from a web page! Ideal alternative to Enterprise
manager, especially when you need to administer your SQL servers over a 28.8
modem connection on RAS!" Excellent point! I haven't yet used DMO with ASP, but as soon as I read that I thought of a project at work that would be perfect for using the two together - I'll let you know how it goes in a future article. |