使用MDX查询执行sql SP时出错

时间:2013-04-26 11:29:00

标签: stored-procedures sql-server-2008-r2 mdx openquery

我正在使用openquery在sql server 2008存储过程中执行MDX查询。

我是按照网站上查看的步骤进行的。当我执行下面的查询时,它的工作正常,但是存储过程会导致错误。

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

并且SP得到以下错误。

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

我在SP中写的代码是

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

我是分析服务和MDX的新手,所以如果你犯了任何错误,请纠正我。

谢谢。

1 个答案:

答案 0 :(得分:1)

您在open_query中丢失了MDX查询周围的引号。尝试更改该行,如下所示:

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