05-24-2011 1:15 PM
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
05-24-2011 2:08 PM
Try using select-options instead of parameters, they have the desired functionality built in, empty ranges are not being passed to the database, you could have a static WHERE-clause.
On the selection screen, you can use the NO-EXTENSION and NO INTERVALS options of SELECT-OPTIONS to create a "parameter look-a-like". Or define your own ranges with DATA ... TYPE RANGE OF ...
Thomas
05-24-2011 2:10 PM
the first part seems to be o.k., I don't see serious performance problems
But be aware that the generated SELECT can have kind of performance from extremely good to extremely poor.
Select data:
SELECT * INTO CORRESPONDING FIELDS OF TABLE lt_my_table
FROM ZMYCUSTOMTABLE
WHERE (lv_cond_syntax).
05-24-2011 3:16 PM
Thanks Thomas, I know that, my program is a webdynpro so i can do it with the component WDR_SELECT_OPTIONS.
I just wondering if you had some knowledge to do it in a less complicated way... perhaps i'll do with a static WHERE-clause...
Thanks also Siegfried. I'm going to try to measure the performance...