‎2010 Jan 07 11:23 AM
Hi Experts,
I am facing problem to insert the entry in a table.
I am getting the database table name as import parameter of function module and the records to be inserted are in a table structure of type ANY as import parameter.
I need to insert these enteries in the table entered as import parameter.
Can you suggest how should I typecast records enterd in table sructure (type ANY) to the type database table in import parameter to insert the records.
Regards,
Pravesh
‎2010 Jan 07 11:32 AM
Hi pravesh,
..................
(The post is not appearing properly......)
This code is not exact, but similar.
On selection screen it will show a field for table name.
It will dynamically select from this table, and will also show the fields.
At the time of showing, I have also written one line to include your requirement,
where it inserts / updates the table record by record.
May be this can help.
(Please make sure you use this with care, and do not use any standard table in the screen, other wise it might change it).
REPORT abc.*------------- COMPULSORY
FIELD-SYMBOLS: <dyntable> TYPE ANY TABLE.
FIELD-SYMBOLS: <dynline> TYPE ANY.
DATA: lt TYPE lvc_t_fcat.
DATA: ls TYPE lvc_s_fcat.
FIELD-SYMBOLS: <fld> TYPE ANY.
DATA : fldname(50) TYPE c.
*--------------------
parameters : tabname LIKE dd02l-tabname DEFAULT 'T001' OBLIGATORY.
*-----------------------------------------------
START-OF-SELECTION.
*-------------- PERFORM
PERFORM mydyntable USING lt.
PERFORM GET_DATA.
PERFORM SHOW_DATA.
BREAK-POINT.
*---------------------------------------------------------------
* FORM
*---------------------------------------------------------------
FORM GET_DATA.
SELECT * FROM (TABNAME)
INTO TABLE <DYNTABLE>.
ENDFORM. "GET_DATA
*---------------------------------------------------------------
* FORM
*---------------------------------------------------------------
FORM SHOW_DATA.
loop at <dyntable> ASSIGNING <dynline>.
sy-subrc = 0.
write :/ .
WHILE SY-SUBRC = 0.
ASSIGN COMPONENT SY-INDEX OF STRUCTURE <DYNLINE> TO <FLD>.
WRITE : <FLD>.
ENDWHILE.
endloop.
ENDFORM. "SHOW_DATA
*---------------------------------------------------------------
* INDEPENDENT FORM
*---------------------------------------------------------------
FORM mydyntable USING ptabname.
*-------------- Create Dyn Table From FC
FIELD-SYMBOLS: <fs_data> TYPE REF TO data.
FIELD-SYMBOLS: <fs_1>.
FIELD-SYMBOLS: <fs_2> TYPE ANY TABLE.
DATA: lt_data TYPE REF TO data.
data : lt TYPE lvc_t_fcat .
DATA : ddfields LIKE ddfield OCCURS 0 WITH HEADER LINE.
*-------------------------------------------
CALL FUNCTION 'DD_NAMETAB_TO_DDFIELDS'
EXPORTING
tabname = tabname
TABLES
ddfields = ddfields.
.
*------------- CONSTRUCT FIELD LIST
LOOP AT ddfields.
ls-fieldname = ddfields-fieldname.
APPEND ls TO lt.
ENDLOOP.
ASSIGN lt_data TO <fs_data>.
CALL METHOD cl_alv_table_create=>create_dynamic_table
EXPORTING
it_fieldcatalog = lt
IMPORTING
ep_table = <fs_data>
EXCEPTIONS
generate_subpool_dir_full = 1
OTHERS = 2.
IF sy-subrc <> 0.
ENDIF.
*------------- Assign Dyn Table To Field Sumbol
ASSIGN <fs_data>->* TO <fs_1>.
ASSIGN <fs_1> TO <fs_2>.
ASSIGN <fs_1> TO <dyntable>.
ENDFORM. "MYDYNTABLEregards,
amit m.
Edited by: Amit Mittal on Jan 7, 2010 5:05 PM
‎2010 Jan 20 7:21 AM
Hi,
do You need to typecast your table? Why not to do it like that:
PERFORM insert_table_to_db USING 'TAB1' gt_tab1.
FORM insert_table_to_db USING uv_table_name TYPE string
ut_table TYPE ANY TABLE.
INSERT (uv_table_name) FROM TABLE ut_table.
ENDFORM.You can also catch exception like SAPSQL_WA_TOO_SMALL and others which can occur.
Regards,
Adrian