Sie sind auf Seite 1von 10

Execute BW Query Using ABAP

SDN Community Contribution


(This is not an official SAP document.)

Disclaimer & Liability Notice


This document may discuss sample coding or other information that does not include SAP official interfaces and therefore is not supported by SAP. Changes made based on this information are not supported and can be overwritten during an upgrade. SAP will not be held liable for any damages caused by using or misusing the information, code or methods suggested in this document, and anyone using these methods does so at his/her own risk. SAP offers no guarantees and assumes no responsibility or liability of any type with respect to the content of this technical article or code sample, including any liability resulting from incompatibility between the content within this document and the materials and services offered by SAP. You agree that you will not hold, or seek to hold, SAP responsible or liable with respect to the content of this document.

2005 SAP AG

The SAP Developer Network: http://sdn.sap.com

Execute BW Query Using ABAP

Applies To:
BW3.5 (This is tested in BW3.5 and may/may not work in earlier versions)

Summary
In April 2005, I have written two weblogs titled Execute BW query using ABAP Part I & Execute BW query using ABAP Part II . Since then the function module (Y_EXECUTE_QUERY) explained in Execute BW query using ABAP Part I has undergone major changes to handle performance and to provide some additional features. The changed code is provided in this document.

By: Durairaj Athavan Raja Company: Atos Origin Middle East Date: 20 July 2005

Table of Contents
SDN Community Contribution ..........................................................................................................1 (This is not an official SAP document.) ............................................................................................1 Disclaimer & Liability Notice .............................................................................................................1 Applies To:........................................................................................................................................2 Summary ..........................................................................................................................................2 Table of Contents .............................................................................................................................2 Fixes .................................................................................................................................................2 The New Y_EXECUTE_QUERY code .........................................................................................3 Author Bio.......................................................................................................................................10

Fixes
Major fixes done to Y_EXECUTE_QUERY 1. Time consuming validations (query variable) are taken away. Now its the responsibility of the user to pass proper variable/variable value. 2. Display attributes were not supported in the earlier version of Y_EXECUTE_QUERY and the same is fixed in this version. 3. Restricted key figures with multiple columns which were not supported in the earlier version is fixed in this version.

2005 SAP AG

The SAP Developer Network: http://sdn.sap.com

Execute BW Query Using ABAP

The New Y_EXECUTE_QUERY code Place the following code in the global data of the function module.
type-pools: RRX1 . DATA: r_request TYPE REF TO cl_rsr_request. DATA: r_dataset TYPE REF TO cl_rsr_data_set. DATA: zcx_message TYPE REF TO cx_rsr_x_message. DATA: zcx_root TYPE REF TO cx_root. DATA: lcount TYPE i . DATA: xcount TYPE i . DATA: wa_var TYPE rrx1_s_var. DATA: i_var TYPE rrx1_t_var. data: i_var_final TYPE rrx1_t_var. DATA: wf_variant TYPE variant . data: wa_axis type RRWS_SX_AXIS_DATA . data: wa_axis_info type RRWS_SX_AXIS_INFO . data: wa_chars type RRWS_SX_AXIS_CHARS . data: tmp_char type RRWS_THX_AXIS_CHARS. data: wa_tmp_char type RRWS_SX_AXIS_CHARS . data: wa_attrinm type RRWS_S_ATTRINM. data: wa_cell type RRWS_S_CELL . DATA: wa_textsymbols type RRWS_S_TEXT_SYMBOLS . DATA: wa_textsymbols1 type RRWS_S_TEXT_SYMBOLS . data: wa_set type RRWS_SX_TUPLE . data: tmp_set type RRWS_TX_SET. data: wa_tmp_set type RRWS_SX_TUPLE . data: wa_dattrinm type RRWS_S_ATTRIBUTES . Replace the source code of Y_EXECUTE_QUERY with the following code. FUNCTION y_execute_query . *"---------------------------------------------------------------------*"*"Local Interface: *" IMPORTING *" VALUE(QUERY_NAME) TYPE CHAR50 *" EXPORTING *" VALUE(XML_OUT) TYPE STRING *" TABLES *" QUERY_VARIABLES STRUCTURE RRX_VAR *" RETURN STRUCTURE BAPIRET2 OPTIONAL *" META STRUCTURE ZBW_QUERY_OUTPUT_METADATA OPTIONAL *" EXCEPTIONS *" BAD_VALUE_COMBINATION *" USER_NOT_AUTHORIZED *" UNKNOWN_ERROR *" QUERY_NOT_FOUND *"---------------------------------------------------------------------DATA: cube_name TYPE bapi6110var-cube_nam . DATA: p_genuniid TYPE rsgenuniid . DATA: r_param TYPE REF TO cl_rsr_parameter. DATA: wf_query_var TYPE REF TO cl_rsr_query_variables . DATA: var_def TYPE rsr_t_variable_definition . DATA: wf_variables TYPE REF TO cl_rsr_variables . DATA: wf_has_variables TYPE rs_bool . DATA: error_string TYPE string.

