cancel
Showing results for 
Search instead for 
Did you mean: 

Create dynamic database tables

07-08-2008 12:07 PM
746 views 5 comments
0 Likes
SAP Managed Tags
Subscribe

Hi,

I want to create DB tables through program is there any way of doing?

Regards,

Shankar

0 Likes

Accepted Solutions (0)

Answers (2)

Answers (2)

Former Member
0 Likes

Hi,

Please see the link.

http://www.sap-img.com/ab030.htm

Regards,

Anil

Former Member
0 Likes

Thanks guys,

Actually i want to create database z tables and not internal tables.

regards,

Shankar

Former Member
0 Likes

Hi Shankar,

If your problem has been resove then pls mark as answered and mentioned how to resolved so that in future any other need same type of problem he can get the reply immediately.

Thanks for your support.

Regards,

Anil

Former Member
0 Likes

Hi Anil,

my query was regarding DB z table generation which yet not resolved. As i already mentioned about same, creating internal dynamic table is not a issue problem is with designing database table.

Regards,

Shankar G

Former Member
0 Likes

Hi shankar,

welcome.

you can create a Dynamic Internal table .Just chek out this program .

type-pools : abap.

field-symbols: type standard table,

,

.

data: dy_table type ref to data,

dy_line type ref to data,

xfc type lvc_s_fcat,

ifc type lvc_t_fcat.

selection-screen begin of block b1 with frame.

parameters: p_table(30) type c default 'T001'.

selection-screen end of block b1.

start-of-selection.

perform get_structure.

perform create_dynamic_itab. *********Creates a dyanamic internal table*********

perform get_data.

perform write_out.

form get_structure.

data : idetails type abap_compdescr_tab,

xdetails type abap_compdescr.

data : ref_table_des type ref to cl_abap_structdescr.

  • Get the structure of the table.

ref_table_des ?=

cl_abap_typedescr=>describe_by_name( p_table ).

idetails[] = ref_table_des->components[].

loop at idetails into xdetails.

clear xfc.

xfc-fieldname = xdetails-name .

xfc-datatype = xdetails-type_kind.

xfc-inttype = xdetails-type_kind.

xfc-intlen = xdetails-length.

xfc-decimals = xdetails-decimals.

append xfc to ifc.

endloop.

endform.

form create_dynamic_itab.

  • Create dynamic internal table and assign to FS

call method cl_alv_table_create=>create_dynamic_table

exporting

it_fieldcatalog = ifc

importing

ep_table = dy_table.

assign dy_table->* to .

  • Create dynamic work area and assign to FS

create data dy_line like line of .

assign dy_line->* to .

endform.

form get_data.

  • Select Data from table.

select * into table

from (p_table).

endform.

Write out data from table.

loop at into .

do.

assign component sy-index

of structure to .

if sy-subrc <> 0.

exit.

endif.

if sy-index = 1.

write:/ .

else.

write: .

endif.

enddo.

endloop.

-


REPORT ZCLUST1 .

  • Example: how to create a dynamic internal table

  • The dynamic internal table stucture

DATA: BEGIN OF STRUCT OCCURS 10,

FILDNAME(8) TYPE C,

ABPTYPE TYPE C,

LENGTH TYPE I,

END OF STRUCT.

  • The dynamic program source table

DATA: BEGIN OF INCTABL OCCURS 10,

LINE(72),

END OF INCTABL.

DATA: LNG TYPE I, TYPESRTING(6).

  • Sample dynamic internal table stucture

STRUCT-FILDNAME = 'field1'. STRUCT-ABPTYPE = 'c'. STRUCT-LENGTH = '6'.

APPEND STRUCT. CLEAR STRUCT.

STRUCT-FILDNAME = 'field2'. STRUCT-ABPTYPE = 'd'.

APPEND STRUCT. CLEAR STRUCT.

STRUCT-FILDNAME = 'field3'. STRUCT-ABPTYPE = 'i'.

APPEND STRUCT. CLEAR STRUCT.

  • Create the dynamic internal table definition in the dyn. program

INCTABL-LINE = 'program zdynpro.'. APPEND INCTABL.

INCTABL-LINE = 'data: begin of dyntab occurs 10,'. APPEND INCTABL.

LOOP AT STRUCT.

INCTABL-LINE = STRUCT-FILDNAME.

LNG = STRLEN( STRUCT-FILDNAME ).

IF NOT STRUCT-LENGTH IS INITIAL .

TYPESRTING(1) = '('.

TYPESRTING+1 = STRUCT-LENGTH.

TYPESRTING+5 = ')'.

CONDENSE TYPESRTING NO-GAPS.

INCTABL-LINE+LNG = TYPESRTING.

ENDIF.

INCTABL-LINE+15 = 'type '.

INCTABL-LINE+21 = STRUCT-ABPTYPE.

INCTABL-LINE+22 = ','.

APPEND INCTABL.

ENDLOOP.

INCTABL-LINE = 'end of dyntab. '.

APPEND INCTABL.

  • Create the code processes the dynamic internal table

INCTABL-LINE = ' '. APPEND INCTABL.

INCTABL-LINE = 'dyntab-field1 = ''aaaaaa''.'. APPEND INCTABL.

INCTABL-LINE = 'dyntab-field1 = ''19970814''.'. APPEND INCTABL.

INCTABL-LINE = 'dyntab-field1 = 1.'. APPEND INCTABL.

INCTABL-LINE = 'append dyntab.'. APPEND INCTABL.

