Application Development 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: 

How to use Dynamic SQL in ABAP

Former Member
0 Kudos

If I have mara table and I have an selection screen where I have allowed user to select the fields of MARA,Now What I want is depending on his selection we should be able to execute the sql command.

E.g.

Tables: mara.

selection-screen

MATNR , ERSDA , ERNAM , MTART , MEINS.

NOW THE OUTPUT SHOULD BE ABLE TO DISPLAY ONLY THE SELECTED FIELD VALUE.

4 REPLIES 4

Former Member
0 Kudos

Hi Nandan,

I need some more clarification. regarding ur requirement.what i infer is that u want the user to be able to select the fields he wants to see in the report.

regards

varun

ssimsekler
Active Contributor
0 Kudos

Hi Nandan

Dynamic SELECT statement may be writen as follows:


data: begin of ftab occurs 0 ,
        fname(30) type c ,
      end of ftab .
data: begin of condtab ocurs 0 ,
        cond_line(60) type c ,
      end of condtab .

refresh ftab .
ftab-fname = 'MARA' .
append ftab .

refresh condtab .
CONCATENATE 'MATNR =' p_matnr into condtab-cond_line
            separated by space .
append condtab .

SELECT (ftab) from (lv_dbtabname)
       INTO CORRESPONDING FIELDS OF <target>
       WHERE (condtab) .

However, you see the INTO statement needs a defined structure. So, you can have a structure comprising all possibilities and use "CORRESPONDING FIELDS" addition.

Or you can generate an internal table dynamicaly using "cl_alv_table_create", but I do not know whether INTO-statement can handle field-symbols.

Secondly, displaying fields dynamicaly needs further coding.

Hope this much helps...

*--Serdar

Former Member
0 Kudos

You can achieve this fairly easily using ALV.

Define an internal table T_MARA of line type MARA.

Create a field table, FTAB, as Serdar describes. Populate this with the field names that the user selects.

Your select would then be:

SELECT (FTAB) FROM TABLE MARA
  INTO CORRESPONDING FIELDS OF TABLE T_MARA
  WHERE ...

Now you need to display the results to the user.

Build a field catalog using MARA as the input structure. Loop thru the field catalog and set TECH = 'X' for all fields that you do NOT want to display. Now display the ALV Grid and all TECH fields will never be displayed.

Former Member
0 Kudos

Hello Nandan,

Try the following code. It works.

tables: mara.
data: tablename  type tabname,
      fname      type fieldname,
      fieldnames type fieldname occurs 0.

field-symbols : <fs_tableline> type any,
                <fs_fieldval>  type any.


initialization.
  tablename = 'MARA'.
  assign (tablename) to <fs_tableline> .

  fname = 'MATNR'.
  append fname to fieldnames.

  fname = 'MTART'.
  append fname to fieldnames.


select (fieldnames)
  from (tablename)
  up to 20 rows
  into corresponding fields of <fs_tableline>.

  loop at fieldnames into fname.
    assign component fname of structure <fs_tableline> to <fs_fieldval>.
    write : <fs_fieldval>.
  endloop.

  new-line.

endselect.

The <b>tables</b> statement is very important. You should at least have the list of the tables which you expect the user to enter.

Please do get back if you have any doubts. If you think the question is answered, please reward the points to the useful answers and close the thread.

Regards,

Anand Mandalika.