2005 SAP AG

The SAP Developer Network: http://sdn.sap.com

Execute BW Query Using ABAP

DATA: no_of_chars TYPE i , no_of_keyf TYPE i . DATA: var_nam(10) . DATA: iobj_detail TYPE bapi6108 . DATA: wa_obj_detail TYPE bapi6108 . DATA: iobj_details TYPE STANDARD TABLE OF bapi6108 . DATA: it_fieldcat TYPE lvc_t_fcat, is_fieldcat TYPE lvc_s_fcat. DATA: new_table TYPE REF TO data. DATA: new_line TYPE REF TO data. FIELD-SYMBOLS: <ltable> TYPE ANY TABLE, <l_line> TYPE ANY, <l_field> TYPE ANY. DATA: meta_data TYPE STANDARD TABLE OF zbw_query_output_metadata . DATA: wa_meta_data TYPE zbw_query_output_metadata . DATA: grid TYPE REF TO cl_rsr_csv_grid . DATA: r_result TYPE string . DATA: rq_param TYPE rrxw3tquery . DATA: wa_params TYPE w3query . DATA: off TYPE i, moff TYPE i, mlen TYPE i. DATA: iobj_return TYPE STANDARD TABLE OF bapiret2 . DATA: n_counter(3) TYPE n . DATA: char_count TYPE i . DATA: wf_fldnm(40) . MOVE: query_name TO cube_name . * convert the query name to technical id CLEAR p_genuniid . CALL FUNCTION 'CONVERSION_EXIT_GENID_INPUT' EXPORTING input = query_name IMPORTING output = p_genuniid. * create instance of cl_rsr_request CREATE OBJECT r_request EXPORTING i_genuniid = p_genuniid . *create instance of cl_rsr_variables CREATE OBJECT wf_variables EXPORTING i_genuniid = p_genuniid EXCEPTIONS query_not_found = 1 x_message = 2 OTHERS = 3 . IF sy-subrc <> 0. RAISE query_not_found . ENDIF. CLEAR: i_var_final . REFRESH i_var_final . LOOP AT query_variables .

2005 SAP AG

The SAP Developer Network: http://sdn.sap.com

Execute BW Query Using ABAP

CLEAR wa_var . MOVE-CORRESPONDING query_variables TO wa_var . APPEND wa_var TO i_var_final . ENDLOOP . CREATE OBJECT wf_query_var EXPORTING i_r_request = r_request i_t_nvar = i_var_final EXCEPTIONS user_not_authorized = 1 no_processing = 2 bad_value_combination = 3 x_message = 4 OTHERS = 5 . IF sy-subrc <> 0. CASE sy-subrc . WHEN 1 . RAISE user_not_authorized . WHEN 3 . RAISE bad_value_combination . WHEN OTHERS . RAISE unknown_error . ENDCASE . ENDIF. CALL METHOD wf_variables->set EXPORTING i_t_nvar = i_var_final. CALL METHOD wf_variables->check EXCEPTIONS bad_value_combination = 1 user_not_authorized = 2 x_message = 3 OTHERS = 4. IF sy-subrc <> 0. CALL FUNCTION 'BAPI_MESSAGE_GETDETAIL' EXPORTING id = sy-msgid number = sy-msgno language = sy-langu textformat = 'CHA' LINKPATTERN = MESSAGE_V1 = MESSAGE_V2 = MESSAGE_V3 = MESSAGE_V4 = IMPORTING message = return-message . MOVE: 'E' TO return-type , sy-msgid TO return-id , sy-msgno TO return-number . APPEND return. CLEAR return .

* * * * *

2005 SAP AG

The SAP Developer Network: http://sdn.sap.com

Execute BW Query Using ABAP

