SQL Server attempts to prepare the remote server call before actually executing it. Prepare on AnalysisServices results in a Prepare call to your stored procedure (a regular call with Context.ExecuteForPrepare == true). Analysis Services will fail to prepare your stored procedure unless it is marked as Safe for prepare
You should add a reference to the Microsot.AnalysisServices.AdomdServer.dll assembly in your stored procedure, then decorate the actual method with the [SafeToPrepare(true)] attribute.
This informs Analysis Services that it is "safe" to execute a Prepare invocation over the method (i.e. will not have any side effects)
If the implementation of the stored procedure is trivial and not time consuming, then that is all you need to do.
If the implementation is potentially time consuming (say, constructs a large table withc omplex calculations and returns that table as a rowset) then your stored procedure's code should start with something like:
[SafeToPrepare(true)]
DataTable MyMethod()
{
DataTable myTable = new DataTable();
// Build table metadata here
.AddColumn(...)
....
if( Context.ExecuteForPrepare )
{
// return an empty table withthe CORRECT schema
return myTable;
}
// perform calculations and add rows
...
// return the actual populated table
return myTable
}
Both the SafeToPrepareAttribute and Context are defined in Microsoft.AnalysisServices.AdomdServer.dll