How do I make an MDX Query, run from inside a stored procedure, using OPENROWSET, dynamic?

Using SQL Server 2014 and I'm trying to merge Analytic data and Relational data for a report. I'm almost there but there are parts that need to be dynamic in the MDX part.

See attached query. I need the DATASOURCE dynamic, the Initial Catalog dynamic, and the year currently hardcoded as 2017 dynamic. I try to replace them with variables and it keeps giving me errors.
SQLMDXQuery.txt
Paul MaurielloSoftware Programmer Developer Analyst EngineerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Pawan KumarDatabase ExpertCommented:
Please use this -

DECLARE @SQL AS VARCHAR(MAX)= ''
DECLARE @Year AS VARCHAR(4) = '2017'
SET @SQL = ' SELECT NON EMPTY {  [Measures].[PnL], 
													      [Measures].[Hire Rate], 
													      [Measures].[TCE], 
													      [Measures].[Offhire Days], 
													      [Measures].[Net Days]  } ON COLUMNS,
											         NON EMPTY { ([Voyage].[Vessel].[Vessel].ALLMEMBERS ) } ON ROWS 
												 FROM ( SELECT ( { [Date].[Year].&'+ @Year +' } ) ON COLUMNS FROM 
																	( SELECT ( {  
																			 [Voyage].[Vessel Tag].&[Atlantis], 
																			  } ) ON COLUMNS 
																		 FROM [cubeVoyageResults])) 
														 WHERE ( [Voyage].[Vessel Tag].CurrentMember, [Date].[Year].&'+ @Year + ')'
EXEC ( 'SELECT * FROM OPENROWSET(''MSOLAP'',''DATASOURCE=SERVER; Initial Catalog=CUBEDB;'' , ' + @SQL + ' ); ' )

Open in new window

0
Paul MaurielloSoftware Programmer Developer Analyst EngineerAuthor Commented:
I tried that and got errors
Msg 156, Level 15, State 1, Line 46
Incorrect syntax near the keyword 'SELECT'.
Msg 102, Level 15, State 1, Line 46
Incorrect syntax near '{'.
Msg 102, Level 15, State 1, Line 52
Incorrect syntax near 'Date'.
Msg 102, Level 15, State 1, Line 54
Incorrect syntax near 'Voyage'.
0
Pawan KumarDatabase ExpertCommented:
Sorry updated code for you-
DECLARE @SQL AS VARCHAR(MAX)= ''
DECLARE @Year AS VARCHAR(4) = '2017'
SET @SQL = ' SELECT NON EMPTY {  [Measures].[PnL], 
													      [Measures].[Hire Rate], 
													      [Measures].[TCE], 
													      [Measures].[Offhire Days], 
													      [Measures].[Net Days]  } ON COLUMNS,
											         NON EMPTY { ([Voyage].[Vessel].[Vessel].ALLMEMBERS ) } ON ROWS 
												 FROM ( SELECT ( { [Date].[Year].&['+ @Year +'] } ) ON COLUMNS FROM 
																	( SELECT ( {  
																			 [Voyage].[Vessel Tag].&[Atlantis], 
																			  } ) ON COLUMNS 
																		 FROM [cubeVoyageResults])) 
														 WHERE ( [Voyage].[Vessel Tag].CurrentMember, [Date].[Year].&['+ @Year + '])'
EXEC ( 'SELECT * FROM OPENROWSET(''MSOLAP'',''DATASOURCE=SERVER; Initial Catalog=CUBEDB;'' , ' + '''' + @SQL + '''' + ')' )

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Paul MaurielloSoftware Programmer Developer Analyst EngineerAuthor Commented:
Thank you that worked awesomely
0
Pawan KumarDatabase ExpertCommented:
welcome. Glad to help as always. :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
SSAS

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.