cancel
Showing results for 
Search instead for 
Did you mean: 

Inserting new records into database table at runtime

Former Member
0 Kudos

Hi all ,

How to insert new records into database table at runtime on click update?

Thanks.

Accepted Solutions (1)

Accepted Solutions (1)

uday_gubbala2
Active Contributor
0 Kudos

Hi Sasikala,

Just for your understanding am giving a sample code snippet which you can use to read the contents of your Table UI element & save the data on to your database. Suppose you have a button up on pressing which you want to read the data from your screens table & save on to the database then you can proceed as shown below:

1) Obtain the reference of your context node.

2) Fetch all the data present in your table into an internal table using methods of if_wd_context_node

3) Use your normal ABAP logic to update the database table with the data from your internal table

In my example I have a node by name SFLIGHT_NODE and under this I have the desired attributes from SFLIGHT. Am displaying these in an editable table & the user would press up on a push button after making the necessary changes to the tables data. I would then need to obtain the tables information & save on to the database.

data: node_sflight           type ref to if_wd_context_node,
        elem_sflight           type ref to if_wd_context_element,
        lt_elements            type WDR_CONTEXT_ELEMENT_SET,
       stru_sflight           type if_main=>element_sflight_node,
       it_flights             type if_main=>elements_sflight_node.
 
"   navigate from <CONTEXT> to <SFLIGHT_NODE> via lead selection
    node_sflight_node = wd_context->get_child_node( name = 'SFLIGHT_NODE'  ).
 
   lt_elements = node_sflight->get_elements( ).
 
"   Get all the rows from the table for saving on to the database
    loop at lt_elements into elem_sflight.
      elem_sflight->get_static_attributes( importing static_attributes = stru_sflight ).
      append stru_sflight to it_flights.
    endloop.
 
" Finally save the entries on to the database 
    modify ZSFLIGHT99 from table it_flights.
    if sy-subrc eq 0.
endif.

However a word of caution here.... SAP doesn't ever recommend directly modifying the database through an SQL query. You would preferably make use of a BAPI for the same. Try go through Thomas Jung's comments in [here|;.

Regards,

Uday

Answers (2)

Answers (2)

Former Member
0 Kudos

Thanks UDAY.

uday_gubbala2
Active Contributor
0 Kudos

Hi Sasikala,

Please try search through the forum before you post any new queries as this issue has been discussed earlier many number of times.

I guess you have an editable table on your views layout & you want to save the tables entries on to the database. When you make any kind of changes to your tables data (insert/delete/change) all these changes get reflected in your context node. So when you want to save these entries into database, just read your context nodes contents by using get_static_attributes_table. Then use this internal table to modify your database table.

Regards,

Uday