Question

I am executing MDX query in sql server 2008 stored procedure with openquery.

I jst followed the steps viewed in a website. When i executing the query like below its working fine, but with stored procedure its getting error.

 select * from openquery (TESTLINKSERVER,' SELECT NON EMPTY 
 { [Measures].[Duration], [Measures].[ID] } ON COLUMNS FROM [CUBEDB] ')

and with SP got the below error.

Incorrect syntax near the keyword 'SELECT'.
Incorrect syntax near '{'.

The code I wrote in SP is following

USE [TESTDB]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[123CUBE_SP]

AS

BEGIN

declare @mdx_query as varchar(max), @open_query as nvarchar(max), @linked_server as varchar(max)

set @mdx_query = 'SELECT NON EMPTY { [Measures].[Duration], [Measures].[ID] } ON COLUMNS FROM [CUBEDB]'

set @linked_server = 'TESTLINKSERVER'

set @open_query = 'SELECT * FROM OpenQuery('+@linked_server+','+ @mdx_query +')'

execute sp_executesql @open_query

END

I am new to Analysis service and MDX , so pls correct me if you fing=d any mistake.

Thank You.

Was it helpful?

Solution

You have lost the quotes around your MDX query within the open_query. Try changing the line as follows:

set @open_query = 'SELECT * FROM OpenQuery(' + @linked_server + ',''' + @mdx_query + ''')'
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top