Datenbank Strukturen zur Laufzeit erzeugen

Wie man dynamisch Strukturen und interne Tabellen zur Laufzeit erzeugen kann, habe ich bereits in einem anderen Artikel beschrieben.

Heute möchte ich zeigen, wie man eine Datenbank Struktur zur Laufzeit erzeugt und diese auch gleich einem Transportauftrag zuweisen kann.

Der unten gezeigte Report besitzt einen Selection Screen mit vier Eingabefeldern. Diese Felder sind :

  • Strukturname
  • Strukturbeschreibung
  • Feldname
  • Datenelement des Feldes

In diesem Beispiel wird eine Struktur mit einem Feld erzeugt. Dies sollte zur Veranschaulichung völlig ausreichend sein.

In der Form create_ddic_structure wird die gewünschte Datenbankstruktur erzeugt und aktiviert. Ab diesem Zeitpunkt ist die Struktur via SE11 auffindbar.

Als nächstes wird mit in der Form write_transport der nötige Transportauftrag erzeugt. Wie gewohnt kann man hier einen bereits existierenden Auftrag auswählen oder einen neuen Auftrag anlegen.

REPORT  create_structure.

TYPE-POOLS: abap.

DATA: wa_structure_data TYPE dd02v.
DATA: lt_table_fields TYPE STANDARD TABLE OF dd03p,
      wa_table_fields TYPE dd03p.

SELECTION-SCREEN: BEGIN OF BLOCK a WITH FRAME.
 PARAMETERS: p_struc TYPE ddobjname.
 PARAMETERS: p_ddtext TYPE char60.
 SELECTION-SCREEN: SKIP 1.
 PARAMETERS: p_field TYPE fieldname.
 PARAMETERS: p_dtype TYPE rollname.
SELECTION-SCREEN: END OF BLOCK a.

START-OF-SELECTION.
 PERFORM create_ddic_structure.
 PERFORM write_transport.

END-OF-SELECTION.

*---------------------------------------------------------------------*
*      Form  create_ddic_structure
*---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM create_ddic_structure.
 DATA: wa_data_head TYPE dd04v,
       wa_data_tec TYPE tpara.
 DATA: obj_data TYPE REF TO data.

* Build structure data
 MOVE p_struc TO wa_structure_data-tabname.
 MOVE 'INTTAB' TO wa_structure_data-tabclass.
 MOVE p_ddtext TO wa_structure_data-ddtext.
 MOVE sy-langu TO wa_structure_data-ddlanguage.

* Build table fields
* Get data type data
 CALL FUNCTION 'DDIF_DTEL_GET'
  EXPORTING
   name          = p_dtype
   langu         = sy-langu
  IMPORTING
   dd04v_wa      = wa_data_head
   tpara_wa      = wa_data_tec
  EXCEPTIONS
   illegal_input = 1
   OTHERS        = 2.

 MOVE-CORRESPONDING wa_data_head TO wa_table_fields.
 MOVE-CORRESPONDING wa_data_tec TO wa_table_fields.
 MOVE p_struc TO wa_table_fields-tabname.
 MOVE p_field TO wa_table_fields-fieldname.
 MOVE sy-langu TO wa_table_fields-ddlanguage.
 MOVE '0001' TO wa_table_fields-position.
 APPEND wa_table_fields TO lt_table_fields.

* Create table in data dictionary
 CALL FUNCTION 'DDIF_TABL_PUT'
  EXPORTING
   name                    = p_struc
   dd02v_wa                = wa_structure_data
  TABLES
   dd03p_tab               = lt_table_fields
  EXCEPTIONS
   tabl_not_found          = 1
   name_inconsistent       = 2
   tabl_inconsistent       = 3
   put_failure             = 4
   put_refused             = 5
   OTHERS                  = 6.

 CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
  EXPORTING
   wait = abap_true.

* Activate table
 CALL FUNCTION 'DDIF_TABL_ACTIVATE'
  EXPORTING
   name        = p_struc
  EXCEPTIONS
   not_found   = 1
   put_failure = 2
   OTHERS      = 3.

 CREATE DATA obj_data TYPE (p_struc).

 CLEAR: wa_structure_data.
