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

Fetch data from Table

former_member189596
Active Participant
0 Likes
1,817

Hi all,

I am using function module through Call fucntion.

There i am getting <b>Table Name</b> as import Parameter.

Now i need to fetch data from that table (Which name we got througfh function module).

Is there any point to know how can we fetch data from runtime table .

i need some sample code for this.

Thanks inn advance,

Regards,

Bhaskar

1 ACCEPTED SOLUTION
Read only

former_member189059
Active Contributor
0 Likes
1,265

Hello,

I'm not sure if I got your question correctly

From what I understood, you need to fetch data from a table whose name is determined at runtime

If that is your requirement, then use the following code

DATA : LV_FIELD_DESC TYPE STRING.
DATA : LV_DATA1 TYPE STRING.
DATA : LV_DETAIL(128).
DATA : COMMA TYPE C VALUE ','.
DATA : LV_TNAME LIKE DD02L-TABNAME.
DATA : LV_DBTAB1 LIKE DD02L-TABNAME.
DATA : DREF TYPE REF TO DATA.

FIELD-SYMBOLS: <ITAB> TYPE ANY TABLE, " used to store dynamic tables
               <WA>    TYPE ANY,      " used to store record data
                <WA1> TYPE ANY .      " used to store field data

LV_DBTAB1 = 'MARA'. " <-- put your table name here

* we do not know the sized of the table that must be generated beforehand
* hence we use field symbols to dynamically generate the internal table
  CREATE DATA DREF TYPE STANDARD TABLE OF (LV_DBTAB1)
                            WITH NON-UNIQUE DEFAULT KEY.
  ASSIGN DREF->* TO <ITAB> .

* selects all data
  SELECT * FROM (LV_DBTAB1) INTO TABLE <ITAB> .
  LOOP AT <ITAB> ASSIGNING <WA>.

    DO.
      ASSIGN COMPONENT SY-INDEX
             OF STRUCTURE <WA> TO <WA1>.
      IF SY-SUBRC <> 0.
        EXIT.
      ENDIF.
      IF SY-SUBRC = 1.
        WRITE :/ .
      ENDIF.
      MOVE <WA1> TO LV_DETAIL.
      CONCATENATE LV_DATA1 LV_DETAIL COMMA INTO LV_DATA1.
    ENDDO.

*   display
    NEW-LINE.
    WRITE LV_DATA1.

*   file output if required
*   TRANSFER LV_DATA1 TO LV_OUTFILE.

*   clears the variable for reuse
    CLEAR LV_DATA1.

  ENDLOOP.

5 REPLIES 5
Read only

former_member189059
Active Contributor
0 Likes
1,266

Hello,

I'm not sure if I got your question correctly

From what I understood, you need to fetch data from a table whose name is determined at runtime

If that is your requirement, then use the following code

DATA : LV_FIELD_DESC TYPE STRING.
DATA : LV_DATA1 TYPE STRING.
DATA : LV_DETAIL(128).
DATA : COMMA TYPE C VALUE ','.
DATA : LV_TNAME LIKE DD02L-TABNAME.
DATA : LV_DBTAB1 LIKE DD02L-TABNAME.
DATA : DREF TYPE REF TO DATA.

FIELD-SYMBOLS: <ITAB> TYPE ANY TABLE, " used to store dynamic tables
               <WA>    TYPE ANY,      " used to store record data
                <WA1> TYPE ANY .      " used to store field data

LV_DBTAB1 = 'MARA'. " <-- put your table name here

* we do not know the sized of the table that must be generated beforehand
* hence we use field symbols to dynamically generate the internal table
  CREATE DATA DREF TYPE STANDARD TABLE OF (LV_DBTAB1)
                            WITH NON-UNIQUE DEFAULT KEY.
  ASSIGN DREF->* TO <ITAB> .

* selects all data
  SELECT * FROM (LV_DBTAB1) INTO TABLE <ITAB> .
  LOOP AT <ITAB> ASSIGNING <WA>.

    DO.
      ASSIGN COMPONENT SY-INDEX
             OF STRUCTURE <WA> TO <WA1>.
      IF SY-SUBRC <> 0.
        EXIT.
      ENDIF.
      IF SY-SUBRC = 1.
        WRITE :/ .
      ENDIF.
      MOVE <WA1> TO LV_DETAIL.
      CONCATENATE LV_DATA1 LV_DETAIL COMMA INTO LV_DATA1.
    ENDDO.

*   display
    NEW-LINE.
    WRITE LV_DATA1.

*   file output if required
*   TRANSFER LV_DATA1 TO LV_OUTFILE.

*   clears the variable for reuse
    CLEAR LV_DATA1.

  ENDLOOP.

Read only

0 Likes
1,265

Hi Kris,

thanks for u r reply.

