2

I am trying to use SAP OLAP BAPI for a very simple task. I want to connect to a SAP BW server, send an MDX query, get the result and disconnect. While I seem to have no problems connecting and disconnecting from the server, sending a query and retrieving results seem to be rather non-trivial tasks that I am seeking help with.

According to the SAP documentation, I need to use MDDataSetBW object, by first creating the query object using CreateObject and then retrieving the results using GetAxisInfo and GetCellData.

Right now I am stuck at the very first step of just creating the query object. The documentation states that CreateObject has one import parameter CommandText and two export parameters DataSetID and Return. The problem is that according to SAP Documentation CommandText parameter is a table with a single column called LINE.

So basically it's just an MDX query, with each line of the query being a value of the LINE column of each row of this table. The question is how do I pass it? Can anyone point me to a C++ example code that does something like this?

Here's my current prototype code:

#include "stdafx.h"

using namespace std;

RFC_HANDLE Logon() 
{ 
    RFC_CONNOPT_R3ONLY conn_options;
    conn_options.hostname = SAP_LOGON::Hostname; 
    conn_options.sysnr = 0; 
    conn_options.gateway_host = nullptr; 
    conn_options.gateway_service = nullptr;

    RFC_OPTIONS options;
    options.mode = RFC_MODE_R3ONLY; 
    options.destination = SAP_LOGON::Destination; 
    options.client = SAP_LOGON::Client; 
    options.user = SAP_LOGON::User; 
    options.password = SAP_LOGON::Password; 
    options.language = SAP_LOGON::Language; 
    options.trace = 0; 
    options.connopt = &conn_options;

    RFC_HANDLE hConn = RfcOpen(&options);

    if (hConn == RFC_HANDLE_NULL)
        ErrorHandler(_T("RfcOpen"));

    return hConn;
} 

void MakeStringParam(RFC_PARAMETER& param, rfc_char_t* name, rfc_char_t* buf, unsigned long size)
{
    param.name = name;
    param.nlen = (unsigned int)wcslen(name);
    param.type = TYPC;
    param.leng = size;
    param.addr = &buf;
}

RFC_HANDLE WIN_DLL_EXPORT_FLAGS CreateQuery(RFC_HANDLE hConn, rfc_char_t* query) 
{
    rfc_char_t datasetID[256];
    rfc_char_t ret[256];

    RFC_PARAMETER exporting[3];
    MakeStringParam(exporting[0], _T("DataSetID"), datasetID, 256);
    MakeStringParam(exporting[1], _T("Return"), ret, 256);
    exporting[2].name = nullptr;

    // TODO: is it needed?
    RFC_PARAMETER importing[2]; 
    MakeStringParam(importing[0], _T("CommandText"), ret, 256);
    importing[1].name = nullptr;

    // TODO: put query into the table parameter
    RFC_TABLE tables[2]; 
    tables[0].name = _T("CommandText");
    tables[1].name = nullptr;
    rfc_char_t* ex = nullptr;

    RFC_RC rc = RfcCallReceive(hConn, _T("BAPI_MDDATASETBW_CREATE_OBJECT"), exporting, importing, tables, &ex); 

    if (rc == RFC_OK)
    {
        // TODO: retrieve query handle here
        RFC_HANDLE result = RFC_HANDLE_NULL;
        return result;
    }

    return RFC_HANDLE_NULL; 
} 

int _tmain(int argc, _TCHAR* argv[])
{
    RfcInit();

    RFC_HANDLE hConn = Logon();

    rfc_char_t* query = _T("\
        SELECT\
            {[Measures].[DA70CEZJ18267Q9RS4XFK7J32]} ON COLUMNS,\
            {[0APO_LOCNO].[LEVEL01].AllMembers} ON ROWS\
        FROM [0APO_C01/IDES_APO_04]");

    RFC_HANDLE hQuery = CreateQuery(hConn, query);

    // TODO: retrieve query results using 
    // - MDDataSetBW.GetAxisData
    // - MDDataSetBW.GetAxisInfo
    // - MDDataSetBW.GetCellData

    RfcClose(hConn);
    return 0;
}
Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
Valeri Kim
  • 21
  • 1

0 Answers0