Home ABAP Tutorials Dynamic Programming Dynamic DB operation
Dynamic DB operation PDF Print E-mail
User Rating: / 1
PoorBest 
ABAP Tutorials - Dynamic Programming
Written by Varun Verma   
Saturday, 16 August 2008 20:37

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.

Comments
Add New Search
Write comment
Name:
Email:
 
Title:
UBBCode:
[b] [i] [u] [url] [quote] [code] [img] 
 
 
:angry::0:confused::cheer:B):evil::silly::dry::lol::kiss::D:pinch:
:(:shock::X:side::):P:unsure::woohoo::huh::whistle:;):s
:!::?::idea::arrow:
 
Please input the anti-spam code that you can read in the image.

3.26 Copyright (C) 2008 Compojoom.com / Copyright (C) 2007 Alain Georgette / Copyright (C) 2006 Frantisek Hliva. All rights reserved."

Last Updated ( Saturday, 16 August 2008 21:48 )
 

Advertisement

 

Google Search

Advertisement