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

Dynamic select

Former Member
0 Likes
798

I have a dynamic select like:

data: where_tab(50) occurs 0.

tables mara.

ranges s_matnr for mara-matnr

where_tab = 'matnr in s_matnr'.

select single * from mara where (where_tab).

and i get a dump.

Can i make a dynamic selection with the IN operator.

Thanks in advance

regards

1 ACCEPTED SOLUTION
Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
637

Hi, you cannot use the IN operator in a dynamic WHERE clause, at least not in 46c.

Your only option would be do create the select statement in a generated subroutine and pass the range to this.

Regards,

RIch Heilman

6 REPLIES 6
Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
638

Hi, you cannot use the IN operator in a dynamic WHERE clause, at least not in 46c.

Your only option would be do create the select statement in a generated subroutine and pass the range to this.

Regards,

RIch Heilman

Read only

0 Likes
637

You can not in 5.0, either.

Read only

0 Likes
637

I don´t understand the solution Rich, can you explain me a bit please what is your solution?

Thanks in advance

regards

Read only

0 Likes
637

Here is what I mean by a generated subroutine. You can see how you can build your select statement at runtime.

In this program, it is statically defined, but you can maniuplate the source as you need to.



report zrich_0001
       no standard page heading.


types: t_source(72).

data: routine(32) value 'TEMP_ROUTINE',
      program(8),
      message(128),
      line type i.

data: isource type table of t_source,
            xsource type t_source.

data: imara type table of mara with header line.
ranges: r_matnr for imara-matnr.

start-of-selection.

  r_matnr-sign = 'I'.
  r_matnr-option = 'BT'.
  r_matnr-low = '000000000040000100'.
  r_matnr-high = '000000000040000200'.
  append r_matnr.

  perform build_select_statement.




  generate subroutine pool isource name program
                           message message
                           line line.
  if sy-subrc = 0.
    perform (routine) in program (program) tables r_matnr imara.
  else.
    write:/ message.
  endif.

  loop at imara.
    write:/ imara-matnr.
  endloop.


*&---------------------------------------------------------------------*
*&      Form  build_select_statement
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form build_select_statement.


  xsource = 'REPORT ZTEMP_REPORT.'.
  append xsource  to isource.

  concatenate 'FORM' routine
            'tables xr_matnr ximara .'
            into xsource separated by space.
  append xsource  to isource.

  xsource = 'select * into table ximara from mara'.
  append xsource  to isource.

  xsource = 'where matnr in xr_matnr.'.
  append xsource  to isource.

  xsource = 'ENDFORM.'.
  append xsource to isource.

endform.                    " build_select_statement


Regards,

Rich Heilman

Read only

Read only

Former Member
0 Likes
637

hi Carlson,

try this way ..


concatenate ''' 's_matnr' ''' into str1.
condense str1 no-gaps.
 concatenate 'matnr in '     str1                
             into where_tab  separated by space.  


select single * from mara where (where_tab).

Regards,

Santosh