We get frequent requirements to create (DDL operation) data base / dictionary table or structure for example, through ABAP program.
Please use section (1). Reusable source code.
1. Call FM 'DDIF_TABL_PUT' for table or structure creation
2. Call FM 'DDIF_TABL_ACTIVATE' only for transparent table activation – This step is NOT applicable for dictionary structure.
It’s also needed a mechanism for DML operation like insert data, select data or delete operation to dictionary table. Sample source code mention in the section (2).
1. Source code for dictionary table/structure creation dynamically:
*** Data declaration****
DATA lt_fields TYPE TABLE OF dd03p,
wa_field TYPE dd03p,
table_header TYPE dd02v,
techn_set TYPE dd09v.
DATA: TAB TYPE CHAR30.
*** Fill Table Header ***
table_header-tabname = Sy-repid.
table_header-ddtext = 'Data'.
table_header-ddlanguage = sy-langu.
table_header-tabclass = 'TRANSP'. “INTTAB – for structure
table_header-as4user = sy-uname.
table_header-contflag = 'A'.
table_header-mainflag = 'X'.
*** technical settings ***
techn_set-tabname = Sy-repid.
techn_set-tabkat = 0.
techn_set-tabart = 'APPL1'.
techn_set-bufallow = 'X'.
techn_set-pufferung = 'X'.
*** Field Information***
wa_field-tabname = sy-repid.
wa_field-ddlanguage = sy-langu.
wa_field-notnull = 'X'.
wa_field-keyflag = 'X'.
wa_field-fieldname = 'TYPE'.
wa_field-position = 2.
wa_field-rollname = 'CHAR50'.
APPEND wa_field TO lt_fields.
wa_field-tabname = sy-repid.
wa_field-ddlanguage = sy-langu.
wa_field-notnull = 'X'.
wa_field-keyflag = 'X'.
wa_field-fieldname = 'VALUE'.
wa_field-position = 3.
wa_field-rollname = 'CHAR50'.
APPEND wa_field TO lt_fields.
*** Call FM for table/structure creation ***
TAB = SY-REPID.
CALL FUNCTION 'DDIF_TABL_PUT'
EXPORTING
name = TAB
dd02v_wa = table_header
dd09l_wa = techn_set
TABLES dd03p_tab = lt_fields " Table fields
EXCEPTIONS
tabl_not_found = 1
name_inconsistent = 2
tabl_inconsistent = 3
put_failure = 4
put_refused = 5
OTHERS = 6.
IF sy-subrc = 0.
*** Activating the Table (Only transparent table) (not required for structure) ***
CALL FUNCTION 'DDIF_TABL_ACTIVATE'
EXPORTING
name = TAB
EXCEPTIONS
not_found = 1
put_failure = 2
OTHERS = 3.
ENDIF.
2. Access dictionary table dynamically:
Data: TABLENAME TYPE TABNAME.
DATA: W_TABNAME TYPE W_TABNAME.
FIELD-SYMBOLS: <DBTAB> TYPE W_TABNAME.
TABLENAME = SY-REPID.
ASSIGN TABLENAME TO <DBTAB> .
MODIFY (,DBTAB>) FROM TABLE IT_RECORD.
COMMIT WORK.
SELECT * FROM (<DBTAB>) INTO CORRESPONDING FIELDS OF TABLE IT_RECORD.
DELETE FROM (<DBTAB>).