cancel
Showing results for 
Search instead for 
Did you mean: 

dynamic table read

Former Member
0 Kudos

Hi friends,

Here is my issue - I need to read the data dictionary table contents dynamically. i.e. I shall be giving the name of the table as a parameter on the selection screen and hence and i need to display the contents of the table given. Any inputs on this issue.

Thanks in advance.

regards

Bill

Accepted Solutions (0)

Answers (4)

Answers (4)

Former Member
0 Kudos

Hi Bill,

Here is my sample code.

          • TABLE DESCRIPTION

tables: dd02l, dd03l, dd04t, dd02t.

                • DATA DECLARATION

data: flag type i value '0'.

data: flag1 type i value '0'.

data: begin of mytable,

tabname like dd02l-tabname,

as4local like dd02l-as4local,

as4vers like dd02l-as4vers,

tabclass like dd02l-tabclass,

end of mytable.

data: begin of mytable1,

ddtext like dd02t-ddtext,

end of mytable1.

data: begin of tableinc,

tabname like dd02l-tabname,

as4local like dd02l-as4local,

as4vers like dd02l-as4vers,

tabclass like dd02l-tabclass,

end of tableinc.

data: temp like dd02l-tabname.

              • PARAMETERS DECLARATION

selection-screen begin of screen 100.

parameters thetable like dd02l-tabname obligatory.

selection-screen end of screen 100.

call selection-screen 100.

at selection-screen.

**SELECTING THE TABLE NAME,STATUS,VERSION AND CLASS FOR **THE GIVEN TABLE

select single tabname as4local as4vers tabclass into mytable from

dd02l

where tabname = thetable.

if sy-subrc ne 0.

message i000.

call selection-screen 100.

endif.

start-of-selection.

write:/ 'TABLE NAME:',mytable-tabname.

write: 'STATUS:',mytable-as4local.

write: 55'VERSION:',mytable-as4vers.

write: 70'CLASS:',mytable-tabclass.

      • SELECTING THE TABLE DESCRIPTION

select single ddtext into mytable1 from

dd02t

where tabname = thetable and

ddlanguage = 'E'.

.

write:/ 'TABLE DESCRIPTION:',mytable1-ddtext.

uline.

write:/ 'FNAME', 13 'KEY',18 'DOMAINNAME',30'CHECKTABLE',

42'INITTYPE',53'LENGTH',63 'REFTABLE',75 'DATATYPE',

84 'DESCRIPTION'.

uline.

clear mytable1.

***SELECTING THE DETAILS OF FILEDS

select * from dd03l

where tabname = mytable-tabname and

as4local = mytable-as4local and as4vers = mytable-as4vers.

        • SELECTING THE FIELD DESCRIPTION

if dd03l-fieldname = '.INCLUDE' or

dd03l-fieldname = '.APPEND' and

flag eq '0'.

move dd03l-precfield to temp.

select single ddtext into mytable1 from dd02t

where tabname = temp and

ddlanguage = 'E'.

format intensified on.

write: / dd03l-fieldname, 85 mytable1-ddtext.

flag = 1.

else.

select single * from dd04t

where rollname = dd03l-rollname and

as4local = mytable-as4local and as4vers = mytable-as4vers and

ddlanguage = 'E'.

format intensified off.

write: / dd03l-fieldname, 13 dd03l-keyflag, 18 dd03l-rollname,

30 dd03l-checktable, 44 dd03l-inttype, 53 dd03l-intlen,

63 dd03l-reftable, 75 dd03l-datatype, 85 dd04t-ddtext.

endif.

endselect.

regards,

Ruthra

Message was edited by: Ruthra Rajendran

andreas_mann3
Active Contributor
0 Kudos

Hi Bill ,

or use this short Program from http://www.abaps.de/

REPORT ztabaccess .

* Dieses Programm liest Daten aus einer beliebigen Datenbanktabelle
* in den Hauptspeicher und ermöglicht einen Zugriff aus einer internen
* Pointertabelle auf die Datenzeilen

