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

Variable Select

Former Member
0 Likes
1,493

I have a question regarding a select that requires a lot of variability.

I am looping at an internal table and comparing each record within that to the results from a function module which I just processed.

It looks something like this:

LOOP AT ITAB1.
SELECT (itab1-field) FROM (itabresult-structurename) 
             WHERE (itab1-field) EQ (itab1-value).
ENDLOOP.

Since itab1-field is not defined as an internal table, I cannot process it as a variable field. Should I pass the value of itab1-field to a variable before I do the select and then use (varField)? I'm not sure.

Any suggestions? Please help.

N L

1 ACCEPTED SOLUTION
Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
1,421

You can't do a SELECT statement against an internal table. You must use the READ statement.

I'm not sure that you can do a dynamic read statement. I will test it out and get back to you.

Regards,

Rich Heilman

12 REPLIES 12
Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
1,422

You can't do a SELECT statement against an internal table. You must use the READ statement.

I'm not sure that you can do a dynamic read statement. I will test it out and get back to you.

Regards,

Rich Heilman

Read only

0 Likes
1,421

I think you're correct, this variability isn't going to fly.

This is going to require some manual coding.

Thanks for your help.

N L

Read only

0 Likes
1,421

Ok, here is sample program which illistrats what I think you are trying to achieve. This sample program uses tables MARA and MARC for sample data. Please install this program and run thru the debug. Make sure to change the hard coded material numbers to something that exists in your system.



report zrich_0003 .


data: imara type table of mara with header line.
data: imarc type table of marc with header line.

data: begin of itab occurs 0,
      table(20) type c,
      wa(20) type c,
      field(20) type c,
      value(18) type c,
      end of itab.

data: wa like line of itab.


  field-symbols: <fs1> type standard table,
                 <fs4>.

start-of-selection.

  select * into corresponding fields of table imara
              from mara
                     where matnr = '000000000060001262'.


  select * into corresponding fields of table imarc
              from marc
                     where matnr = '000000000060001262'.


  itab-table = 'IMARA[]'.
  itab-wa    = 'IMARA'.
  itab-field = 'MATNR'.
  itab-value = '000000000060001262'.
  append itab.

  itab-table = 'IMARC[]'.
  itab-wa    = 'IMARC'.
  itab-field = 'MATNR'.
  itab-value = '000000000060001262'.
  append itab.


  loop at itab.

    assign (itab-table) to <fs1>.
    assign (itab-wa)    to <fs4>.

    read table <fs1> into <fs4> with key (itab-field) =  itab-value.
    if sy-subrc = 0.

    endif.

  endloop.

Regards,

Rich Heilman

Read only

0 Likes
1,421

