Application Development 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: 

RTTS dynamic DB table reference, how?

Former Member
0 Kudos

Hello all,

Here is a very simple program that will capture and write a list of the fields and their definitions of a database table that I have explicitly assigned to a variable using the type statement. I have been playing with this code and attempting to dynamically associate a database table structure with a variable, and then using that variable to assign it's structure to a <FS>. Everything that I tried generated a syntax error.

Could somebody please tell me what syntax I need to dynamically associate a database structure with a variable, versus statically?

This is the code.

data: dr_dref type ref to data,

rf_descr_ref type ref to cl_abap_typedescr .

data: <b>wa_vbap type vbak</b>. " DB table reference

field-symbols: <fs> type any.

start-of-selection.

do.

  • Assign every field of this structure to the untyped field symbol.

assign component sy-index of structure <b>WA_vbap</b> to <fs>.

if sy-subrc ne 0.

exit.

endif.

call method cl_abap_typedescr=>describe_by_data

EXPORTING

p_data = <fs>

RECEIVING

p_descr_ref = rf_descr_ref.

write: / sy-index, rf_descr_ref->type_kind,

rf_descr_ref->length,

rf_descr_ref->absolute_name+6.

enddo.

Thanks

Bruce

1 ACCEPTED SOLUTION

athavanraja
Active Contributor
0 Kudos

here is the code sample. I guess this is what you wanted. right?

REPORT  YRTTS.
type-pools : abap.
data: wa_vbak type vbak .
data : it_details type abap_compdescr_tab,
wa_comp type abap_compdescr.

data : ref_descr type ref to cl_abap_structdescr.

ref_descr ?= cl_abap_typedescr=>describe_by_data( wa_vbak ).
it_details[] = ref_descr->components[].

break-point .

Regards

Raja

5 REPLIES 5

athavanraja
Active Contributor
0 Kudos

here is the code sample. I guess this is what you wanted. right?

REPORT  YRTTS.
type-pools : abap.
data: wa_vbak type vbak .
data : it_details type abap_compdescr_tab,
wa_comp type abap_compdescr.

data : ref_descr type ref to cl_abap_structdescr.

ref_descr ?= cl_abap_typedescr=>describe_by_data( wa_vbak ).
it_details[] = ref_descr->components[].

break-point .

Regards

Raja

0 Kudos

Durairaj,

Your code is still explicitly associating a database tablename with a variable. Your code populates an internal table with the database table fields and attributes, just the way I wanted. But, I want to be able to associate the database table name at runtime rather than hard coding it in the program.

What do I need to change in your code to dynamically associate a database table name? Everything I tried previously failed the syntax check.

data: wa_vbak type vbak .

Thanks

Bruce

0 Kudos

Run this program, enter the structure name that you want the components of and hit execute.



report zrich_0003.


type-pools : abap.


data : it_details type abap_compdescr_tab,
       wa_comp type abap_compdescr.

parameters: p_type(10) type c.

data : ref_descr type ref to cl_abap_structdescr.

ref_descr ?= cl_abap_typedescr=>describe_by_name( p_type ).
it_details[] = ref_descr->components[].

break-point .

Regards,

Rich Heilman

0 Kudos

This message was moderated.

0 Kudos

Rich,

Thanks for your answer which solved my problem. I awarded you points. Have a good week.

Bruce