OPENQUERY using MDX

Hi,

I am using MDX query union with sql query. when i use OPENQUERY it's not allow multivalued parameter.i am using this query in SSRS.The parameter also getting from SSRS

My OPENQUERY is like below.

DECLARE @MDXExpression VARCHAR(MAX)
DECLARE @Customerid VARCHAR(10) --multivalue parameter

SELECT @MDXExpression =
'
SELECT {[Measures].[Count]} ON 0,
{[ProcessDate].[Date].[Date]} ON 1
FROM
(SELECT (STRTOSET("[Customer].[CustomerID].&[' + @Customerid +']")) ON 0 FROM
[MailSort])))
'

EXEC ('SELECT * INTO tempdb..##tmpCustomer FROM OpenQuery(LNKSER,''' + @MDXExpression + ''')')

SELECT
    CONVERT(VARCHAR,"[Measures].[Count]") AS count1,
    CONVERT(VARCHAR,"[ProcessDate].[Date].[Date].[MEMBER_CAPTION]") AS PDate
FROM  
    ##tmpCustomer

Thanks in ADVANCE

Paramesh

May 21st, 2013 9:31am

Hi Paramesh,

I've written some code for you. I'm a little bit blind because as is often in the case I don't have access to the cube.

If you have any further issues please create an SQL trace and / or and MDX trace within SQL Server Profiler then cut and paste the relevant text into your MSDN thread; -

USE myDatabase GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO IF EXISTS (SELECT * FROM sys.objects WHERE type = 'P' AND name = 'pExampleOpenQueryMDXCall') DROP PROCEDURE dbo.pExampleOpenQueryMDXCall GO CREATE PROCEDURE dbo.pExampleOpenQueryMDXCall /********************************************************************************************* OBJECT: pExampleOpenQueryMDXCall PARAMETERS: Customerid DESCRIPTION: Example of calling MDX within Stored Procedures RELATED LINKS; Stored Procedures; - http://msdn.microsoft.com/en-us/library/ms190669.aspx Calling MDX Within Stored Procedures; - http://jesseorosz.wordpress.com/2008/02/12/calling-mdx-from-a-sql-server-stored-procedure/ RETURNS: Data Set for User Selected CustomerIds AUTHOR: Kieran Wood EXAMPLE USAGE: exec pExampleOpenQueryMDXCall @CustomerID='[Customer].[CustomerID].[All]', CREATED: 2013-05-21 DATE MODIFIED NAME COMMENTS 07-JAN-2013 Kieran Wood pExampleOpenQueryMDXCall *********************************************************************************************/ @CustomerID varchar(max) ='[Customer].[CustomerID].[All]' AS BEGIN -- VARCHAR(MAX) can store up to 2 GB of data - not just 8000 characters..... -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON; PRINT ' @CustomerID = ' + @CustomerID -- The problem with OpenQuery is that every column has a data type of string, -- including the numerical columns. This means that the data type must be changed if you want -- to perform numerical computations on the figures using T-SQL. So our numerical columns need to be -- wrapped in CAST & CONVERT -- Build SQL, N.B. ; - In open query you can't using dynamic SQL within the open query statement. DECLARE @SQL nvarchar(max) DECLARE @MDX nvarchar(max) SET @MDX = '{[Measures].[Count]} ON COLUMNS, ' SET @MDX = @MDX + ' {[ProcessDate].[Date].[Date].ALLMEMBERS} ' SET @MDX = @MDX + ' DIMENSION PROPERTIES MEMBER_CAPTION, MEMBER_UNIQUE_NAME ON ROWS ' SET @MDX = @MDX + 'FROM ( SELECT ( { ' + @CustomerID + ' } ) ON COLUMNS ' SET @MDX = @MDX + ' FROM [MailSort]) ' PRINT '@MDX = ' PRINT @MDX -- N.B. ; - MDX calls from linked server returns data types as ntext. -- The following code assumes that you have set up a linked server to your Analysis Services instance -- from your Database Engine instance called MailSort_CUBE SET @SQL = 'SELECT DISTINCT CAST ("[Measures].[Count]" AS int) AS MEASURE, ' SET @SQL = @SQL + ' CAST ("[ProcessDate].[Date].[Date].[MEMBER_CAPTION]" as varchar(15)) AS myDate ' SET @SQL = @SQL + 'FROM openquery(MailSort_CUBE, ' + '''' + @MDX + '''' + ')' PRINT '@SQL ' + @SQL EXEC(@SQL) END GO


Free Windows Admin Tool Kit Click here and download it now
May 21st, 2013 1:34pm

Hi Kieran,

Thanks, The Query was excute fine. but i require pass multiple customerid's.Not All option

For Example:

"[Customer].[CustomerID].[00231],
[Customer].[CustomerID].[00232]"

But parameter is not in MDX format.it's SQL format like "'[Customer].[CustomerID].[' & @CustomerID & ']'"

Thanks,

Paramesh

May 21st, 2013 3:38pm

Hi Paramesh, I just used @CustomerID varchar(max) ='[Customer].[CustomerID].[All]' as the default value for the stored procedure. This stored procedure should be able to cope with multiple values in a similar format to the following; -

SELECT [Measures].[Internet Tax Amount] ON COLUMNS, [Date].[Calendar Year] ON ROWS FROM (SELECT ({[Customer].[Customer].&[20075],[Customer].[Customer].&[15568]}) ON COLUMNS FROM [Adventure Works])

So try something similar to ; -

exec pExampleOpenQueryMDXCall @CustomerID='[Customer].[CustomerId].&[1],[Customer].[CustomerId.&[2]'

then tell me what you get (using SQL Server Profiler traces as necessary)

Kind Regards,

K

Free Windows Admin Tool Kit Click here and download it now
May 21st, 2013 3:55pm

Thanks Kieran, it's working. but i use Hierarchy dimension(Region,Name,ID) like [Customer].[CustomerID].[South].[Allien].[00231].

i pass the each value like region,Name,ID from SQL query parameter.

Regards,

Paramesh

May 21st, 2013 4:33pm

Hi Kieran,

Thanks for this useful answer. I have same kind of question.

This works fine when we use [Customer].[Customer].[All]

But I have to pass only few values.

What I am doing is:

I am using sql table for storing the parameter values. If I give [Customer].[Customer].[All] then its works fine because it runs for all the values. But If I give [Customer].[Customer].[100],[Customer].[Customer].[200] then it is throwing an error

The syntax for ',' is incorrect.

It just taking as one string.

Could you please advise on this.

Appreciate all and any help.

Thanks,

Divya

Free Windows Admin Tool Kit Click here and download it now
January 15th, 2014 2:15am

Hi Divya,

You can use [Customer].[Customer].[100] and [Customer].[Customer].[200] in set bracket like

{[Customer].[Customer].[100],[Customer].[Customer].[200]}

Hope this will solve your problem

Happy coding

January 21st, 2014 1:10am

Hi,

Instead, you can implement the same at dataset param. However you are passing value not the complete name like dimension.member.member, so you can use expression for dataset params like below:

="[Dimension].[Attribute].&[" +  Join(Parameters!Param.Value, ", [Dimension].[Attribute].&[") + "]"

In this way you will get the complete value for each member, either you pass single value for multiple values.

And don't prefix anything in your procedure or t-sql code.

Thanks, Madhu

Free Windows Admin Tool Kit Click here and download it now
January 21st, 2014 4:16am

Hi,

This will not work if your passing it in StrtoSet function for multiple values case.

For that you need to use {} at both end like below

="{[Dimension].[Attribute].&[" +  Join(Parameters!Param.Value, ", [Dimension].[Attribute].&[") + "]}"

January 21st, 2014 4:25am

Forgot to mention it. But the overall idea is to get the solution in easy way :)

="{[Dimension].[Attribute].&[" +  Join(Parameters!Param.Value, "], [Dimension].[Attribute].&[") + "]}"

Free Windows Admin Tool Kit Click here and download it now
January 21st, 2014 5:23am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics