Professional Documents
Culture Documents
Execute BW Query Using ABAP
Execute BW Query Using ABAP
2005 SAP AG
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.
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
2005 SAP AG
2005 SAP AG
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.
*
*
*
*
*
2005 SAP AG
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 .
2005 SAP AG
2005 SAP AG
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
2005 SAP AG
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
<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
10