SSIS – Using SQL Server Analysis Services (SSAS) MDX query into Variable in Execute SQL Task
What I wanted to do was to get the output from my MDX query, and then use this in SSIS and take the output and put it into an SSIS Variable which could then be used later in your SSIS Package.
Example:
· We are going to get the Internet Sales Amounts for 2 days.
o 01 June 2008 – $44,650.70
o 02 June 2008 – $35,496.03
· Then we are going to put the two values into a variable.
· Then we are going to use a Precedence constraint to compare the values, and if the Internet Sales amount is lower when compared then continue onto the next step.
o So if the value on 02 June 2008 is lower than the value on 01 June 2008 continue to the next step.
o So as from the above values it should continue onto the next step.
o If the amount is higher then do not do anything.
· NOTE: The reason that we are using an MDX query is so that we can get our data back as quickly as possible.
You can get a copy of this here: http://msftdbprodsamples.codeplex.com/releases/view/55330
· I used the AdventureWorksDW2012 Data File and AdventureWorks Multidimensional Models SQL Server 2012
1. The first thing that we are going to do below is get our MDX query which connects to the AdventureWorksDW2012 SSAS Cube.
a. This query is for Internet Sales for 01 June 2008
Selectnonempty {[Measures].[Internet Sales Amount]} on 0
,nonempty {(
[Date].[Date].&[20080601]
)} on 1
from [Adventure Works]
i.
b. This query is for Internet Sales for 02 June 2008
Selectnonempty {[Measures].[Internet Sales Amount]} on 0
,nonempty {(
[Date].[Date].&[20080602]
)} on 1
from [Adventure Works]
i.
2. Now create your new SSIS Package.
3. Next we will create our 2 variables that will be used to populate using the MDX queries above.
4. Below is how we have created them:
b. NOTE: We have created them as Int64 due to us getting back a numeric value.
i. And this can also be compared in our Precedence Constraint
5. Next we need to create an OLE DB Connection to our SSAS Cube by doing the following below:
a. Right click in your Connection Managers section and select New OLE DB Connection
b. When the window opens click on New
i. Now from the drop down select the following:
2. NOTE: You have to select the above so that we can connect to our SSAS Instance.
ii. Now once configured you will see the following below:
iii. If you want you can click on Test Connection to ensure that it can connect.
iv. Then click Ok and then Ok again.
c. You should now see the following in your Connection Managers Window:
ii. NOTE: If required you can make this a Project Connection if your SSIS Project is in Project Deployment mode.
6. Now what we are going to do is to assign our MDX Query to our variables using the Execute SQL Task.
7. Drag in an Execute SQL Task and complete the following steps for One Day back
a. As per our example I renamed the Execute SQL Task to the following:
Get Internet Sales Amount for One Day Back into Variable
b. I went into the Execute SQL Task Properties and configured it with the following:
c. We then selected our OLE DB connection that we created in step 5 above:
d. Next under SQLStatement we are then using our MDX query from Step 1a above:
ii. Then click Ok.
e. Next click on the Result Set and click on Add
i. Now where it says Result Name change this to the following:
[Measures].[Internet Sales Amount]
ii. And then ensure that the Variable Name is:
User::InternetSales_OneDayBack
f. Then click Ok.
g. Now right click and select Execute Task to test that the variable will be populated.
8. Drag in an Execute SQL Task and complete the following steps for Two Days back
a. As per our example I renamed the Execute SQL Task to the following:
Get Internet Sales Amount for Two Days Back into Variable
b. I went into the Execute SQL Task Properties and configured it with the following:
c. We then selected our OLE DB connection that we created in step 5 above:
d. Next under SQLStatement we are then using our MDX query from Step 1b above:
ii. Then click Ok.
e. Next click on the Result Set and click on Add
i. Now where it says Result Name change this to the following:
[Measures].[Internet Sales Amount]
ii. And then ensure that the Variable Name is:
User::InternetSales_TwoDaysBack
f. Then click Ok.
g. Now right click and select Execute Task to test that the variable will be populated.
9. Now the final step is to have a next step to go to if the value is lower.
10. For our example we are going to put the next step as a Send Mail Task
a. So we dragged in the Send Mail Task and configured it so that it can send an email if run.
b. Next we dragged the success precedence constraints as shown below:
d. NOTE: The reason it was done in this order is so that we can get both variables populated before we change the constraint options.
e. Now double click on the success precedence constraint that goes from Get Internet Sales Amount for Two Days Back into Variable to the Send Mail Task
f. Now in the Precedence Constraint Editor Window complete the following:
i. Change the Evaluation operation from Constraint to Expression
g. Then where it says Expression click on the Ellipses button.
h. Now in order to check if the value is less to continue we put the following:
@[User::InternetSales_OneDayBack] < @[User::InternetSales_TwoDaysBack]
i. Click on Evaluate Expression to ensure that it is valid.
i. Then Click Ok and Ok again to go back to the Control Flow
11. Now finally run your SSIS Package and it should complete with the following as shown below:
13. NOTE: If you had to change your Precedence Constraint to the following below and re-run the above in order to test if the value was higher you would get the following result:
Hi again! Nice method to execute mdx in SSIS. Never needed it, but usefull to know )
Little remark – at first you say that var1 must be less than var2:
“So if the value on 02 June 2008 is lower than the value on 01 June 2008 continue to the next step.”
But the expression in constraint checks that they are not equal.
Thanks for the reply I have updated the precedence constraint so that it is now as per the example. Thanks for picking that up. I forgot to change it during my testing.
Hi,
Thanks for posting! Do you know if you can do this for a DMV too?
I am trying to check if a trace of a certain ID is in the $SYSTEM.DISCOVER_TRACES table. The result set looks like SQL from inside Management Studio but there is no header. I’m getting a failure in my SSIS task; Possible failure reasons: Problems with the query, “ResultSet” property not set correctly, parameters not set correctly, or connection not established correctly.
Hi there
You can indeed do this for a DMV.
If you can read through this blog post I created. https://gqbi.wordpress.com/2014/05/29/ssas-sql-server-analysis-services-getting-all-partition-information-from-ssas-database-2/
All that you would need to do is change the source query to the DMV that you want to run. As well as changing the destination table.
Please let me know if you get stuck.
Thanks
Gilbert