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: 

Accessing Dynamic Database Table Names (Stored in a variable)

Former Member
0 Kudos

Dear Experts,

I am currenty having a basic problem on which i hope you can help me with.

Suppose i have a code running and in runtime a name of a database table will be saved into the variable lv_tablename. Something like this:

lv_tablename = MARA.

I would like to work with this Variable in the following way. For example, suppose i would like to build a custom type, which contains all fields of mara with one additional custom field at the end.

'Something like this':

TYPES:
  BEGIN OF ty_custom.
    Include type mara as ty_custom.
Types:
    lastfield type c,
  END OF ty_custom.

Instead of the table name 'MARA' i want to use the content of the variable lv_tablename. Something like this:

TYPES:
  BEGIN OF ty_custom.
    Include type (lv_tablename) as ty_custom.
Types:
    lastfield type c,
  END OF ty_custom.

Or Use the Variable for an inner Join :

Select MARA~MATNR MARC~MATNR MARC~WERKS
    INTO CORRESPONDING FIELDS OF TABLE lt_table
    FROM ((lv_tablename) AS MARA
    INNER JOIN MARC AS MARC
       on MARA~MATNR = MARC~MATNR).

But as probably many of you already know, it doesn't work. Is there any easy way to do this? Maybe by putting the complete syntax in a variable? Any help would be appreciated!

3 REPLIES 3

matt
Active Contributor
0 Kudos

Welcome to the site!

Please in future, if you're posting code, using the "code" button in the editor and make sure your code is formatted nicely. Use right-click "paste as text" if pasting code copied from an editor. This time, I've done it for you.

Matt Billingham

SAP Community Moderator

matt
Active Contributor
0 Kudos

You can't use

Include type (lv_tablename) as ty_custom.

If you want dynamic data structures, you must use the RTTS classes. (If you don't know what they are, then please search this site - there's many examples).

For the inner join - please search before posting. Often you'll find the answer without posting a question. Try: https://archive.sap.com/discussions/thread/1898930