Skip to Content
0
Sep 19, 2007 at 04:25 PM

How to select a single value from a table dynamically by its table key

348 Views

Hi experts,

I have an internal table which is combined by two fields:

data: begin of itab occurs 0,

tabname like dd03l-tabname,

tabkey(100) type c,

end of itab.

that is to say, when I loop the itab then I know the database table name and its tablekey value. My requirement is I want to get a single record by the itab-tabkey from the database table itab-tabname. But I don't know how to build the WHERE condition for my SQL in this case.

In my original idea, I will get the key fields from table DD03L by the table name itab-tabname, and get the data element for the this table's key field, then I use the data elements to get the field length for each key data element from table DD04L. At last I can know each key fields and it lenth, then I want to use off-set of

itab-tabkey by knowing key field's length to combine my WHERE condition. At this point I don't know how to do it.

If my idea is right, can any experts give me a solution for this dynamic SQL??

If my idea is wrong, can any experts give me a right solutions??

Now I give an example for the ITAB.

P.S,

Client: 222

company code: 1000

Purchase Org: 1001

Sales Org: 1001

Vendor:1001000000

Customer:3001000000

tabname tabkey

LFA1 2221001000000

LFB1 22210010000001000

LFM1 22210010000001001

KNA1 2223001000000

KNB1 22230010000001000

KNVV 22230010000001001