INCTABL-LINE = ' '. APPEND INCTABL.

INCTABL-LINE = 'loop at dyntab.'. APPEND INCTABL.

INCTABL-LINE = 'write: / dyntab.'. APPEND INCTABL.

INCTABL-LINE = 'endloop.'. APPEND INCTABL.

  • Create and run the dynamic program

INSERT REPORT 'zdynpro'(001) FROM INCTABL.

SUBMIT ZDYNPRO.

-


or Just try out this simpler dynamic internal tables

DATA: itab TYPE STANDARD TABLE OF spfli,

wa LIKE LINE OF itab.

DATA: line(72) TYPE c,

list LIKE TABLE OF line(72).

START-OF-SELECTION.

*line = ' CITYFROM CITYTO '.

line = ' AIRPTO '.

APPEND line TO list.

SELECT DISTINCT (list)

INTO CORRESPONDING FIELDS OF TABLE itab

FROM spfli.

IF sy-subrc EQ 0.

LOOP AT itab INTO wa.

  • WRITE: / wa-cityfrom, wa-cityto.

WRITE 😕 wa-airpto.

ENDLOOP.

ENDIF.

or

CREATE DATA new_line TYPE table of (p_tab).

This syntax was not allowed in earlier releases. The FM Y_CREATE_DYNAMIC_TABLE can help you to achieve this goal.

Code

FUNCTION y_create_dynamic_table.

*"----


""Local interface:

*" IMPORTING

*" REFERENCE(I_STRUCT_NAME) TYPE DD02L-TABNAME

*" REFERENCE(I_ALV_CLEAR_KEY) TYPE FLAG OPTIONAL

*" EXPORTING

*" REFERENCE(E_TOTAL_LENGTH) TYPE I

*" TABLES

*" CTBL_ALV_FIELDCAT TYPE SLIS_T_FIELDCAT_ALV OPTIONAL

*" CTBL_DYN_CATALOG STRUCTURE LVC_S_FCAT OPTIONAL

*" CHANGING

*" REFERENCE(DYN_WA) TYPE REF TO DATA

*" REFERENCE(DYN_TABLE) TYPE REF TO DATA

*"----


FIELD-SYMBOLS : <tbl_output> TYPE STANDARD TABLE,

<wa_output> TYPE ANY.

DATA: new_line TYPE REF TO data, " dereference <fs>

new_table TYPE REF TO data.

  • Dynamic field description of a structure

DATA: ref_descr TYPE REF TO cl_abap_structdescr,

wa_comp TYPE abap_compdescr.

  • cl_alv_table_create=>create_dynamic_table METHOD

DATA: it_fieldcat TYPE lvc_t_fcat,

is_fieldcat LIKE LINE OF it_fieldcat.

  • REUSE_ALV FM

DATA: ct_fieldcat TYPE slis_t_fieldcat_alv,

wa_fc LIKE LINE OF ct_fieldcat.

    • create working area according to selection

CREATE DATA new_line TYPE (i_struct_name).

ASSIGN new_line->* TO <wa_output>.

  • create table-> fill catalog

ref_descr ?= cl_abap_typedescr=>describe_by_data( <wa_output> ).

LOOP AT ref_descr->components INTO wa_comp.

MOVE:

wa_comp-name TO is_fieldcat-fieldname,

wa_comp-name TO is_fieldcat-ref_field,

i_struct_name TO is_fieldcat-ref_table.

APPEND is_fieldcat TO it_fieldcat.

ADD wa_comp-length TO e_total_length.

  • Catalog for REUSE_ALV_GRID_DISPLAY

MOVE-CORRESPONDING is_fieldcat TO wa_fc.

wa_fc-fieldname = is_fieldcat-fieldname.

wa_fc-fieldname = is_fieldcat-ref_field.

wa_fc-ref_tabname = is_fieldcat-ref_table.

  • If the key is cleared, the order of columns are not changed

IF NOT i_alv_clear_key IS INITIAL.

CLEAR wa_fc-key.

ENDIF.

APPEND wa_fc TO ct_fieldcat.

ENDLOOP.

CALL METHOD cl_alv_table_create=>create_dynamic_table

EXPORTING

it_fieldcatalog = it_fieldcat

IMPORTING

ep_table = new_table.

ASSIGN new_table->* TO <tbl_output>.

  • Return

ctbl_alv_fieldcat[] = ct_fieldcat[]. " ALV use

ctbl_dyn_catalog[] = it_fieldcat[]. " create_dynamic_table reuse

  • dyn_table[] = <tbl_output>. " generates a dump, <fs> not assigned

  • dyn_wa = <wa_output>. " when calling the FM

dyn_table = new_table.

dyn_wa = new_line.

ENDFUNCTION.

In the calling program:

*----


  • F I E L D u2013 S Y M B O L S

*----


FIELD-SYMBOLS : <tbl> TYPE STANDARD TABLE,

<wa> TYPE ANY.

*----


  • D A T A

*----


DATA: new_line TYPE REF TO data, " dereference <fs>

new_table TYPE REF TO data.

CALL FUNCTION 'Y_CREATE_DYNAMIC_TABLE'

EXPORTING

i_struct_name = p_tab

i_alv_clear_key = 'X'

TABLES

ctbl_alv_fieldcat = ct_fieldcat

CHANGING

dyn_wa = new_line

dyn_table = new_table.

  • Dereference to field symbol

ASSIGN new_table->* TO <tb>.

ASSIGN new_line->* TO <wa>.

thanks

shankar

reward me ifusefull