ENDIF. READ TABLE return WITH KEY type = 'E' . IF sy-subrc EQ 0 . RETURN . ENDIF . * set the variable and execute the query TRY. r_request->variables_set( i_t_var = i_var_final ). r_request->variables_start( ). r_request->read_data( ). r_dataset = cl_rsr_data_set=>get( i_r_request = r_request ). r_dataset->refresh( i_version = 1 ). CATCH cx_rsr_x_message INTO zcx_message. CATCH cx_root INTO zcx_root. CLEAR error_string . error_string = zcx_root->get_text( ). MOVE: 'E' TO return-type , error_string+0(220) TO return-message . APPEND return . CLEAR return . RETURN . ENDTRY. IF r_request->n_sx_output-no_authorization NE 'X' AND r_request->n_sx_output-no_data NE 'X' . * find no. of key figures CLEAR: lcount ,wa_axis , wa_axis_info . READ TABLE r_dataset->n_sx_version_20a_1-axis_data INTO wa_axis WITH KEY axis = '000' . IF sy-subrc EQ 0 . CLEAR no_of_keyf . DESCRIBE TABLE wa_axis-set LINES no_of_keyf . ENDIF . * find number of characteristics READ TABLE r_dataset->n_sx_version_20a_1-axis_info INTO wa_axis_info WITH KEY axis = '001' . IF sy-subrc EQ 0 . CLEAR no_of_chars . DESCRIBE TABLE wa_axis_info-chars LINES no_of_chars . ENDIF . CLEAR : iobj_detail , iobj_details . REFRESH iobj_details . CLEAR wa_axis_info . * get chars. details READ TABLE r_dataset->n_sx_version_20a_1-axis_info '001' . IF sy-subrc EQ 0 . CLEAR wa_chars . REFRESH tmp_char .

INTO wa_axis_info WITH KEY axis =

2005 SAP AG

The SAP Developer Network: http://sdn.sap.com

Execute BW Query Using ABAP

LOOP AT wa_axis_info-chars INTO wa_chars . CLEAR wa_tmp_char . MOVE-CORRESPONDING wa_chars TO wa_tmp_char . INSERT wa_tmp_char INTO TABLE tmp_char. IF NOT wa_chars-attrinm[] IS INITIAL . LOOP AT wa_chars-attrinm INTO wa_attrinm . CLEAR :wa_tmp_char-chanm , wa_tmp_char-caption . MOVE: wa_attrinm-attrinm TO wa_tmp_char-chanm , wa_attrinm-caption TO wa_tmp_char-caption . INSERT wa_tmp_char INTO TABLE tmp_char. ENDLOOP . ENDIF . ENDLOOP . LOOP AT tmp_char INTO wa_chars . CLEAR off . FIND '__' IN SECTION OFFSET off OF wa_chars-chanm MATCH OFFSET moff MATCH LENGTH mlen. IF sy-subrc EQ 0 . off = moff + mlen . SHIFT wa_chars-chanm LEFT BY off PLACES . ENDIF . CLEAR: iobj_return . REFRESH : iobj_return . CALL FUNCTION 'Z_BAPI_IOBJ_GETDETAIL' EXPORTING version = rs_c_objvers-active infoobject = wa_chars-chanm IMPORTING details = iobj_detail. IF NOT iobj_detail IS INITIAL . CLEAR wa_obj_detail . MOVE-CORRESPONDING iobj_detail TO wa_obj_detail . APPEND wa_obj_detail TO iobj_details . CLEAR wa_obj_detail . ENDIF . CLEAR : iobj_detail . ENDLOOP . ENDIF . READ TABLE return WITH KEY type = 'E' . IF sy-subrc EQ 0 . RAISE user_not_authorized . ENDIF . * build field cat. for building the itab CLEAR wa_obj_detail . LOOP AT iobj_details INTO wa_obj_detail . is_fieldcat-fieldname = wa_obj_detail-infoobject . IF is_fieldcat-fieldname+0(1) EQ '0' . SHIFT is_fieldcat-fieldname LEFT BY 1 PLACES . ENDIF . IF wa_obj_detail-datatp = 'CHAR' . is_fieldcat-datatype = wa_obj_detail-datatp.

2005 SAP AG

The SAP Developer Network: http://sdn.sap.com

Execute BW Query Using ABAP

is_fieldcat-outputlen = '130' . ELSE . is_fieldcat-datatype = 'CHAR' . "iobj_details-datatp. is_fieldcat-outputlen = wa_obj_detail-outputlen . ENDIF . is_fieldcat-scrtext_l = wa_obj_detail-textlong. APPEND is_fieldcat TO it_fieldcat. CLEAR : is_fieldcat , wa_obj_detail . ENDLOOP . CLEAR :n_counter, wa_axis . READ TABLE r_dataset->n_sx_version_20a_1-axis_data INTO wa_axis WITH KEY axis = '000' . IF sy-subrc EQ 0 . LOOP AT wa_axis-set INTO wa_set . AT NEW tuple_ordinal . n_counter = n_counter + 1 . CONCATENATE 'VALUE' n_counter INTO is_fieldcat-fieldname . is_fieldcat-outputlen = '30'. is_fieldcat-datatype = 'CHAR'. ENDAT . CONCATENATE is_fieldcat-scrtext_l wa_set-caption INTO is_fieldcat-scrtext_l SEPARATED BY ` ` . AT END OF tuple_ordinal . SHIFT is_fieldcat-scrtext_l LEFT DELETING LEADING ' ' . APPEND is_fieldcat TO it_fieldcat. CLEAR : is_fieldcat . ENDAT . ENDLOOP . ENDIF . CLEAR meta_data . REFRESH meta_data . LOOP AT it_fieldcat INTO is_fieldcat . CLEAR wa_meta_data . MOVE-CORRESPONDING is_fieldcat TO wa_meta_data . APPEND wa_meta_data TO meta_data . CLEAR: wa_meta_data, is_fieldcat . ENDLOOP . * create itab SORT it_fieldcat BY fieldname. DELETE ADJACENT DUPLICATES FROM it_fieldcat COMPARING fieldname . CALL METHOD cl_alv_table_create=>create_dynamic_table EXPORTING it_fieldcatalog = it_fieldcat IMPORTING ep_table = new_table. * Create a ASSIGN CREATE ASSIGN new Line with the same structure of the table. new_table->* TO <ltable>. DATA new_line LIKE LINE OF <ltable>. new_line->* TO <l_line>.

