Skip to Content
author's profile photo Former Member
Former Member

Dyamically Fetching the Content of the REQUIRED TABLE ??? How ???

Hi,

My requirements is,when I give the table name on the Standard Selection screen.Then, I should fetch complete records of that table.That means,

Whatever existing table name we give, we have to fetch table records and

displaying on th list.( i.e., Creating internal table for ANY structure ) .

Regards,

V.Raghavender.

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

7 Answers

  • Best Answer
    author's profile photo Former Member
    Former Member
    Posted on Feb 28, 2007 at 01:29 PM

    Hi,

    here an example:

    REPORT ZGRO_ALV_TABLE MESSAGE-ID ZZ.

    *

    *> define selection screen

    ************************************************************************

    SELECTION-SCREEN: BEGIN OF BLOCK A01 WITH FRAME TITLE MELD_A01.

    *

    • Eingabe festlegen

    SELECTION-SCREEN: SKIP.

    SELECTION-SCREEN: BEGIN OF LINE.

    SELECTION-SCREEN: COMMENT 01(25) T_DBTAB.

    PARAMETER: P_DBTAB LIKE DATABROWSE-TABLENAME DEFAULT 'LFA1'.

    SELECTION-SCREEN: END OF LINE.

    *

    SELECTION-SCREEN: SKIP.

    SELECTION-SCREEN: BEGIN OF LINE.

    SELECTION-SCREEN: COMMENT 01(25) T_MAXSEL.

    PARAMETER: P_MAXSEL LIKE RSEUMOD-TBMAXSEL DEFAULT 100.

    SELECTION-SCREEN: END OF LINE.

    *

    SELECTION-SCREEN: SKIP.

    *

    SELECTION-SCREEN: END OF BLOCK A01.

    *

    ************************************************************************

    INITIALIZATION.

    *

    MELD_A01 = 'Datenbankselektion'.

    *

    T_DBTAB = 'Datenbanktabelle'.

    T_MAXSEL = 'Anzahl Zeilen'.

    *

    ************************************************************************

    • PROGRAMMSTART *

    ************************************************************************

    START-OF-SELECTION.

    *

    PERFORM ITAB.

    *

    END-OF-SELECTION.

    ************************************************************************

    FORM ITAB.

    *

    DATA: GT_FCAT TYPE LVC_T_FCAT,

    GDO_DATA TYPE REF TO DATA.

    *

    FIELD-SYMBOLS: <GT_ITAB> TYPE TABLE.

    *

    DATA: LINES LIKE SY-TABIX.

    *

    CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'

    EXPORTING

    I_STRUCTURE_NAME = P_DBTAB

    CHANGING

    CT_FIELDCAT = GT_FCAT

    EXCEPTIONS

    INCONSISTENT_INTERFACE = 1

    PROGRAM_ERROR = 2

    OTHERS = 3.

    *

    IF SY-SUBRC <> 0.

    MESSAGE I010 WITH 'Error: LVC_FIELDCATALOG_MERGE!'.

    EXIT.

    ENDIF.

    *

    DESCRIBE TABLE GT_FCAT LINES LINES.

    *

    IF LINES = 0.

    MESSAGE I010 WITH 'Error: Kein Feldkatalog aufgebaut!'.

    EXIT.

    ENDIF.

    *

    CALL METHOD CL_ALV_TABLE_CREATE=>CREATE_DYNAMIC_TABLE

    EXPORTING

    IT_FIELDCATALOG = GT_FCAT

    IMPORTING

    EP_TABLE = GDO_DATA

    EXCEPTIONS

    GENERATE_SUBPOOL_DIR_FULL = 1

    OTHERS = 2.

    *

    IF SY-SUBRC <> 0.

    MESSAGE I010 WITH 'Error: CREATE_DYNAMIC_TABLE!'.

    EXIT.

    ENDIF.

    *

    ASSIGN GDO_DATA->* TO <GT_ITAB>.

    *

    SELECT * FROM (P_DBTAB) UP TO P_MAXSEL ROWS INTO TABLE <GT_ITAB>.

    *

    CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'

    EXPORTING

    I_STRUCTURE_NAME = P_DBTAB

    TABLES

    T_OUTTAB = <GT_ITAB>.

    *

    ENDFORM.

    Regards, Dieter

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Feb 28, 2007 at 01:19 PM

    There is a mechanism of creating dynamic internal tables in SAP ABAP

    you can check that.

    Please let me know whether this answer is sufficient .

    Regards

    Sai.

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Feb 28, 2007 at 01:20 PM

    Hi ,

    Please go thru this sample program. I am just getting the table based on a key field(YREL_REQNO) from one of the YREL table . You can change this program as per your requirement.

    function y_copy_data_req.

    *"----


    ""Local interface:

    *" IMPORTING

    *" VALUE(REQ_NO) TYPE YREL_REQNO

    *" VALUE(DEST) TYPE ADRTP

    *" EXPORTING

    *" VALUE(RET_CODE) TYPE CHAR1

    *" EXCEPTIONS

    *" INVALID_REQ_NO

    *" INVALID_TABLE_NAME

    *" FIELD_NOT_FOUND_IN_TABLE

    *"----


    ************************************************************************

    • Function Module Name : Y_COPY_DATA_REQ *

    • Start Date : 08-Jun-2006 *

    • Developer : *

    • CR# : 51959 *

    • Description : To copy the table data by reqno *

    ************************************************************************

    • Modification Log *

    ----


    • ChangeReq Date DevID * Description *

    ----


    *-- Tables

    tables : dd03l,

    *-Adding table in place of Tab_name parameter.

    yrel_tables. " YREL Table

    *--Field symbols

    field-symbols: <f_fs> type table.

    *--Global variables

    data: g_repid like sy-repid,

    g_tabname like dcobjdef-name,

    g_ucomm like sy-ucomm,

    g_destin(10) type c,

    g_alvtab type slis_tabname,

    g_batch(10) type c,

    g_ret_code type char1.

    *-- Internal tables

    data: d_ref type ref to data,

    d_ref2 type ref to data,

    d_ref1 type ref to data,

    new_table type ref to data,

    new_line type ref to data,

    tb_alv_cat type table of lvc_s_fcat,

    tb_alv_cat1 type table of lvc_s_fcat,

    ls_alv_cat like line of tb_alv_cat.

    data: tb_data like zuptab occurs 0 with header line.

    data: begin of tb_dd03l occurs 0,

    tabname type tabname,

    fieldname type fieldname,

    end of tb_dd03l.

    data: begin of tb_header occurs 0.

    include structure dntab.

    data: end of tb_header.

    data: begin of tb_request occurs 0,

    whereclause(72),

    end of tb_request.

    *-Table to hold YREL_TABLE data.

    data: begin of i_yrel_tables occurs 0,

    tabname like yrel_tables-tabname,

    end of i_yrel_tables.

    *- Fetching Table contents .

    select tabname into table i_yrel_tables

    from yrel_tables.

    • Check for the table name

    loop at i_yrel_tables.

    select tabname fieldname into table tb_dd03l

    from dd03l

    where tabname = i_yrel_tables-tabname.

    if sy-subrc <> 0.

    raise invalid_table_name.

    endif.

    • Check for the reqno field

    read table tb_dd03l with key fieldname = 'REQNO'.

    if sy-subrc <> 0.

    raise field_not_found_in_table.

    endif.

    • Get the table name

    g_tabname = i_yrel_tables-tabname.

    • Get the fields of the table

    refresh tb_header.

    call function 'NAMETAB_GET'

    exporting

    langu = sy-langu

    tabname = g_tabname

    tables

    nametab = tb_header

    exceptions

    no_texts_found = 1.

    loop at tb_header .

    ls_alv_cat-fieldname = tb_header-fieldname.

    ls_alv_cat-ref_table = i_yrel_tables-tabname.

    ls_alv_cat-ref_field = tb_header-fieldname.

    append ls_alv_cat to tb_alv_cat.

    endloop.

    • Build the internal table

    call method cl_alv_table_create=>create_dynamic_table

    exporting it_fieldcatalog = tb_alv_cat

    importing ep_table = d_ref .

    assign d_ref->* to <f_fs>.

    concatenate 'REQNO' 'EQ' req_no into tb_request-whereclause

    separated by space.

    append tb_request.

    select * from (i_yrel_tables-tabname)

    into corresponding fields of table <f_fs>

    where (tb_request).

    • Get the destination

    case dest.

    when 'D48'.

    g_destin = c_dest_d48.

    when 'M08'.

    g_destin = c_dest_m08.

    when 'P08'.

    g_destin = c_dest_p08.

    when 'T48'.

    g_destin = c_dest_t48.

    endcase.

    • Assign the data to the internal table

    tb_data[] = <f_fs>.

    • Update the data in the given destination

    call function 'YREL_COPY_DATA_REQ_DEST' destination g_destin

    exporting

    tabname = i_yrel_tables-tabname

    importing

    ret_code = g_ret_code

    tables

    data = <f_fs>.

    • Pass the return code

    if g_ret_code = 0.

    ret_code = 'S'.

    else.

    ret_code = 'F'.

    endif.

    *-Depending upon RET_CODE we need to raise an event.

    endloop.

    endfunction.

    Regards,

    Jayaram...

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Feb 28, 2007 at 01:21 PM

    Hello,

    U can use the Tcode SE16 for this purpose.

    Vasanth

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Feb 28, 2007 at 01:25 PM

    Yes you can. please see the below steps

    1. Add parameter on selection-screen to accept table name

    2. Using SAP functions (FM exactly i don't know) you can fetch the structure details

    3. Create dynamic internal table (CL_GUI_ALV_GRID=> create_dynamic_table) for the above retrieved details in step2

    4. Write a dynamic select query, pass the data into the internal table.

    select *

    into table (t_name)

    from (table_name).

    If it helps plz reward points.

    Regards

    Bhupal Reddy

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Feb 28, 2007 at 01:26 PM

    YOu can use the FM: RFC_READ_TABLE

    parameters: p_table type DD02L-TABNAME.

    call function 'RFC_READ_TABLE'

    destination ldf_rfcdest

    exporting

    query_table = p_table

    tables

    OPTIONS = ldt_options "Fill the where condtion in this internal table

    fields = ldt_fields

    data = ldt_data

    exceptions

    table_not_available = 1

    table_without_data = 2

    option_not_valid = 3

    field_not_valid = 4

    not_authorized = 5

    data_buffer_exceeded = 6

    others = 7.

    Regards,

    Ravi

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Feb 28, 2007 at 01:36 PM

    Hi Raghavender,

    The only solution for this issue is to program dynamically.

    For this, you have to get the structure of the table from the data base table DD03L and then create a dynamic internal table by using the class cl_alv_table_create and method create_dynamic_table.

    Have a look at the following code to generate internal table dynamically...

    <b>

    FORM prepare_itab .
      DATA: lv_fpos(4) TYPE n value '0001'.
      data: str1(10).
      refresh fcat[].
      if charno ge 2.
        wa-col_pos = lv_fpos.
        wa-fieldname = 'J_3AKORD'.
        wa-tabname = 'J_3APGEN'.
        wa-outputlen = '8'.
        APPEND WA TO FCAT.
      endif.
      loop at tx_cawn.
        clear wa.
        concatenate 'XDIM' lv_fpos into str1.
        add 1 to LV_FPOS.
        wa-col_pos = lv_fpos.
        wa-fieldname = str1.
    *    wa-tabname = 'J_3APGEN'.
        wa-outputlen = '8'.
        wa-domname = 'FUNC'.
        APPEND WA TO FCAT.
      endloop.
    *  clear wa.
    *  add 1 to LV_FPOS.
    *  wa-col_pos = lv_fpos.
    *  wa-fieldname = 'STYLE'.
    *  wa-inttype = 'h'.
    *  wa-rollname = 'LVC_T_STYL'.
    *  APPEND WA TO FCAT.
      free: xtab, ytab.
      CALL METHOD cl_alv_table_create=>create_dynamic_table
        EXPORTING
          i_style_table             = 'X'
          it_fieldcatalog           = fcat
        IMPORTING
          ep_table                  = xtab
        EXCEPTIONS
          generate_subpool_dir_full = 1
          OTHERS                    = 2.
    
      CALL METHOD cl_alv_table_create=>create_dynamic_table
        EXPORTING
    *      i_style_table             = 'X'
          it_fieldcatalog           = fcat
        IMPORTING
          ep_table                  = ytab
        EXCEPTIONS
          generate_subpool_dir_full = 1
          OTHERS                    = 2.
    ENDFORM.                    " prepare_itab

    </b>.

    Please go through the above code, analyze it to understand how to create the dynamic internal table...

    Then write a select statement that fetches data from the required table...

    example::: <b>select * from (p_tabname) into table <dynamically generated internal table>.</b>

    Regards,

    Phani

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.