Using Dynamic Values in XMLA

, 2011-12-18

Using Dynamic Values in XMLA

XMLA

XMLA

A question was raised on Twitter today via the #sqlhelp hash tag about passing parameter values to XMLA for a backup script. The popular answer at the time I saw it was to dynamiclly create and drop a SQL job to run the script every time you need it to run. I proposed a different solution,

The Question

The following request was sent in a series of tweets:

Is there a way to pass variables between tsql and mdx queries within a sql job to automate #SSAS db backups. #sqlhelp

Correction: Is there a way to pass variables between tsql and XMLA queries within a sql job to automate #SSAS db backups. #sqlhelp

I have the tsql part working which creates the backup syntax, now I need it to execute the XMLA to perform the backups. #sqlhelp

My Proposed Solution

As I stated above, the popular solution was to use dynamic SQL to have it create a job that runs the backup and then delete the job. In my session A DBAs Guide to Administering BI Systems at the PASS Summit 2011, I talked about executing MDX queries via T-SQL across a linked server. This approach works with XMLA as well. I simply need to use dynamic SQL to build the XMLA command and then execute it across the linked server. I set up a test to demonstrate how to do it before recommending it.

I started by using the SSMS GUI to script out the backup command for the Adventure Works DW 2008R2 database in my local SSAS instance. I suspect that adding a date serial to the backup name is likely to be the reason for using this, that is the scenario I used. I then use the EXEC() AT command to execute the string across a linked server (named SSAS in my sample code).

Declare @XMLA nvarchar(1000),
    @DateSerial nvarchar(35);
-- Change date to format YYYYMMDDHHMMSS
Set @DateSerial = Convert(nvarchar, getdate(), 112) +
        Replace(Convert(nvarchar, getdate(), 108), ':', '');
-- Create the XMLA string
Set @XMLA = N'<Backup xmlns="http://schemas.microsoft.com/analysisservices/2003/engine">
  <Object>
    <DatabaseID>Adventure Works DW 2008R2</DatabaseID>
  </Object>
  <File>c:\bak\Adventure Works DW 2008R2_' + @DateSerial + '.abf</File>
</Backup>';
-- Execute the string across the linked server (SSAS)
Exec (@XMLA) At SSAS;

Rate

Share

Share

Rate

Related content

Database Mirroring FAQ: Can a 2008 SQL instance be used as the witness for a 2005 database mirroring setup?

Question: Can a 2008 SQL instance be used as the witness for a 2005 database mirroring setup? This question was sent to me via email. My reply follows. Can a 2008 SQL instance be used as the witness for a 2005 database mirroring setup? Databases to be mirrored are currently running on 2005 SQL instances but will be upgraded to 2008 SQL in the near future.

2009-02-23

1,567 reads

Networking - Part 4

You may want to read Part 1 , Part 2 , and Part 3 before continuing. This time around I'd like to talk about social networking. We'll start with social networking. Facebook, MySpace, and Twitter are all good examples of using technology to let...

2009-02-17

1,530 reads

Speaking at Community Events - More Thoughts

Last week I posted Speaking at Community Events - Time to Raise the Bar?, a first cut at talking about to what degree we should require experience for speakers at events like SQLSaturday as well as when it might be appropriate to add additional focus/limitations on the presentations that are accepted. I've got a few more thoughts on the topic this week, and I look forward to your comments.

2009-02-13

360 reads