I almost got solution for my questions.

i am using Fm <b>/SAPAPO/TS_PA_COPY_TABLE_GET</b>.

This is SCM system.

using this function module, i am geting table name for planning book name( this is my export parameter for that FM).

i need to fetch data from that table (which name i got through Fm) and need to update field value in that table.

So, can u give me extended code for this.

Thanks Ina advance,

Regards,

Bhaksar

Read only

0 Likes
1,265

Hello Bhaskar,

I don't have an SCM system, so I cannot test out that FM

However, I modified the above code to update the table as well


*&---------------------------------------------------------------------*
*& Report  ZKRIS_DYNAMIC_TABLE_READ_MOD
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  ZKRIS_DYNAMIC_TABLE_READ_MOD LINE-SIZE 256.

DATA : LV_FIELD_DESC TYPE STRING.
DATA : LV_DATA1 TYPE STRING.
DATA : LV_DETAIL(128).
DATA : COMMA TYPE C VALUE ','.
DATA : LV_TNAME LIKE DD02L-TABNAME.
DATA : LV_DBTAB1 LIKE DD02L-TABNAME.
DATA : DREF TYPE REF TO DATA.
DATA : FLAG_MODIFIED.  " determines if database needs to be updated

FIELD-SYMBOLS: <ITAB> TYPE ANY TABLE, " used to store dynamic tables
               <WA>    TYPE ANY,      " used to store record data
                <WA1> TYPE ANY .      " used to store field data

* call Fm /SAPAPO/TS_PA_COPY_TABLE_GET here
LV_DBTAB1 = 'ZGSTSET'. " <-- put your table name here

DATA: IT_FIELDS TYPE X031L OCCURS 0.
DATA: WA_FIELDS LIKE LINE OF IT_FIELDS.

CALL FUNCTION 'DDIF_NAMETAB_GET'
  EXPORTING
    tabname           = LV_DBTAB1
*   ALL_TYPES         = ' '
*   LFIELDNAME        = ' '
*   GROUP_NAMES       = ' '
*   UCLEN             =
* IMPORTING
*   X030L_WA          =
*   DTELINFO_WA       =
*   TTYPINFO_WA       =
*   DDOBJTYPE         =
*   DFIES_WA          =
*   LINES_DESCR       =
 TABLES
   X031L_TAB         = IT_FIELDS
*   DFIES_TAB         =
 EXCEPTIONS
   NOT_FOUND         = 1
   OTHERS            = 2
          .
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.

* we do not know the sized of the table that must be generated beforehand
* hence we use field symbols to dynamically generate the internal table
  CREATE DATA DREF TYPE STANDARD TABLE OF (LV_DBTAB1)
                            WITH NON-UNIQUE DEFAULT KEY.
  ASSIGN DREF->* TO <ITAB> .

* selects all data
  SELECT * FROM (LV_DBTAB1) INTO TABLE <ITAB> .

  LOOP AT <ITAB> ASSIGNING <WA>.

    FLAG_MODIFIED = ''.
    LOOP AT IT_FIELDS INTO WA_FIELDS.
      ASSIGN COMPONENT WA_FIELDS-FIELDNAME OF STRUCTURE <WA>
        TO <WA1>.

      IF WA_FIELDS-FIELDNAME = 'FIRSTNAME'. " fieldname in the table you wish to modify
        IF <WA1> = 'Kris'. " old value
          <WA1> = 'NewName'. " new value
          MODIFY TABLE <ITAB> FROM <WA>.
          FLAG_MODIFIED = 'X'.
        ENDIF.
      ENDIF.

      WRITE <WA1>. " comment this line to remove the display
    ENDLOOP.
    IF FLAG_MODIFIED = 'X'. " updates database only if the record was changed
      UPDATE (LV_DBTAB1) FROM <WA>.
*     note that if the field you choose is a key field, sy-subrc will be set to 4
    ENDIF.

*   display
    NEW-LINE.

  ENDLOOP.

Read only

0 Likes
1,265

Hi Kris,

Thanks for quick reply.

Small doubt here.

Actuvally , in The fucntion mdoule <b>/SAPAPO/TS_PA_COPY_TABLE_GET</b> .

they are called the function module wht you mention in answer <b>'DDIF_NAMETAB_GET'</b>. shall i call the fucntion module again.

Thanks in advance,

regards,

Bhaskar

Read only

0 Likes
1,265

Hello Bhaskar,

The Function Module 'DDIF_NAMETAB_GET' returns the fields of the database table name you pass to it

It may have been used in '/SAPAPO/TS_PA_COPY_TABLE_GET'

If it returns a table parameter with the field names, then you will not need to use it here, but instead loop at that returned table

Else, you will have to use it separately in this program

You can check what it returns by testing the FM in SE37