ENDFORM.                    "create_ddic_structure

*---------------------------------------------------------------------*
*      Form  write_transport
*---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM write_transport.
 DATA: wa_e070 TYPE e070.
 DATA: lt_e071 TYPE TABLE OF e071,
       wa_e071 TYPE e071,
       lt_e071k TYPE TABLE OF e071k,
       wa_e071k TYPE e071k.
 DATA: lv_order TYPE trkorr,
       lv_task TYPE trkorr.
 DATA: wa_tadir TYPE tadir,
       wa_new_tadir TYPE tadir.

 MOVE 'R3TR' TO wa_tadir-pgmid.
 MOVE 'TABL' TO wa_tadir-object.
 MOVE p_struc TO wa_tadir-obj_name.
 MOVE sy-sysid TO wa_tadir-srcsystem.
 MOVE sy-uname TO wa_tadir-author.
 MOVE sy-langu TO wa_tadir-masterlang.

* Ask for development package
 CALL FUNCTION 'TRINT_TADIR_POPUP'
  EXPORTING
   wi_message_enter_devclass          = abap_true
   wi_tadir                           = wa_tadir
   wi_no_tadir                        = abap_true
   wi_no_tdevc                        = abap_true
   wi_no_delete_function              = abap_true
   iv_name_too_long_for_old           = abap_true
  IMPORTING
   we_tadir_new                       = wa_new_tadir
  EXCEPTIONS
   display_mode                       = 1
   exit                               = 2
   global_tadir_insert_error          = 3
   no_systemname                      = 4
   no_systemtype                      = 5
   no_tadir_type                      = 6
   reserved_name                      = 7
   tadir_not_exist                    = 8
   close                              = 9
   no_object_authority                = 10
   no_modification_of_head_syst       = 11
   obj_specification_not_unique       = 12
   pgmid_object_not_allowed           = 13
   object_reserved_for_devclass       = 14
   OTHERS                             = 15.

* Create tadir entry
 CALL FUNCTION 'TRINT_TADIR_INSERT'
  EXPORTING
   author                     = sy-uname
   masterlang                 = sy-langu
   devclass                   = wa_new_tadir-devclass
*   GENFLAG                    = ' '
   object                     = wa_new_tadir-object
   obj_name                   = wa_new_tadir-obj_name
   pgmid                      = wa_new_tadir-pgmid
*   SRCSYSTEM                  = SY-SYSID
  EXCEPTIONS
   object_exists_global       = 1
   object_exists_local        = 2
   OTHERS                     = 3.

* Create transport request
 CALL FUNCTION 'TRINT_ORDER_CHOICE'
  EXPORTING
*   WI_SIMULATION                = ' '
    wi_order_type                = 'K'
   wi_task_type                 = 'S'
   wi_category                  = 'SYST'
   wi_client                    = sy-mandt
   iv_tarsystem                 = 'CQR'
   wi_e070                      = wa_e070
   wi_remove_locks              = 'X'
  IMPORTING
   we_order                     = lv_order
   we_task                      = lv_task
  TABLES
   wt_e071                      = lt_e071
   wt_e071k                     = lt_e071k.

* Set transport header
 wa_e071-trkorr = lv_task.
 wa_e071-as4pos = sy-tabix.
 wa_e071-pgmid = 'R3TR'.
 wa_e071-object = 'TABL'.
 wa_e071-obj_name = p_struc.
 wa_e071-objfunc = ' '.
 APPEND wa_e071 TO lt_e071.
 CLEAR wa_e071.

* Save items for transport request
 CALL FUNCTION 'TRINT_APPEND_TO_COMM_ARRAYS'
  EXPORTING
   wi_trkorr = lv_task
  TABLES
   wt_e071   = lt_e071
   wt_e071k  = lt_e071k.

* Sort an supress transport request
 CALL FUNCTION 'TR_SORT_AND_COMPRESS_COMM'
   EXPORTING
    iv_trkorr = lv_task.
ENDFORM.                    "write_transport

Posted on 20. März 2015 in ABAP, Blog

Back to Top