![]() |
|
|
|
By: Daniel Calbimonte | Read Comments (6) | Related Tips: > Analysis Services Development |
Sometimes it is necessary create SSAS partitions for multidimensional databases using dynamic values like the current date and time. Sometimes we need to create SSAS cube partitions daily using the current date. Usually these automation tasks in SSAS can be solved with AMO (Analysis Management Objects), but AMO requires programming skills that usually DBAs do not have (or do not like). This tip is for SSAS Administrators who do not like to program with AMO, but they need to automate their admin tasks with T-SQL.
In this sample we are going to create a partition according to the current date. For example, if today is November 2, 2012 the T-SQL script will create a partition named Internet_Sales_2012. If it were the year 2013, the partition name would be Internet_Sales_2013. The idea is to create partitions automatically using XMLA and variables.
The scripts used in this example can be downloaded here.

For this sample, we are going to download and install the multidimensional database Adventureworks Multidimensional Model 2012 (you can apply this tip to SQL 2008 or 2005 as well): http://msftdbprodsamples.codeplex.com/downloads/get/258486
Note: on my 64 bit machine it was necessary to install the Oledb Provider for 64 bit for Analysis Services: X64 Package (SQL_AS_OLEDB.msi)
In this tip, we are going to create a linked server in SQL Server to the Analysis Services database. After creating the linked server we are going to run a XMLA script with variables using T-SQL and XMLA. For this purpose, make sure that you have the above requirements installed.



USE [master] GO /****** Object: LinkedServer [SSAS] Script Date: 14/09/2012 04:28:40 p.m. ******/ EXEC master.dbo.sp_addlinkedserver @server = N'SSAS', @srvproduct=N'', @provider=N'MSOLAP', @datasrc=N'localhost' /* For security reasons the linked server remote logins password is changed with ######## */ EXEC master.dbo.sp_addlinkedsrvlogin @rmtsrvname=N'SSAS',@useself=N'False',@locallogin=NULL,@rmtuser=NULL,@rmtpassword=NULL GO EXEC master.dbo.sp_serveroption @server=N'SSAS', @optname=N'collation compatible', @optvalue=N'false' GO EXEC master.dbo.sp_serveroption @server=N'SSAS', @optname=N'data access', @optvalue=N'true' GO EXEC master.dbo.sp_serveroption @server=N'SSAS', @optname=N'dist', @optvalue=N'false' GO EXEC master.dbo.sp_serveroption @server=N'SSAS', @optname=N'pub', @optvalue=N'false' GO EXEC master.dbo.sp_serveroption @server=N'SSAS', @optname=N'rpc', @optvalue=N'true' GO EXEC master.dbo.sp_serveroption @server=N'SSAS', @optname=N'rpc out', @optvalue=N'true' GO EXEC master.dbo.sp_serveroption @server=N'SSAS', @optname=N'sub', @optvalue=N'false' GO EXer.dbo.sp_serveroption @server=N'SSAS', @optname=N'connect timeout', @optvalue=N'0' GO EXEC master.dbo.sp_serveroption @server=N'SSAS', @optname=N'collation name', @optvalue=null GO EXEC master.dbo.sp_serveroption @server=N'SSAS', @optname=N'lazy schema validation', @optvalue=N'false' GO EXEC master.dbo.sp_serveroption @server=N'SSAS', @optname=N'query timeout', @optvalue=N'0' GO EXEC master.dbo.sp_serveroption @server=N'SSAS', @optname=N'use remote collation', @optvalue=N'true' GO EXEC master.dbo.sp_serveroption @server=N'SSAS', @optname=N'remote proc transaction promotion', @optvalue=N'true' GO
In a transactional database, to create objects you use T-SQL. In multidimensional databases you use XMLA. XMLA is a XML extension used in Analysis Services to create objects. In this example we are going to generate an XMLA script and then modify it to accept dynamic parameters.



The code generated is as follows and you can see where I highlighted the sections we want to make dynamic:

DECLARE @myXMLA nvarchar(max), @value nvarchar(30), @date varchar(8), @year nvarchar(4) SET @date=FORMAT (getdate(), 'yyyyMMdd') SET @year=FORMAT (getdate(), 'yyyy') SELECT @value='Internet_Sales_'+FORMAT (getdate(), 'yyyy') SET @myXMLA =N'
The code above updates the variable @date with the current date in the format yyyyMMdd. The @year variable stores the current year.

Exec (@myXMLA) At SSAS;

The code now generates a partition according to the current date. In this example we created a partition according to the current year. We used T-SQL to create the variables and get the current year. Then we concatenate the XMLA code with the variables to create partitions dynamically.P>
In this section you can download the following components:
| Wednesday, April 03, 2013 - 11:55:52 AM - Olawale | Read The Tip |
|
Hi, Thanks for this Tutoria but i would like to ask this question.....Is it possible to automate the code using SQL agent? Thanks |
|
| Wednesday, April 03, 2013 - 3:02:22 PM - Yipi | Read The Tip |
|
Thanks for your post. One question though: how to delete the partition first if it already exists? |
|
| Wednesday, April 03, 2013 - 8:59:02 PM - Daniel Calbimonte | Read The Tip |
|
Sure, it is just a T-SQL. You can run it in an agent. In fact that's the reason to use it. To delete the partition in the management studio right click on the partition and press delete. |
|
| Sunday, April 07, 2013 - 9:10:32 AM - Olawale | Read The Tip |
|
Thank You so muchfor the reply, really helps alot.............I do still have one more questionthough. Please is there a way to load data into cube which is developed without a datasource but based on a particular template? For instance, if i build a cube based on the Enterprised edition of adventureworks template, how to i load the data into the cube and dimension... Thanks once again
Regards Olawale |
|
| Monday, April 08, 2013 - 9:27:28 PM - Daniel Calbimonte | Read The Tip |
|
I used the Adventureworks database as datasource. I do not usually work with the templates. In this sample I have downloaded the adventureworks database. |
|
| Friday, April 12, 2013 - 10:51:53 AM - Elena | Read The Tip |
|
Thank you for an excellent article and very detailed accurate step descriptions. It helped me to create code to automate SSAS tabular databases backups in less than 10 minutes. |
|
|
privacy | disclaimer | copyright | advertise | about authors | contribute | feedback | giveaways | user groups Some names and products listed are the registered trademarks of their respective owners. Edgewood Solutions LLC | MSSharePointTips.com | MSSQLTips.com |