‎2013 Dec 04 9:21 AM
Hello,
I would like to write a method to insert data into a database table. The database table name would not be known until runtime.
The table would contain some standard fields. Only these fields should be populated, and the table might contain other fields that should not be populated.
I would like to write something like this :
1) Create a structure [record] that corresponds to the table type.
2) Populate the common fields:
[record]-id = 1.
[record]-status = 'I'.
...
3) Insert record into dtable
INSERT INTO (table_name) VALUES [record].
I have tried to do this using field symbols and creating data objects dynamically, but have not managed to get it to work yet. Can anyone suggest how it could be done?
Kind regards
Steve
‎2013 Dec 04 10:33 AM
Hi Steve,
Try this code.
*&---------------------------------------------------------------------*
* Get structure of an SAP table
*----------------------------------------------------------------------*
form table_structure.
data : it_tabdescr type abap_compdescr_tab,
wa_tabdescr type abap_compdescr.
data : ref_table_descr type ref to cl_abap_structdescr.
* Return structure of the table.
ref_table_descr ?= cl_abap_typedescr=>describe_by_name( p_table ).
it_tabdescr[] = ref_table_descr->components[].
loop at it_tabdescr into wa_tabdescr.
clear wa_fieldcat.
wa_fieldcat-fieldname = wa_tabdescr-name .
wa_fieldcat-datatype = wa_tabdescr-type_kind.
wa_fieldcat-inttype = wa_tabdescr-type_kind.
wa_fieldcat-intlen = wa_tabdescr-length.
wa_fieldcat-decimals = wa_tabdescr-decimals.
append wa_fieldcat to it_fieldcat.
endloop.
endform.
*&---------------------------------------------------------------------*
* Create internal table dynamically
*----------------------------------------------------------------------*
form create_itab.
* Create dynamic internal table and assign to Field-Symbol
call method cl_alv_table_create=>create_dynamic_table
EXPORTING
it_fieldcatalog = it_fieldcat
IMPORTING
ep_table = dyn_table.
assign dyn_table->* to <fs_table>.
* Create dynamic work area and assign to Field Symbol
create data dyn_line like line of <fs_table>.
assign dyn_line->* to <fs_wa>.
endform.
*&---------------------------------------------------------------------*
* Populate dynamic itab
*----------------------------------------------------------------------*
form fetch_data.
select * into CORRESPONDING FIELDS OF TABLE <fs_table>
from (p_table).
endform.
Hope this works.
Regards,
Josh
‎2013 Dec 04 9:33 AM
Hi steve,
you could write like,
PARAMETERS: tablenamme LIKE dd02l-tabname.
DATA: link TYPE REF TO data.
FIELD-SYMBOLS: <fs> TYPE ANY.
CREATE DATA link TYPE (tablename).
ASSIGN link->* TO <fs>.
"During runtime <fs> will be filled by the structure by the table you enter.
INSERT INTO (table) VALUES <fs>.
Regards,
sivaganesh
‎2013 Dec 04 11:56 AM
Many thanks Sivaganesh.
That was the kind of thing I attempted. However I'm stuck between
ASSIGN link->* TO <fs>.
and
INSERT INTO (table) VALUES <fs>.
I'd like know how to set <fs> to contain the values I require before inserting into the datatable, i.e.
<fs_wa>-id = im_id.
<fs_wa>-status = 'Initial'.
I would like to be able to populate the fields in the structure contained in <fs>.
Kind regards
Steve
‎2013 Dec 04 10:33 AM
Hi Steve,
Try this code.
*&---------------------------------------------------------------------*
* Get structure of an SAP table
*----------------------------------------------------------------------*
form table_structure.
data : it_tabdescr type abap_compdescr_tab,
wa_tabdescr type abap_compdescr.
data : ref_table_descr type ref to cl_abap_structdescr.
* Return structure of the table.
ref_table_descr ?= cl_abap_typedescr=>describe_by_name( p_table ).
it_tabdescr[] = ref_table_descr->components[].
loop at it_tabdescr into wa_tabdescr.
clear wa_fieldcat.
wa_fieldcat-fieldname = wa_tabdescr-name .
wa_fieldcat-datatype = wa_tabdescr-type_kind.
wa_fieldcat-inttype = wa_tabdescr-type_kind.
wa_fieldcat-intlen = wa_tabdescr-length.
wa_fieldcat-decimals = wa_tabdescr-decimals.
append wa_fieldcat to it_fieldcat.
endloop.
endform.
*&---------------------------------------------------------------------*
* Create internal table dynamically
*----------------------------------------------------------------------*
form create_itab.
* Create dynamic internal table and assign to Field-Symbol
call method cl_alv_table_create=>create_dynamic_table
EXPORTING
it_fieldcatalog = it_fieldcat
IMPORTING
ep_table = dyn_table.
assign dyn_table->* to <fs_table>.
* Create dynamic work area and assign to Field Symbol
create data dyn_line like line of <fs_table>.
assign dyn_line->* to <fs_wa>.
endform.
*&---------------------------------------------------------------------*
* Populate dynamic itab
*----------------------------------------------------------------------*
form fetch_data.
select * into CORRESPONDING FIELDS OF TABLE <fs_table>
from (p_table).
endform.
Hope this works.
Regards,
Josh
‎2013 Dec 04 11:50 AM
Hello Josh
Many thanks for your reply.
I have modified your code to dynamically create a table, but I'm still not sure how I can append a new record.
In the example you populate the table using a SELECT. Instead of this, I'd like to set the fields of the work area, <fs_wa> using something like :
<fs_wa>-id = im_id.
<fs_wa>-status = 'Initial'.
and then append to the table.
But this is not possible because SAP does not know the structure of the field symbol, and so does not recognise the component called ID.
Kind regards
Steve
‎2013 Dec 04 11:59 AM
Hi Steve
You can use it_fieldcat in this case. As it contains the structure of your table. You can do a loop at it_fieldcat and then use Assign (lwa_fieldcat-fieldname) of <FS> to <FS1>
if sy-subrc eq 0.
<FS1> = value.
endif
append <FS> to <FT>
Nabheet
‎2013 Dec 04 12:45 PM
Hi Steve,
Do you know the field names as id and status?
Then you can do like this.
field-symbols <fs1>.
assign component 'ID' of structure <Fs_wa> to <fs1>.
if <fs1> is assigned.
<fs1> = im_id.
unassign <fs1>.
endif.
assign component 'STATUS' of structure <Fs_wa> to <fs1>.
if <fs1> is assigned.
<fs1> = 'Initial'.
unassign <fs1>.
endif.
‎2013 Dec 04 1:01 PM
Thank you all for your help. Susmitha's answer is what I was looking for,
I think perhaps I should have asked a question about field symbols rather than dynamic SQL.
Kind regards
Steve
‎2014 Sep 12 2:05 AM
I am tryign to do something similar , but I do not know the names of the fields. How can I go about solving that? Please someone reply back. Its an urgent issue for me to solve.
‎2014 Sep 12 7:35 AM
Hello Meghna
You can get the names of structure components dynamically.
There are a few examples at the end of this thread:
http://scn.sap.com/thread/731877
regards
Steve