Skip to Content

Archived discussions are read-only. Learn more about SAP Q&A

help to improve this code

Hi all,

This is the scenario;

I have a selection screen with all the field of a custom table, all the selection fields are parameters not ranges...

ok, i need to make a select statement only with the fields are filled.

I did the following:

(All the fields of my selection screen are stored in the structuer ls_selection_screen)

DATA: lv_cond_syntax TYPE string,
      lv_field2read TYPE string,
      lt_fields TYPE TABLE OF dd03l,
      ls_fields LIKE LINE OF lt_campos,
      lv_number TYPE CHAR24,
      lv_cont TYPE i.
  
* Get all the field of my custom table;
  SELECT * INTO TABLE lt_fields
   FROM dd03l WHERE tabname EQ 'ZMYCUSTOMTABLE'.

* Fill the WHERE conditons
  LOOP AT lt_fields INTO ls_fields.

    ASSIGN COMPONENT ls_fields-fieldname OF STRUCTURE ls_selection_screen TO <fs_field>.
    CHECK sy-subrc EQ 0.

*   Add the field only if are filled;
    CHECK <fs_field> IS NOT INITIAL.
    ADD 1 TO lv_cont.

*   ---  Concatenate the field to the WHERE ----   *
    IF lv_cont GT 1. "The first without 'AND'
      CONCATENATE lv_cond_syntax 'AND' INTO lv_cond_syntax SEPARATED BY SPACE.
    ENDIF.

*   Concatenate field's value with quotation marks
    IF ls_fields-inttype EQ 'P' OR ls_fields-inttype EQ 'F' OR ls_fields-inttype EQ 'I'.
*     If is numeric, first we must to translate the value to a character type var...    
      WRITE <fs_field> TO lv_number LEFT-JUSTIFIED.
      REPLACE ',' WITH '.' INTO lv_number.
      CONCATENATE '''' lv_number '''' INTO lv_field2read.
    ELSE.
      CONCATENATE '''' <fs_field> '''' INTO lv_field2read.
    ENDIF.

    CONCATENATE lv_cond_syntax ls_fields-fieldname 'EQ' lv_field2read INTO lv_cond_syntax SEPARATED BY SPACE.
  ENDLOOP.

* Select data:
  SELECT * INTO CORRESPONDING FIELDS OF TABLE lt_my_table
    FROM ZMYCUSTOMTABLE
    WHERE (lv_cond_syntax).

Works fine, but i don't know it is there any other easiest way to do it...

Thanks in advance.

Moderator message: please choose more descriptive subject lines for your posts.

Edited by: Thomas Zloch on May 24, 2011 3:08 PM

Former Member
Not what you were looking for? View more on this topic or Ask a question