Or are you trying to do a dynamic select from a database table? If so....then it would be more like this.

  data: where_clause(80) occurs 10 with header line.

  loop at itab.

    assign (itab-table) to <fs1>.

    clear where_clause. refresh where_clause.
    concatenate itab-field ' = ''' itab-value '''' into where_clause.
    append where_clause.

    select * into corresponding fields of table <fs1>
         from (itab-dbtable)
              where (where_clause).

  endloop.

Regards,

Rich Heilman

Read only

0 Likes
1,421

it is a select from a database table, but the name of the database table is retrieved from an itab. I'm not having trouble with the tablename variability,

I know you can make the tablename variable in the select by enclosing it in parentheses, but I guess that effect doesn't apply to fieldnames also.

I'm really trying to get the fieldname to be dynamic in the select.

Read only

0 Likes
1,421

Hi Natasha,

You can set the fieldnames dynamically in this case for your where clause. For example your where clause can be stg. like 'FIELD_XYZ' eq 3 but it can not be (lv_fieldname) eq 3, in which case lv_fieldname has value 'FIELD_XYZ'.

Kind Regards,

Sükrü

Read only

0 Likes
1,421

In my previous posting, it had to be 'You can NOT set fieldnames dynamically ...'. Sorry.

Kind Regards,

Sükrü

Read only

0 Likes
1,421

Instead of this...

SELECT (ls_field_table-field)        
   FROM  (ls_field_table-structure_name)    
         INTO <fs_wa>       
                WHERE  (lt_where).

maybe try something like this.

SELECT *    
   FROM  (ls_field_table-structure_name)  
           INTO corresponding fields of <fs_wa> 
                      WHERE  (lt_where).

Regards,

Rich Heilman

Read only

0 Likes
1,421

Hi Natasha,

My programmed ran without any problem. I think you forget the ENDSELECT statement at end of your SELECT, and since we are fetching data ino , which is a structure and not an internal table, this syntax error occurs.

Kind Regards,

Sükrü

Read only

0 Likes
1,421

Hi Natasha,

Here is a sample code for the dynamic select.


DATA: BEGIN OF s_itab,
         tabname       LIKE dd03l-tabname,
         sel_fieldname LIKE dd03l-fieldname,
         val_fieldname LIKE dd03l-fieldname,
         fieldval(30).
DATA: END OF s_itab.

DATA: fieldname LIKE dd03l-fieldname.

DATA: matnr LIKE mara-matnr.

DATA: where_clause(80).

DATA: itab          LIKE STANDARD TABLE OF s_itab
                                              WITH HEADER LINE,
      i_fields      LIKE STANDARD TABLE OF fieldname
                                              WITH HEADER LINE,
      i_matnr       LIKE STANDARD TABLE OF matnr
                                              WITH HEADER LINE,
      i_whereclause LIKE STANDARD TABLE OF where_clause
                                              WITH HEADER LINE.

*------------------
START-OF-SELECTION.
*------------------

  itab-tabname   = 'MARA'.
  itab-sel_fieldname = 'MATNR'.
  itab-val_fieldname = 'MTART'.
  itab-fieldval  = 'HALB'.

  APPEND itab.
  CLEAR itab.

  LOOP AT itab.
    CLEAR: i_fields[].
    MOVE itab-sel_fieldname TO i_fields.
    APPEND i_fields.
    CLEAR i_fields.
    CONCATENATE ''''
                itab-fieldval
                ''''
           INTO i_whereclause.
    CONDENSE i_whereclause NO-GAPS.

    CONCATENATE itab-val_fieldname
                '='
                i_whereclause
           INTO i_whereclause SEPARATED BY space.
    APPEND i_whereclause.
    CLEAR i_whereclause.

    SELECT (i_fields) INTO TABLE i_matnr
                      FROM (itab-tabname)
                     WHERE (i_whereclause).
  ENDLOOP.

  LOOP AT i_matnr.

    WRITE:/ i_matnr.

  ENDLOOP.

Hope this helps,

Srinivas

Read only

Former Member
0 Likes
1,421

Hi Natasha,

You can select data from a database table dynamically as in program below:


REPORT  zsb_dyn_select.

DATA: BEGIN OF ls_field_table,
          field           TYPE  fieldname,
          structure_name  TYPE  typename,
          value           TYPE  string,
      END OF ls_field_table,

      lt_field_table LIKE TABLE OF ls_field_table,
      lref_wa  TYPE REF TO data,
      lt_where TYPE TABLE OF editline,
      lv_where TYPE editline,
      lv_typename TYPE string.

FIELD-SYMBOLS: <fs_wa> TYPE ANY.


ls_field_table-field          = 'CONNID'.
ls_field_table-structure_name = 'SFLIGHT'.
ls_field_table-value          = 'LH'.
APPEND ls_field_table TO lt_field_table.

lv_where = `CARRID eq ls_field_table-value`.
APPEND lv_where TO lt_where.
LOOP AT lt_field_table INTO ls_field_table.
  CONCATENATE ls_field_table-structure_name
              '-'
              ls_field_table-field
              INTO lv_typename.
  CREATE DATA lref_wa TYPE (lv_typename).
  ASSIGN lref_wa->* TO <fs_wa>.
  SELECT (ls_field_table-field)
         FROM  (ls_field_table-structure_name)
         INTO <fs_wa>
         WHERE  (lt_where).
  ENDSELECT.
  IF sy-subrc IS INITIAL.
    WRITE / <fs_wa>.
  ENDIF.
ENDLOOP.

Kind Regards,

Sükrü

Read only

0 Likes
1,421

Thanks Suekrue and Rich,

At the following point, I get an error that (ls_field_table-field) is not an internal table. Same problem I ran into before. Suekrue, did the program execute past this step when you ran it?

SELECT (ls_field_table-field)
         FROM  (ls_field_table-structure_name)
         INTO <fs_wa>
         WHERE  (lt_where).