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

selecting data from table based on selection screen

former_member391265
Participant
0 Likes
7,676

Hi All,

I have selection screen  with 8 parameters. Now i have to fetch data from table ABC into itab based on the selection screen data.

for example :

   parameters: ztable-para1                    ztable-para5              ====> All fields from same table.      

                     ztable-para2                    ztable-para6

                     ztable-para3                    ztable-para7

                     ztable-para4                    ztable-para8

Now i need to select data from ztable based on selecting data from parameters. Now lets say i fill all fields and i will get data based on that.

now, what if i fill only 2 parameters, that is para1 & para2 and rest is blank. now i want data based on para1 and para2. it is checking other  field is blank and comparing for that.

para1 = Userid , para2 = name para3 = rollnum  para4 = class,

do i need to put where condition in all fields? because if i put some fields empty, still it should fetch data based on my para selection.

if i need to check each para empty or not, it is not feasible.

please help.

Thanks

6 REPLIES 6
Read only

atul_mohanty
Active Contributor
0 Likes
2,104

Hi

In that case use select option. If you do pass value to select option, it will restrict to that value, if you do not pass any value it will check for all possible values.

Regards,

Atul Mohanty

Read only

Former Member
0 Likes
2,104

Hi KSRCM,

before doing a select to the database, build a range for each parameter in the selection screen then it will work. sample code for the same below.

parameters: var1 type data1

                    var2 type data2

                    var3 type data3

         

data: lr_range1 type range of data1,

         lw_range1 like line of lr_range1,

         lr_range2 type range of data2,

         lw_range2 like line of lr_range2,

         lr_range3 type range of data3,

         lw_range3 like line of lr_range3.

lw_range1-sign = lw_range2-sign = lw_range3-sign = 'I'.

lw_range1-option = lw_range2-option = lw_range3-option = 'EQ'.

lw_range1-low = var1.

lw_range2-low = var2.

lw_range3-low = var3.

append: lw_range1 to lr_range,

              lw_range2 to lr_range,

              lw_range3 to lr_range.

select *

     from abc

     into table lt_tab

     where var1 in lr_range1

          and  var2 in lr_range2

          and var3 in lr_range3.

now even though one of the parameters is not passed the above select will fetch the correct data

Read only

former_member214709
Participant
0 Likes
2,104

Hi,

Declare all the fields like:

SELECT-OPTIONS: s1 for ztable-fieldname1,

                              s2 for ztable-fieldname2.

And got for the select query in this way:

SELECT FI F2 F3 F4 F5

FROM TABLE TAB

INTO TABLE ITAB

WHERE S1 IN S1

AND      S2 IN S2.

It will automatically take care even if the values are blank.

Regards,

Dinesh

Read only

Former Member
0 Likes
2,104

You need to use select options instead of parameters. Paramaters considers blank value as a value in itself and will do the select query based on that. However, for a select-option, no values inputted means, it can take any value and hence will retrieve data based only on the inputs entered.

To make the display look like parameters, you can give the addition no-extension and no-intervals.

SELECT-OPTIONS : p_bukrs for anla-bukrs NO-EXTENSION NO INTERVALS.

Read only

marius_greeff
Active Participant
0 Likes
2,104

Should you not want to use SELECT-OPTIONS you can build a dynamic where clause. Do f1 on Select statement. The SAP Documentation is good with some examples.

In short, a dynamic select statement will then look something like this

TRY.

SELECT * FROM (p_table_name)
             INTO CORRESPONDING FIELDS OF TABLE <it_table>
             WHERE (it_select_where).

      CATCH cx_sy_dynamic_osql_error INTO gr_exc.
        EXIT.
ENDTRY

Read only

nishantbansal91
Active Contributor
0 Likes
2,104

Hi KSRCM,

Follow the below code its solve your problem.



data text type table of string.

IF s_bukrs[] IS NOT INITIAL .

  APPEND ' BUKRS IN S_BUKRS ' TO text .

  ENDIF .

  IF s_pernr[] IS NOT INITIAL .

  APPEND ' AND PERNR IN S_PERNR ' TO text .

  ENDIF .

  LOOP AT text .

  IF sy-tabix = 1 .

  REPLACE ALL OCCURRENCES OF ' AND ' IN text WITH ' ' .

  MODIFY text .

  EXIT .

  ENDIF .

  ENDLOOP .

  IF text[] IS NOT INITIAL .

  CONCATENATE LINES OF text INTO text1 . " This can combine all th e line into one line.

  SELECT lifnr FROM lfb1 INTO CORRESPONDING FIELDS OF TABLE it_itab1 WHERE (text1).


  ENDIF .

Let me know if you have any other issue.

Regards.

Nishant Bansal.