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
653

Hi All

I need to pass internal tables to a select query dynamically. The database table from which i am selecting remains the same but the internal tables are subjected to change. Since I am using for all entries addition, the interpreter refuses to recognize the structure of the internal table on which i am using for all entries.

The code i tried to work with is as follows, but it throws a syntax error, saying "the structure of <fs_tab2> is unknown, there is no component matwa".

form sel_kotd001 using value(fp_tab1)

value(fp_tab2).

field-symbols:<fs_tab1> type standard table,

<fs_tab2> type standard table.

assign fp_tab1 to <fs_tab1>.

assign fp_tab2 to <fs_tab2>.

SELECT knumh

matwa

FROM kotd001

INTO TABLE <fs_tab1>

FOR ALL ENTRIES IN <fs_tab2>

WHERE matwa EQ <fs_tab2>-matwa.

endform.

any help in this regard will be appreciated

thanks

Sudhir

5 REPLIES 5
Read only

uwe_schieferstein
Active Contributor
0 Likes
570

Hello Sudhir

Here is a workaround for your problem:

*&---------------------------------------------------------------------*
*& Report  ZUS_SDN_DYNAMIC_SELECT_FOR_ALL
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  zus_sdn_dynamic_select_for_all.


TABLES:  kotd001.  " Conditions: Substitution - Sample Structure


*MANDT
*KAPPL
*KSCHL
*MATWA
*DATBI
*DATAB
*KNUMH


DATA:
  gdo_data2     TYPE REF TO data,
  gt_itab1      TYPE STANDARD TABLE OF kotd001,
  gt_itab2      TYPE STANDARD TABLE OF lips.  " dynamic itab


PARAMETERS:
  p_vbeln       TYPE vbeln  DEFAULT '80005708'.


START-OF-SELECTION.

  SELECT * FROM  lips INTO TABLE gt_itab2
      WHERE  vbeln = p_vbeln.


  PERFORM set_kotd001
                  USING
                     gt_itab2
               CHANGING
                     gt_itab1.


END-OF-SELECTION.

*&---------------------------------------------------------------------*
*&      Form  set_kotd001
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_GT_ITAB2  text
*      <--P_GT_ITAB1  text
*----------------------------------------------------------------------*
FORM set_kotd001
             USING
                ut_itab2  TYPE table
          CHANGING
                ct_itab1  TYPE table.
* define local data
  DATA:
    lr_matwa    TYPE RANGE OF matwa,
    ls_rng      LIKE LINE OF lr_matwa.

  FIELD-SYMBOLS:
    <ld_fld>    TYPE ANY,
    <ls_entry>  TYPE ANY.


  ls_rng-sign   = 'I'.
  ls_rng-option = 'EQ'.

  LOOP AT ut_itab2 ASSIGNING <ls_entry>.
    CLEAR: <ld_fld>.

    ASSIGN COMPONENT 'MATWA' OF STRUCTURE <ls_entry> TO <ld_fld>.
   IF ( <ld_fld> IS BOUND ).
      ls_rng-low = <ld_fld>.
      APPEND ls_rng TO lr_matwa.
   ENDIF.
  ENDLOOP.


  SELECT knumh matwa FROM kotd001
    INTO corresponding fields of TABLE ct_itab1
      WHERE matwa IN lr_matwa.


ENDFORM.                    " set_kotd001

Instead of selecting with the dynamic itab use a defined range. If you need different WHERE conditions simply add the appropriate range and check if the dynamic structure contains this selection field. If not, the range will be empty (= no restriction for this field).

Regards

Uwe

Read only

Former Member
0 Likes
570

Hi,

create a structure of type RSDSWHERE,

E.g.

data: tplnr_where type standard table of rsdswhere.

append '0001' to tplnr_where.

append wa_tplnr_where to tplnr_where.

select equnr eqasp eqtyp eqart serge

objnr cuobj matnr sernr

werk iwerk hequi heqnr

ingrp gewrk spras eqktx tplnr vkorg

vtweg spart vkbur vkgrp

from v_equi

where tplnr eq (tplnr_where).

Regards,

Raghavendra

Read only

0 Likes
570

Please check this out, This will work for any table / infotype.

DATA WF_TABLE_NAME LIKE DD02L-TABNAME.

DATA ITABREF TYPE REF TO DATA.

DATA WF_WHERE(100) TYPE C.

FIELD-SYMBOLS:

<DBTAB> TYPE STANDARD TABLE,

<FS_ITAB> TYPE ANY.

*

Get <b>WF_TABLE_NAME</b> value from user input.

SELECT SINGLE * FROM DD02L

WHERE TABNAME EQ WF_TABLE_NAME AND

AS4LOCAL EQ 'A'.

If sy-subrc eq 0.

CREATE DATA ITABREF TYPE STANDARD TABLE OF (WF_TABLE_NAME).

ASSIGN ITABREF->* TO <DBTAB>.

SELECT * FROM (WF_TABLE_NAME)

INTO TABLE INT_TABLE WHERE <fldname> = WF_WHERE.

IF SY-SUBRC EQ 0.

LOOP AT INT_TABLE ASSIGNING <FS_ITAB>.

******

Write your logic....

******

ENDLOOP.

ENDIF.

endif.

Regards,

Ramki.

Read only

Former Member
0 Likes
570

Hi,

Dynamic Select Statement

check this code...

*-----

REPORT demo_select_dynamic_database .

DATA wa TYPE scarr.

DATA name(10) TYPE c VALUE 'SCARR'.

SELECT *

INTO wa

FROM (name) CLIENT SPECIFIED

WHERE mandt = '000'.

WRITE: / wa-carrid, wa-carrname.

ENDSELECT.

*-----

For dynamic field list

REPORT demo_select_dynamic_columns .

DATA: itab TYPE STANDARD TABLE OF spfli,

wa LIKE LINE OF itab.

DATA: line(72) TYPE c,

list LIKE TABLE OF line(72).

line = ' CITYFROM CITYTO '.

APPEND line TO list.

SELECT DISTINCT (list)

INTO CORRESPONDING FIELDS OF TABLE itab

FROM spfli.

IF sy-subrc EQ 0.

LOOP AT itab INTO wa.

WRITE: / wa-cityfrom, wa-cityto.

ENDLOOP.

ENDIF.

Regards

Sudheer

Read only

0 Likes
570

If you got an answer, please close this thread.