Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

EXECUTE A SQL STATEMENT FROM A STRING.

Former Member
0 Likes
2,096

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.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,368



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.

4 REPLIES 4
Read only

Former Member
0 Likes
1,368

Hi

You need to be using the EXECUTE_QUERY logic as described in this thread

Regards

Arden

Read only

0 Likes
1,368

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.

Read only

Former Member
0 Likes
1,368

Thanks for quick replay.

function module available for this?

Read only

Former Member
0 Likes
1,369



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.