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 into dbtab from dynamic internal table

Former Member
0 Likes
3,346

Hello All,

I am want to insert data into a database table from a dynamic internal table. I am using the below syntax:

INSERT INTO (<dbtab>) FROM TABLE itab.

Where this dbtab will be taken as input in selection screen of the program and the itab will be a internal table of the same type. I am able to pass database table name dynamically but not able for itab.

It seems SAP does not allow Field-Symbols in INSERT statement and also not a Reference Object.

Do you have any other solution for this?

Thanks & Regards,

Sumanas

1 ACCEPTED SOLUTION
Read only

brunobex
Active Participant
0 Likes
1,830

Hi Sumanas,

I did a test with a table and ZTEST and ZTESTE2. You can try the code below:

See if that's what you want.

I modified these variables via debug for this test.


REPORT  zteste.

PARAMETERS: p_tab1 TYPE dd03l-tabname OBLIGATORY,
            p_tab2 TYPE dd03l-tabname OBLIGATORY.

DATA: v_data_ref TYPE REF TO data.

DATA: v_inter_tab TYPE dd03l-tabname.

FIELD-SYMBOLS: <f_tab> TYPE STANDARD TABLE.

START-OF-SELECTION.

*TABLE 1
  UNASSIGN <f_tab>.
  CREATE DATA v_data_ref TYPE TABLE OF (p_tab1).
  ASSIGN v_data_ref->* TO <f_tab>.
  IF <f_tab> IS ASSIGNED.

      PERFORM f_insert USING p_tab1
                             <f_tab>.
  ENDIF.

*TABLE 2
  CLEAR v_data_ref.
  UNASSIGN <f_tab>.
  CREATE DATA v_data_ref TYPE TABLE OF (p_tab2).
  ASSIGN v_data_ref->* TO <f_tab>.
  IF <f_tab> IS ASSIGNED.

      PERFORM f_insert USING p_tab2
                             <f_tab>.
  ENDIF.
*&---------------------------------------------------------------------*
*&      Form  F_INSERT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_V_TAB  text
*      -->P_<F_TAB>  text
*----------------------------------------------------------------------*
FORM f_insert  USING    pv_tab TYPE dd03l-tabname
                        pt_tab TYPE STANDARD TABLE.

  INSERT (pv_tab) FROM TABLE pt_tab.
ENDFORM.                    " F_INSERT

Regards

Bruno Xavier.

4 REPLIES 4
Read only

brunobex
Active Participant
0 Likes
1,831

Hi Sumanas,

I did a test with a table and ZTEST and ZTESTE2. You can try the code below:

See if that's what you want.

I modified these variables via debug for this test.


REPORT  zteste.

PARAMETERS: p_tab1 TYPE dd03l-tabname OBLIGATORY,
            p_tab2 TYPE dd03l-tabname OBLIGATORY.

DATA: v_data_ref TYPE REF TO data.

DATA: v_inter_tab TYPE dd03l-tabname.

FIELD-SYMBOLS: <f_tab> TYPE STANDARD TABLE.

START-OF-SELECTION.

*TABLE 1
  UNASSIGN <f_tab>.
  CREATE DATA v_data_ref TYPE TABLE OF (p_tab1).
  ASSIGN v_data_ref->* TO <f_tab>.
  IF <f_tab> IS ASSIGNED.

      PERFORM f_insert USING p_tab1
                             <f_tab>.
  ENDIF.

*TABLE 2
  CLEAR v_data_ref.
  UNASSIGN <f_tab>.
  CREATE DATA v_data_ref TYPE TABLE OF (p_tab2).
  ASSIGN v_data_ref->* TO <f_tab>.
  IF <f_tab> IS ASSIGNED.

      PERFORM f_insert USING p_tab2
                             <f_tab>.
  ENDIF.
*&---------------------------------------------------------------------*
*&      Form  F_INSERT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_V_TAB  text
*      -->P_<F_TAB>  text
*----------------------------------------------------------------------*
FORM f_insert  USING    pv_tab TYPE dd03l-tabname
                        pt_tab TYPE STANDARD TABLE.

  INSERT (pv_tab) FROM TABLE pt_tab.
ENDFORM.                    " F_INSERT

Regards

Bruno Xavier.

Read only

Former Member
0 Likes
1,830

Hi Bruno,

Good code and many thanks for this.

I hope Sumanas is looking to insert the values from local internal table.

In that case, can we create a reference to ITAB and then pass it to respective DB table??

Regards,

Raja.D

Read only

Former Member
0 Likes
1,830

Thanks Bruno. It was very helpful.

Read only

Former Member
0 Likes
1,830

Hi guys,

I'm also trying to do an INSERT (l_nametab) FROM TABLE <dynamic_table>.

I was following the Bruno's code but I received this runtime error:

DBIF_RSQL_INTERNAL_ERROR

Internal error when accessing a table.

The current ABAP/4 program terminated due to

an internal error in the database interface.

An internal error in the database interface occurred during access to

the data of table "PA0002 ".

The situation points to an internal error in the SAP software

or to an incorrect status of the respective work process.

For further analysis the SAP system log should be examined

(transaction SM21).

For a precise analysis of the error, you should supply

documents with as many details as possible.

DATA: l_nametab TYPE TABNAME VALUE 'PA0002'.

FIELD-SYMBOLS: <dynamic_table> TYPE STANDARD TABLE.

DATA: dy_table2 type ref to data.

CREATE DATA dy_table2 TYPE STANDARD TABLE OF (l_nametab).

ASSIGN dy_table2->* TO <dynamic_table>

If you have some Ideas about this error, I'll really apreciate it.

Regards.