CLEAR :wa_axis , char_count , lcount, xcount. REFRESH tmp_set .

2005 SAP AG

The SAP Developer Network: http://sdn.sap.com

Execute BW Query Using ABAP

LOOP AT r_dataset->n_sx_version_20a_1-axis_data INTO wa_axis WHERE axis = '001' . LOOP AT wa_axis-set INTO wa_set . CLEAR wa_tmp_set . MOVE-CORRESPONDING wa_set TO wa_tmp_set . INSERT wa_tmp_set INTO TABLE tmp_set. IF NOT wa_set-attributes[] IS INITIAL . LOOP AT wa_set-attributes INTO wa_dattrinm . CLEAR: wa_tmp_set-chanm , wa_tmp_set-chavl , wa_tmp_set-chavl_ext , wa_tmp_set-caption . MOVE: wa_dattrinm-attrinm TO wa_tmp_set-chanm , wa_dattrinm-attrivl TO wa_tmp_set-chavl , wa_dattrinm-attrivl TO wa_tmp_set-chavl_ext , wa_dattrinm-caption TO wa_tmp_set-caption . INSERT wa_tmp_set INTO TABLE tmp_set. ENDLOOP . ENDIF . ENDLOOP . ENDLOOP . LOOP AT tmp_set INTO wa_set . AT NEW tuple_ordinal . IF lcount GT 0 . lcount = lcount - 1 . ENDIF . CLEAR <l_line> . ENDAT . CLEAR off . FIND '__' IN SECTION OFFSET off OF wa_set-chanm MATCH OFFSET moff MATCH LENGTH mlen. IF sy-subrc EQ 0 . off = moff + mlen . SHIFT wa_set-chanm LEFT BY off PLACES . ENDIF . IF wa_set-chanm+0(1) EQ '0' . SHIFT wa_set-chanm LEFT BY 1 PLACES . ENDIF . ASSIGN COMPONENT wa_set-chanm OF STRUCTURE <l_line> TO <l_field>. IF wa_set-chavl = '#' . <l_field> = wa_set-chavl_ext . ELSE . CONCATENATE wa_set-chavl_ext wa_set-caption INTO <l_field> SEPARATED BY ' ' . ENDIF . AT END OF tuple_ordinal . CLEAR: xcount , char_count , n_counter . lcount = lcount + 1 . LOOP AT r_dataset->n_sx_version_20a_1-cell_data INTO wa_cell FROM lcount . n_counter = n_counter + 1 . IF n_counter GT no_of_keyf . EXIT . ENDIF . CONCATENATE 'VALUE' n_counter INTO wf_fldnm . ASSIGN COMPONENT wf_fldnm OF STRUCTURE <l_line> TO <l_field>.

2005 SAP AG

The SAP Developer Network: http://sdn.sap.com

Execute BW Query Using ABAP

<l_field> = wa_cell-value . ENDLOOP . lcount = lcount + no_of_keyf . INSERT <l_line> INTO TABLE <ltable>. ENDAT . ENDLOOP . meta[] = meta_data[] . CALL TRANSFORMATION (`ID`) SOURCE meta = meta_data[] output = <ltable>[] RESULT XML xml_out. ENDIF . " no data/auth IF r_request->n_sx_output-no_authorization EQ 'X' . MOVE: 'I' TO return-type . CONCATENATE 'No authorization' '' INTO return-message SEPARATED BY ' '. APPEND return. CLEAR return . ELSEIF r_request->n_sx_output-no_data EQ 'X' . MOVE: 'I' TO return-type . CONCATENATE 'No applicable data ' '' INTO return-message SEPARATED BY ' '. APPEND return. CLEAR return . ENDIF . ENDFUNCTION.

Author Bio

Durairaj Athavan Raja works as Business System Analyst with Atos Origin Middle East and has been involved in SAP development for over 8 years. He is a big fan of SDN.

2005 SAP AG

The SAP Developer Network: http://sdn.sap.com

10

Das könnte Ihnen auch gefallen