Tuesday, November 16, 2010

Is Incest Thinking Wrong?

Extract your XMLA script database creation in SSAS

I was asked not long ago it was possible to retrieve the script for the creation of AS basis. Of course, Managment Studio lets just do it. But in our case, the creation script should be recovered during backups, so in an automated manner.
For this, there are classes that allow OMA to do so fairly easily. A code like the one below it will give you an example:


srv.Connect ("MyServer \\ MonInstanceAS");
Database db = srv.Databases.FindByName ("mydatabase");
Scripter scripter = new Scripter ();
System . Xml.XmlTextWriter xmlWriter = new System.Xml.XmlTextWriter ("C: \\ \\ MonScript.xml" Encoding.UTF8)
Scripter.WriteCreate (xmlWriter, srv, db, true, true);
xmlWriter . Close ();



The last two parameters of the method WriteCreate of your script are:
  • fullExpanded:
- false: the script does contriendra the creation of the database
- true: it will feature all of your database objects (dimensions, cubes, etc.).
  • AllowOverwrite:
- False: when running the script, if the database exists, it will not be crushed
- true: the reverse

The Scripter object has other methods you can find on the net or in the book Microsoft SQL Server Analysis Services Unleashed.
If ever there who likes to take the lead, you can "parse" the result of a query as XMLA DISCOVER_XML_METADATA which also contains the full description of your base (I say this because this is what I wanted to do at first).

Source:
MS SQL Server Analysis Services Unleashed (Chapter 34, p 694)