Application Development 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: 

field-symbol for internal tables

former_member182041
Active Contributor
0 Kudos

Hi Experts,

I have an internal table <fs_outtab>. [its dynamically declared with the help of field symbol]

I want to read from this like we read from normal internal tables like

READ TABLE Z_TAB1 INTO W_TAB1 WITH KEY K1 = 'N'.

But this is not possible with field symbol as its structure is known only at run time and hence key name cannot be used.

Is there any other good alternative apart from using LOOP-----ENDLOOP.

Thanks.

1 ACCEPTED SOLUTION

SuhaSaha
Advisor
Advisor
0 Kudos

You can use dynamic tokens to READ the dynamic table. Check this sample code :

PARAMETERS p_bukrs TYPE bukrs.

DATA: dref TYPE REF TO data.

AT SELECTION-SCREEN ON p_bukrs.
  FIELD-SYMBOLS:
  <itab> TYPE STANDARD TABLE,
  <wa> TYPE ANY.

  CREATE DATA dref TYPE STANDARD TABLE OF t001.
  ASSIGN dref->* TO <itab>.

  SELECT * FROM t001 INTO TABLE <itab>.
  IF sy-subrc = 0.
    SORT <itab> BY ('BUKRS').
    READ TABLE <itab> ASSIGNING <wa>
    WITH KEY ('BUKRS') = p_bukrs.
    IF sy-subrc <> 0.
      MESSAGE 'Invalid Comp. Code' TYPE 'E'.
    ENDIF.
  ENDIF.

BR,

Suhas

16 REPLIES 16

SuhaSaha
Advisor
Advisor
0 Kudos

You can use dynamic tokens to READ the dynamic table. Check this sample code :

PARAMETERS p_bukrs TYPE bukrs.

DATA: dref TYPE REF TO data.

AT SELECTION-SCREEN ON p_bukrs.
  FIELD-SYMBOLS:
  <itab> TYPE STANDARD TABLE,
  <wa> TYPE ANY.

  CREATE DATA dref TYPE STANDARD TABLE OF t001.
  ASSIGN dref->* TO <itab>.

  SELECT * FROM t001 INTO TABLE <itab>.
  IF sy-subrc = 0.
    SORT <itab> BY ('BUKRS').
    READ TABLE <itab> ASSIGNING <wa>
    WITH KEY ('BUKRS') = p_bukrs.
    IF sy-subrc <> 0.
      MESSAGE 'Invalid Comp. Code' TYPE 'E'.
    ENDIF.
  ENDIF.

BR,

Suhas

0 Kudos

Hi,

READ TABLE <itab> ASSIGNING <wa> transporting no fields where k1 = 'N'.

Hope this might help you.

With Regards,

Sumodh.P

0 Kudos

Thanks for the reply.

You have mentioned T001 as the base table and then declared the internal table. Hence you know the key

with which you have to read the internal table.

In my scenario , the base table is entered as a selection screen parameter and hence I don't know the key.

Former Member
0 Kudos

Hi SINGHKUMUD,

so somebody has to tell you what key to use for accessing the table, but who?

what about the one who enters the table name? maybe he can provide the key, too?

regards

REA

0 Kudos

>

> In my scenario , the base table is entered as a selection screen parameter and hence I don't know the key.

You can use the FM: 'DDIF_NAMETAB_GET'. Pass the table name i/p from the selection-screen.

In the interface table DFIES_TAB (structure DFIES) you have a field KEYFLAG which will identify the key fields of the table.

BR,

Suhas

0 Kudos

To support Suhas,

SELECT * FROM dd03l INTO TABLE i_dd03l WHERE tabname EQ wf_tab.

IF sy-subrc EQ 0.

SORT i_dd03l BY position ASCENDING.

DELETE i_dd03l WHERE fieldname CP '.INCLU*'.

ENDIF.

The field keyflag will be 'X' for the key fields.

@Suhas - I have faced some similar problem like this, since the key fields are dynamic its very hard to find the duplicate records in the internal table. I think the OP's requirement is somethign similar to this.

0 Kudos

Hi,

THanks for the reply.

With the help of FM I can get the key fields but then how do I form the select statement dynamically.

Lets say:

select * from table db1 where (key1) = wa-key1 etc. etc.

0 Kudos

>

> but then how do I form the select statement dynamically

By searching in the forums, using F1 help etc. I feel this is quite a basic question.

0 Kudos

Hello SINGHKUMUD ,

DATA: select_condition TYPE string.

" fill your custom selection condition here, for example
select_condition = ' key = XXX'.
select * from table db1 where (select_condition).

Best Regards,

Jerry

0 Kudos

Hey, thanks,

I think you got my exact question...will try this out and award points.

jrg_wulf
Active Contributor
0 Kudos

Hi Singhkumud,

maybe this gives you an idea to solve your request:


data: a_ref type ref to CL_ABAP_STRUCTDESCR.
data: comptab TYPE ABAP_COMPONENT_TAB,
         a_component like line of comptab.
a_ref ?= CL_ABAP_STRUCTDESCR=>DESCRIBE_BY_DATA( <fs_outtab> ).
comptab = a_ref->get_components( ).

in comptab-name you'll have the names of all actual fields of your given structure, so you could go for st like this


read TABLE comptab into a_component index 1.
read TABLE <fs_outtab> with key (a_component-name) = yourvalue

regards

Jörg

0 Kudos

Hi Jorg,

Thanks for the reply. I tried this out but TYPE DATA_COMPONENT_TAB does not exist. It throws a syntax error.

jrg_wulf
Active Contributor
0 Kudos

Hi Singhkumud,

ABAP_COMPONENT_TAB is defined in type pool abap, so you'll have to say

TYPE-POOLS: abap.

in your prog.

regards

Jörg

0 Kudos

Hi Jorg,

I did use that....... TYPE-POOLS: ABAP.

Still I got the error.

I am using 4.6C version of SAP.

jrg_wulf
Active Contributor
0 Kudos

OK, for 4.6C forget about the class-methods.

Still you could use


TYPE-POOLS: sydes.
DATA: a_descr type sydes_descr.

DESCRIBE FIELD <fs_outtab> into a_descr.

Now a_descr contains 2 tables, types and names, which hold similar informations.

For further details plz refer to help-text of describe.

regards

Jörg

Former Member
0 Kudos

You can use the following code.

DATA: IT_TAB TYPE STANDARD TABLE OF S_MOVIE .

field-symbols: <fs_movie> TYPE STANDARD TABLE.

field-symbols: <WA_movie> LIKE LINE OF IT_TAB.

SELECT AWARDYEAR CATEGORY WINNER NOTES FROM YMOVIE INTO TABLE IT_TAB.

ASSIGN IT_TAB TO <fs_MOVIE>.

LOOP AT <fs_MOVIE> ASSIGNING <WA_movie>.

WRITE 😕 <WA_movie>-AWARDYEAR, <WA_movie>-CATEGORY, <WA_movie>-WINNER, <WA_movie>-NOTES.

ENDLOOP.

With Regards