Application Development and Automation 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: 
Read only

Problem in inserting the records in a database table

Former Member
0 Likes
564

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

2 REPLIES 2
Read only

Former Member
0 Likes
414

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. "MYDYNTABLE

regards,

amit m.

Edited by: Amit Mittal on Jan 7, 2010 5:05 PM

Read only

Former Member
0 Likes
414

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