Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

creating dynamic db tables

Former Member
0 Likes
1,304

Hello everybody,

could any tell me how to create a dynamic db table from se38, i need the code.

Thanks in advance

srikanth.

13 REPLIES 13
Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
1,119

You want to create a Database Table during runtime? Can you give the reason for this. Are you sure that you don't mean a dynamic internal table?

Regards,

Rich Heilman

Read only

0 Likes
1,119

Dynamic Internal Table Example.....



report zrich_0003
       no standard page heading.

type-pools: slis.

field-symbols: <dyn_table> type standard table,
               <dyn_wa>.

data: alv_fldcat type slis_t_fieldcat_alv,
      it_fldcat type lvc_t_fcat.


selection-screen begin of block b1 with frame title text-001.
parameters: p_check type c.
selection-screen end of block b1.

start-of-selection.

  perform build_dyn_itab.
  perform build_report.

  loop at <dyn_table> into <dyn_wa>.
    write:/ <dyn_wa>.
  endloop.


************************************************************************
*  Build_dyn_itab
************************************************************************
form build_dyn_itab.

  data: index(3) type c.

  data: new_table type ref to data,
        new_line  type ref to data,
        wa_it_fldcat type lvc_s_fcat.

* Create fields
  clear index.
  do 10 times.
    index = sy-index.
    clear wa_it_fldcat.
    concatenate 'Field' index into
             wa_it_fldcat-fieldname .
    condense  wa_it_fldcat-fieldname no-gaps.
    wa_it_fldcat-datatype = 'CHAR'.
    wa_it_fldcat-intlen = 5.
    append wa_it_fldcat to it_fldcat .
  enddo.

* Create dynamic internal table and assign to FS
  call method cl_alv_table_create=>create_dynamic_table
               exporting
                  it_fieldcatalog = it_fldcat
               importing
                  ep_table        = new_table.

  assign new_table->* to <dyn_table>.

* Create dynamic work area and assign to FS
  create data new_line like line of <dyn_table>.
  assign new_line->* to <dyn_wa>.

endform.

*********************************************************************
*      Form  build_report
*********************************************************************
form build_report.

  data: fieldname(20) type c.
  data: fieldvalue(5) type c.
  data: index(3) type c.
  field-symbols: <fs1>.

  do 10 times.

    index = sy-index.

* Set up fieldname
    concatenate 'FIELD' index into
             fieldname .
    condense   fieldname  no-gaps.

* Set up fieldvalue
    concatenate 'FLD' index into
             fieldvalue.
    condense   fieldvalue no-gaps.

    assign component  fieldname  of structure <dyn_wa> to <fs1>.
    <fs1> =  fieldvalue.

  enddo.

* Append to the dynamic internal table
  append <dyn_wa> to <dyn_table>.

endform.


Regards,

Rich Heilman

Read only

0 Likes
1,119

Hi Rich,

Yesterday i had an interview in abap and the interviewer asked me how to create a database table in se38 during runtime.so that is the question?.

cheers

srikanth.

Read only

0 Likes
1,119

Wow, I would say that the interviewer doesn't know what he is talking about. I don't see any reason why you would have to create a database table at runtime. Now there are many uses for creating a dynamic internal table at runtime, which I have shown above.

Does anyone else know of a specific reason why someone would want to create a database table at runtime?

Regards,

Rich Heilman

Read only

0 Likes
1,119

The only way you can do that is by using open sql statements of the underlying database. Alternatively, you may be able to use some function modules like DB_CREATE_TABLE.

Srinivas

Read only

0 Likes
1,119

Hi,

I suppose that this FM can create a database table. I have never used ii.

FM 'DD_CREATE_TABLE'

Svetlin

Read only

0 Likes
1,119

There are some instances where SAP creates the DD tables on the fly. This is particularly true with condition tables, infostructures etc. as the fields of these tables are defined in configuration by a consultant.

Unless this company which interviewed him is involved in some product like that, I don't see why a normal ABAPer would require to create a DB table on the fly.

Srinivas

Read only

0 Likes
1,119

I believe that I've seen (but cannot find an example) of cases where tables are dropped and then re-created. I think this is done to avoid long run times and the possibilities of SQL errors if all data in a large user table has to be deleted before being re-populated.

This was done using native SQL, but I guess FMs could also be used.

Rob

Read only

0 Likes
1,119

thanks for the answer.

Riccardo

Read only

0 Likes
1,119

thanks for the answer.

Riccardo

Read only

Former Member
0 Likes
1,119

Thanks in advance, is there the possibility to read from

an external database like SQL/server or ORACLE ?

Read only

0 Likes
1,119

Yes, using Native SQL.

Regards,

Rich Heilman

Read only

0 Likes
1,119

You could create TEMPORARY TABLES using Native SQL statements. But, there is really no point to "clog up" DB server space or processing time when the ABAP language supports such a wonderful data object like... the internal table (which resides outside of the DB server).

I would guess that was the answer that the interviewer was looking to get.