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

Inserting data to a database table from a dynamic internal table?

Former Member
0 Likes
1,229

Hi,

I have a dynamic internal table called <dyn_table> in which I have data. I want to move all the values from this internal table to a custom database table ZREAL. How do I go about it. Please help ASAP. Points would be awarded for it.

Thanks in advance.

2 REPLIES 2
Read only

Former Member
0 Likes
607

Check this custom FM...You can found it on <a href="/people/alvaro.tejadagalindo/blog/2006/03/03/tasting-the-mix-of-php-and-sap--volume-2 the mix of PHP and SAP - Volume 2</a>


FUNCTION ZRFC_TABLE_OPERATIONS_LINE.
*"----------------------------------------------------------------------
*"*"Interfase local
*"  IMPORTING
*"     VALUE(QUERY_TABLE) LIKE  DD02L-TABNAME
*"     VALUE(OPERATION) LIKE  BDLDATCOL-TYPE
*"     VALUE(FIELDS) LIKE  ZTABLE_OPERATION-FIELDS
*"     VALUE(DATA) LIKE  ZTABLE_OPERATION-DATA
*"  EXCEPTIONS
*"      RECORD_NOT_INSERTED
*"      RECORD_NOT_UPDATED
*"      RECORD_NOT_DELETED
*"      NOT_AUTHORIZED
*"      TABLE_NOT_AVAILABLE
*"----------------------------------------------------------------------

  CALL FUNCTION 'VIEW_AUTHORITY_CHECK'
    EXPORTING
      VIEW_ACTION                    = 'S'
      VIEW_NAME                      = QUERY_TABLE
    EXCEPTIONS
      NO_AUTHORITY                   = 2
      NO_CLIENTINDEPENDENT_AUTHORITY = 2
      NO_LINEDEPENDENT_AUTHORITY     = 2
      OTHERS                         = 1.

  IF SY-SUBRC = 2.
    RAISE NOT_AUTHORIZED.
  ELSEIF SY-SUBRC = 1.
    RAISE TABLE_NOT_AVAILABLE.
  ENDIF.

*=======================================================================
* Types.
*=======================================================================
  TYPES: X_LINES TYPE STRING.

*=======================================================================
* Internal Tables.
*=======================================================================
  DATA: DESCR_STRUCT_REF TYPE REF TO CL_ABAP_STRUCTDESCR,
        DATAREF TYPE REF TO DATA,
        WA_FCAT TYPE LVC_S_FCAT,
        IT_FIELDCATALOG TYPE LVC_T_FCAT,
        T_LINES TYPE STANDARD TABLE OF X_LINES,
        T_FIELDS TYPE STANDARD TABLE OF X_LINES WITH HEADER LINE,
        NEW_LINE  TYPE REF TO DATA.

  DATA: W_LINES TYPE STRING.

*=======================================================================
* Variables.
*=======================================================================
  DATA: W_TABIX LIKE SY-TABIX.

*=======================================================================
* Field-Symbols.
*=======================================================================
  FIELD-SYMBOLS:
                <TABLE>,
                <COMPONENT> TYPE ABAP_COMPDESCR,
                <FS>  TYPE ANY,
                <FIELD> TYPE ANY,
                <LINE> TYPE ANY,
                <DYN_TABLE> TYPE STANDARD TABLE,
                <DYN_WA>.

*=======================================================================
* Create a dynamic internal table.
*=======================================================================
  CREATE DATA DATAREF TYPE (QUERY_TABLE).

  ASSIGN DATAREF->* TO <FS>.

  DESCR_STRUCT_REF ?= CL_ABAP_TYPEDESCR=>DESCRIBE_BY_DATA( <FS> ).

  LOOP AT DESCR_STRUCT_REF->COMPONENTS ASSIGNING <COMPONENT>.
    WA_FCAT-FIELDNAME     = <COMPONENT>-NAME.
    WA_FCAT-REF_TABLE     = QUERY_TABLE.
    WA_FCAT-REF_FIELD     = <COMPONENT>-NAME.
    APPEND WA_FCAT TO IT_FIELDCATALOG.
  ENDLOOP.

  CALL METHOD CL_ALV_TABLE_CREATE=>CREATE_DYNAMIC_TABLE
    EXPORTING
      IT_FIELDCATALOG           = IT_FIELDCATALOG
    IMPORTING
      EP_TABLE                  = DATAREF.
*    EXCEPTIONS
*      GENERATE_SUBPOOL_DIR_FULL = 1
*      OTHERS                    = 2.

  ASSIGN DATAREF->* TO <DYN_TABLE>.

  CREATE DATA NEW_LINE LIKE LINE OF <DYN_TABLE>.
  ASSIGN NEW_LINE->* TO <DYN_WA>.

*=======================================================================
* Fill the dynamic internal table.
*=======================================================================
    SPLIT DATA AT '/' INTO TABLE T_LINES.
    SPLIT FIELDS AT '/' INTO TABLE T_FIELDS.
    LOOP AT T_LINES ASSIGNING <LINE>.
      W_TABIX = SY-TABIX.
      W_LINES = <LINE>.
      READ TABLE T_FIELDS INDEX W_TABIX.
      ASSIGN T_FIELDS TO <FIELD>.
      ASSIGN COMPONENT <FIELD> OF STRUCTURE <DYN_WA> TO <TABLE>.
      <TABLE> =  W_LINES.
    ENDLOOP.

    APPEND <DYN_WA> TO <DYN_TABLE>.

*============================== ========================================
* Perform task depending of the choose operation.
*=======================================================================
    CASE OPERATION.
      WHEN 'I'.
        MODIFY (QUERY_TABLE) FROM TABLE <DYN_TABLE>.
        IF SY-SUBRC NE 0.
          RAISE RECORD_NOT_INSERTED.
        ENDIF.
      WHEN 'U'.
        UPDATE (QUERY_TABLE) FROM TABLE <DYN_TABLE>.
        IF SY-SUBRC NE 0.
          RAISE RECORD_NOT_UPDATED.
        ENDIF.
      WHEN 'D'.
        DELETE (QUERY_TABLE) FROM TABLE <DYN_TABLE>.
        IF SY-SUBRC NE 0.
          RAISE RECORD_NOT_DELETED.
        ENDIF.
    ENDCASE.

ENDFUNCTION.

Greetings,

Blag.

Read only

Former Member
0 Likes
607

Hi Narendra,

After completion of alll validations and populate the data into the final internal table then move each record into one work area and from work area just insert each reocord.

LOOP AT ITAB INTO WA.

INSERT ZREAL FROM WA.

CLEAR ITAB.

CLEAR WA.

ENDLOOP.

hope this helps you.

Regards,

Kumar.