|
Dynamic DB operations are relatively simpler to do. Let's say you have to select / insert / update / delete from a DB table and the name of the DB table is not known in advance. The trick here is that since the name of the DB table is not known, the data structures can also be not declared easily. They have to be declared generically. One way is to make use of field symbols. Thus depending upon the DB type, the structures / internal tables will be created and used. PS: It is important that you are comfortable with the use field symbols in ABAP.
Let's say the name of the DB table is stored in variabe lv_table_name. At run time the lv_table_name can have any value. And I want to select data from this table and store it in a internal table. See the below code snippet. Check out the data declaration and DB operation statements. FIELD-SYMBOLS : <fs_table_data> TYPE ANY TABLE, <fs_wa> TYPE ANY.
DATA : lref_data_table TYPE REF TO data, lref_data_wa TYPE REF TO data.
CREATE DATA lref_data_table TYPE STANDARD TABLE OF (lv_table_name). ASSIGN lref_data_table->* TO <fs_table_data>.
CREATE DATA lref_data_wa TYPE (lv_table_name) ASSIGN lref_data_wa->* TO <fs_wa>.
SELECT * FROM (lv_table_name) INTO TABLE <fs_table_data>. SELECT SINGLE * FROM (lv_table_name) INTO <fs_wa>.
INSERT (lv_table_name) FROM TABLE <fs_table_data>. MODIFY (lv_table_name) FROM TABLE <fs_table_data>. DELETE (lv_table_name) FROM TABLE <fs_table_data>.Here, a field symbol is declared, which can take any type. A data variable which refers to generic data type is declared. Then a object of this generic type is created which is typed to the table name. Now the field symbol is assigned to the this data object. Now the field symbol works just like a normal internal table (or a work area). After that the DB operations are simple .. Write a simple program and check the values in debugging yourself. Just in case you can't get it working, refer to the code example given in the examples section.
|