‎2016 Sep 08 7:17 AM
Dear experts,
I have a string called str which has the data 'select MATNR SPRAS MAKTX from MAKT into standard table of GT_MAKT'. i need to execute this string as a sql and get table data into my internal table GT_MAKT( fields and tables are taken from the selection screen dynamically and concatenated with the string).
Please help me.
‎2016 Oct 05 7:59 AM
Here is one of the solution using 'GENERATE SUBROUTINE POOL'.
DATA: prog TYPE string,
tab TYPE STANDARD TABLE OF string,
mess TYPE string,
sid TYPE string.
APPEND 'PROGRAM subpool.' TO tab.
APPEND `DATA gt_makt TYPE TABLE OF makt.` TO tab.
APPEND `LOAD-OF-PROGRAM.` TO tab.
APPEND ` SELECT MATNR SPRAS MAKTX` &
` FROM makt` &
` INTO TABLE gt_makt.` TO tab.
APPEND `FORM loop_at_tab.` TO tab.
APPEND ` DATA gs_makt TYPE makt.` TO tab.
APPEND `ENDFORM.` TO tab.
GENERATE SUBROUTINE POOL tab NAME prog
MESSAGE mess
SHORTDUMP-ID sid.
IF sy-subrc = 0.
PERFORM ('LOOP_AT_TAB') IN PROGRAM (prog) IF FOUND.
ELSEIF sy-subrc = 4.
MESSAGE mess TYPE 'I'.
ELSEIF sy-subrc = 8.
MESSAGE sid TYPE 'I'.
ENDIF.
‎2016 Sep 08 7:24 AM
‎2016 Sep 08 7:38 AM
I would not recommend using Native SQL. I think Open SQL will fit all your Needs.
Just check the documentation for the select Statement.
There are a lot of possibilities to build dynamic SQL statements within open SQL.
But be carefull to put input from screens to dynamic SQL statements. This can lead to harmfull SQL injections.
‎2016 Sep 08 9:34 AM
‎2016 Oct 05 7:59 AM
Here is one of the solution using 'GENERATE SUBROUTINE POOL'.
DATA: prog TYPE string,
tab TYPE STANDARD TABLE OF string,
mess TYPE string,
sid TYPE string.
APPEND 'PROGRAM subpool.' TO tab.
APPEND `DATA gt_makt TYPE TABLE OF makt.` TO tab.
APPEND `LOAD-OF-PROGRAM.` TO tab.
APPEND ` SELECT MATNR SPRAS MAKTX` &
` FROM makt` &
` INTO TABLE gt_makt.` TO tab.
APPEND `FORM loop_at_tab.` TO tab.
APPEND ` DATA gs_makt TYPE makt.` TO tab.
APPEND `ENDFORM.` TO tab.
GENERATE SUBROUTINE POOL tab NAME prog
MESSAGE mess
SHORTDUMP-ID sid.
IF sy-subrc = 0.
PERFORM ('LOOP_AT_TAB') IN PROGRAM (prog) IF FOUND.
ELSEIF sy-subrc = 4.
MESSAGE mess TYPE 'I'.
ELSEIF sy-subrc = 8.
MESSAGE sid TYPE 'I'.
ENDIF.