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

SELECT Max where column is dynamic in abap program

Former Member
0 Likes
2,162

How to perform a SELECT MAX query where the column is dynamic in abap program?

This is how the code looks like:

FORM get_max_ref_field  USING    p_tabname
                                                   p_field
                         CHANGING p_max_date.

      SELECT MAX( p_field INTO p_max_date
     FROM (p_tabname).


ENDFORM.                    " GET_MAX_REF_FIELD

Thanks,

Vika

How to perform a SELECT MAX query where the column is dynamic in abap program?

This is how the code looks like:

FORM get_max_ref_field  USING    p_tabname
                                                   p_field
                         CHANGING p_max_date.

      SELECT MAX( p_field INTO p_max_date
     FROM (p_tabname).


ENDFORM.                    " GET_MAX_REF_FIELD

Thanks,

Vika

3 REPLIES 3
Read only

Aliaksandr
Active Participant
0 Likes
1,420

Hi Vika,

I have adapted your form a little(see below). I hope it will be suitable solution for you.

form get_max_ref_field using p_tabname

                              p_field

                        changing p_max_date.

   data: l_max_field(72).

   concatenate 'MAX(' p_field ')' into l_max_field separated by space.

   select single (l_max_field) into p_max_date from (p_tabname).

endform.

Kind regards, Aliaksandr.

Read only

Former Member
0 Likes
1,420

Hi Vika,

In your code

FORM get_max_ref_field  USING    p_tabname
                                                   p_field
                         CHANGING p_max_date.

      SELECT MAX( p_field INTO p_max_date

FROM (p_tabname).

p_field is a required field to get the data. Make p_field as field-symbol. To make any field as dynamic better to use field symbol concept.

Regards,

Vineesh.

Read only

Former Member
0 Likes
1,420

Hi Vika,

Check this Code ..

PERFORM MY_SELECT USING `CITYFROM` `max( DISTANCE )` `SPFLI`.        " Group by Column name , Max with Column name , Table Name

FORM MY_SELECT USING L_GROUP TYPE STRING

                      L_MAX TYPE STRING

                      T_NAME TYPE STRING .

   DATA: L_COLUMNS   TYPE TABLE OF STRING,

         L_CONTAINER TYPE STRING,

         L_COUNT     TYPE I.

   APPEND L_GROUP TO L_COLUMNS.

   APPEND L_MAX TO L_COLUMNS.

   SELECT (L_COLUMNS)

          FROM (T_NAME)                 "spfli

          INTO (L_CONTAINER, L_COUNT)

          GROUP BY (L_GROUP).

     WRITE: /  L_COUNT,L_CONTAINER.

   ENDSELECT.

ENDFORM.                    

Regard's

Smruti