DATA: a_table_line TYPE REF TO data.
DATA: table_lines  TYPE STANDARD TABLE OF REF TO data.
DATA: c TYPE cursor.
FIELD-SYMBOLS: <line>  TYPE ANY.
FIELD-SYMBOLS: <field> TYPE ANY.

PARAMETERS: p_tab TYPE dd02l-tabname.

START-OF-SELECTION.
* Es muss hier mit Cursor gearbeitet werden, da jeder Zugriff auf eine
* einzelne Tabellenzeile die Daten in einen separaten, dynamisch
* deklarierten Datenbereich einstellen muss. Dies kann nur der FETCH.
  OPEN CURSOR c FOR SELECT * FROM (p_tab)
       ORDER BY PRIMARY KEY.
  DO.
    CREATE DATA a_table_line TYPE (p_tab).
    ASSIGN a_table_line->* TO <line>.
    FETCH NEXT CURSOR c INTO <line>.
    IF sy-subrc NE 0.
      CLOSE CURSOR c.
      EXIT.
    ENDIF.
    APPEND a_table_line TO table_lines.
  ENDDO.
* table_lines ist eine interne Tabelle aus Pointern, die aus dynamisch
* deklarierten Zeilen besteht
  LOOP AT table_lines INTO a_table_line.
    ASSIGN a_table_line->* TO <line>.
    NEW-LINE.
    DO 6 TIMES.                        "Die ersten 6 Spalten
      CHECK sy-index > 1.
      ASSIGN COMPONENT sy-index OF STRUCTURE <line> TO <field>.
      IF sy-subrc NE 0.
        EXIT.
      ENDIF.
      WRITE: <field>.
    ENDDO.
  ENDLOOP.

regards Andreas

athavanraja
Active Contributor
0 Kudos

from the name of the table first get the table details using FM

TR_NAMETAB_GET

and using the info from this function create a dynamic internal table to hold the data.

CALL METHOD cl_alv_table_create=>create_dynamic_table

EXPORTING

it_fieldcatalog = it_fieldcat " (field info )

IMPORTING

ep_table = new_table.

now do the select from the data base table into this itab .

And the displaying, you can use list or ALV .

Hope this is clear.

Regards

Raja

Former Member
0 Kudos

Hi,

Got this infor from

https://www.sdn.sap.com/irj/servlet/prt/portal/prtroot/com.sap.km.cm.docs/library/webas/abap/abap faqs.faq#q-2

Retrieving field names dynamically

Sometimes you may have only a table name and want to retrieve the name of each field of the corresponding table. For example, when you want to use ASSIGN COMPONENT fieldname OF TABLE table.

An ABAPer's first reaction is to read the standard ABAP basis tables DD02L, DD03L, etc

This way of reading fields is very slow. Use methods from the class CL_ABAP_TYPEDESCR instead.

Example:

data: descr_struc_ref TYPE REF TO cl_abap_structdescr.

descr_struc_ref ?= cl_abap_typedescr=>describe_by_name('SFLIGHT' ).

Here is the result of descr_struct_ref after the execution of this piece of code

ABSOLUTE_NAME C 200 TYPE=SFLIGHT

TYPE_KIND C 1 u

LENGTH I 4 80

DECIMALS I 4 0

KIND C 1 S

STRUCT_KIND C 1 F

COMPONENTS h 8 Table[14x40]

HAS_INCLUDE C 1

The table COMPONENTS is filled with :

LENGTH DECIMALS TYPE_KIND NAME

3 | 0 |C |MANDT

3 | 0 |C |CARRID

4 | 0 |N |CONNID

8 | 0 |D |FLDATE

etc.

You have the fields name and a lot more information about the table. This class can also handle structure, table type, etc.

Note that this method is very fast because it uses the database layer directly thanks to SYSTEM CALLs.

To have a look at the class, use transaction SE24.

Thanks & Regards,

Judith.