06-18-2010 7:21 AM
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.
06-18-2010 7:38 AM
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
06-18-2010 7:38 AM
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
06-18-2010 8:22 AM
Hi,
READ TABLE <itab> ASSIGNING <wa> transporting no fields where k1 = 'N'.
Hope this might help you.
With Regards,
Sumodh.P
06-23-2010 7:29 AM
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.
06-23-2010 11:04 AM
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
06-23-2010 11:28 AM
>
> 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
06-23-2010 12:14 PM
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.
06-25-2010 9:04 AM
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.
06-25-2010 10:10 AM
>
> 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.
06-25-2010 3:55 PM
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
06-28-2010 7:36 AM
Hey, thanks,
I think you got my exact question...will try this out and award points.
06-23-2010 3:56 PM
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
06-28-2010 7:37 AM
Hi Jorg,
Thanks for the reply. I tried this out but TYPE DATA_COMPONENT_TAB does not exist. It throws a syntax error.
06-28-2010 8:17 AM
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
06-28-2010 9:48 AM
Hi Jorg,
I did use that....... TYPE-POOLS: ABAP.
Still I got the error.
I am using 4.6C version of SAP.
06-28-2010 10:15 AM
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
06-28-2010 1:16 PM
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