03-01-2007 1:19 PM
Hello,
I have a table with one field, n records. For each record of the table I have to dynamically create a data object of type table-field. How can I do that?
Thank you!
03-01-2007 1:23 PM
03-01-2007 1:24 PM
There are two ways as follows.
1 .For WAS 6.2 above
report yes_tjung_sflight_test.
Data Declaration
data: sflighttype type ref to cl_abap_structdescr,
tabletype type ref to cl_abap_tabledescr,
comp_tab type cl_abap_structdescr=>component_table,
new_comp_tab like comp_tab,
linetype type ref to cl_abap_structdescr,
dref type ref to data.
Field Symbols
field-symbols: <wa_comp> like line of comp_tab.
field-symbols: <table> type any table.
sflighttype ?= cl_abap_typedescr=>describe_by_name('SFLIGHT').
comp_tab = sflighttype->get_components( ).
loop at comp_tab assigning <wa_comp>.
case <wa_comp>-name.
when 'CARRID' or 'CONNID' or 'FLDATE' or 'PRICE' or 'CURRENCY'.
append <wa_comp> to new_comp_tab.
endcase.
endloop.
linetype = cl_abap_structdescr=>create( new_comp_tab ).
tabletype = cl_abap_tabledescr=>create(
p_line_type = linetype
p_table_kind = cl_abap_tabledescr=>tablekind_std ).
create data dref type handle tabletype.
assign dref->* to <table>.
select * from sflight into corresponding fields of table <table>.
field-symbols: <wa_data> type any.
field-symbols: <wa_field> type any.
loop at <table> assigning <wa_data>.
write: /.
loop at new_comp_tab assigning <wa_comp>.
assign component sy-tabix of structure <wa_data> to <wa_field>.
write: <wa_field>.
endloop.
endloop.
2 .For WAS 6.2
Tables
DATA: LT_DATA type ref to DATA.
DATA: LT_FIELDCATALOG type LVC_T_FCAT.
Structure
DATA: LS_FIELDCATALOG type LVC_S_FCAT.
Data References
DATA: NEW_LINE type ref to data.
Field Symbols
FIELD-SYMBOLS: <FS_DATA> type ref to DATA,
<FS_1> type any table,
<FS_2>,
<FS_3>.
LS_FIELDCATALOG-FIELDNAME = 'MANDT'.
append LS_FIELDCATALOG to LT_FIELDCATALOG.
LS_FIELDCATALOG-FIELDNAME = 'CARRID'. "Fieldname
LS_FIELDCATALOG-INTTYPE = 'C'. "Internal Type C-> Character
append LS_FIELDCATALOG to LT_FIELDCATALOG.
LS_FIELDCATALOG-FIELDNAME = 'CONNID'.
LS_FIELDCATALOG-INTTYPE = 'N'.
append LS_FIELDCATALOG to LT_FIELDCATALOG.
LS_FIELDCATALOG-FIELDNAME = 'FLDATE'.
LS_FIELDCATALOG-INTTYPE = 'D'.
append LS_FIELDCATALOG to LT_FIELDCATALOG.
LS_FIELDCATALOG-FIELDNAME = 'PRICE'.
LS_FIELDCATALOG-INTTYPE = 'P'.
append LS_FIELDCATALOG to LT_FIELDCATALOG.
LS_FIELDCATALOG-FIELDNAME = 'CURRENCY'.
LS_FIELDCATALOG-INTTYPE = 'C'.
append LS_FIELDCATALOG to LT_FIELDCATALOG.
assign LT_DATA to <FS_DATA>.
call method cl_alv_table_create=create_dynamic_table
exporting
it_fieldcatalog = LT_FIELDCATALOG
importing
ep_table = FS_DATA
exceptions
generate_subpool_dir_full = 1
others = 2
.
if sy-subrc <> 0.
endif.
So <FS_1> now points to our dynamic internal table.
assign <FS_DATA>->* to <FS_1>.
Next step is to create a work area for our dynamic internal table.
create data NEW_LINE like line of <FS_1>.
A field-symbol to access that work area
assign NEW_LINE->* to <FS_2>.
And to put the data in the internal table
select MANDT CARRID CONNID FLDATE PRICE CURRENCY
from SFLIGHT
into corresponding fields of table <FS_1>.
Access contents of internal table
loop at <FS_1> assigning <FS_2>.
assign component 1 of structure <FS_2> to <FS_3>.
write: / <FS_3>.
endloop.
data: lv_tablename type string value 'SFLIGHT'.
data: lv_dref type ref to DATA.
CREATE DATA lv_dref type table of (lv_tablename).
FIELD-SYMBOLS: <FS> TYPE STANDARD TABLE.
ASSIGN lv_dref->* TO <FS>.
select *
from sflight
into table <FS>.
Please reward if useful.
03-05-2007 7:31 AM
I cannot use field symbols.
I need dynamic data objects because I am calling a FM with dynamic parameters (enqueue/dequeue). So, I have to use the parameter-table option of the call function statement. Filling the parameters table looks like this.
data: p_lt_enq_para type abap_func_parmbind_tab,
lwa_enq LIKE LINE OF p_lt_enq_para
lwa_enq-name = lmode.
lwa_enq-kind = abap_func_exporting.
GET REFERENCE OF <b>"needed_variable"</b> INTO lwa_enq-value .
INSERT lwa_enq INTO table p_lt_enq_para.
So, for every line of the parameters table, I need a data object to insert the desired value. Any ideea?
03-05-2007 7:46 AM
Hi George,
Try this :
DATA: numref TYPE REF TO DATA,
number TYPE I VALUE 123.
FIELD-SYMBOLS: <fs> TYPE ANY.
GET REFERENCE OF number INTO numref.
ASSIGN numref->* TO <fs>.
You can find this if you do a F1 help on the "Data" keyword.
Regards,
George.
plz reward points to helpful tips
03-05-2007 8:10 AM
This is the problem, I cannot expicitly declare a data object (like numref or number), because I don't know what I need. I have this information in an internal table.
So, I have to call a FM for locking/unlocking table records. For every table that will be locked/unlocked, two FM are generated ( enqueue_... and dequeue_...). These FMs have different parameters from one table to another, because the key fields of every table are used as parameters for these function modules. So, I have the key fields in one internal table, and I have to fill in the parameters table, creatind data objects at runtime for every key field from my internal table.
How can I do that ?
Is it possible to use 'CREATE DATA ...' statement without having a reference variable statically declared in the source code? Or how can I dynamically declare a reference variable, because I don't know how many I need at run time?
Message was edited by:
George Popescu