Skip to Content
avatar image
Former Member

Passing a variable to select statement

Hi there,

I have a selection screen on which you can choose which table do you want to display.

I store the name of the selected table in a variable tab_name.

I want to use something like that:

data: tab_name(40).

.

.

.

select * from tab_name

into gt_master.

endselect.

But it did not allowed because tab_name is not a database table.

Is it possible to pass the table name in a select statement using a variable?

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

5 Answers

  • Best Answer
    avatar image
    Former Member
    Aug 17, 2007 at 03:01 PM

    Hello

    Yes it is posible you can use a dynamic select...

    Try this:

    DATA tabname(10).

    DATA: BEGIN OF wa,

    id TYPE scustom-id,

    name TYPE scustom-name,

    END OF wa.

    tabname = 'SCUSTOM'.

    SELECT id name INTO CORRESPONDING FIELDS OF wa FROM (tabname).

    WRITE: / wa-id, wa-name.

    ENDSELECT.

    or

    CONSTANTS: flight_tab_name(30) VALUE 'SPFLI'.

    DATA: from_clause TYPE STRING.

    DATA: BEGIN OF wa,

    name(20) TYPE C,

    connid TYPE spfli-connid,

    END OF wa.

    CONCATENATE flight_tab_name ' AS t1'

    ' JOIN scarr AS t2 ON t1carrid = t2carrid'

    INTO from_clause.

    SELECT t1connid t2carrname AS name

    FROM (from_clause)

    INTO CORRESPONDING FIELDS OF wa.

    WRITE: / wa-name, wa-connid.

    ENDSELECT

    Hope this helps

    Gabriel

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Aug 17, 2007 at 03:05 PM

    this is not possilbe.

    alternative :

    put selects on different tables inside IF conditions.

    Based on the value of your selection screen field tab_name,

    fetch the data from the appropriate table

    Add comment
    10|10000 characters needed characters exceeded

  • Aug 17, 2007 at 03:06 PM
    CREATE DATA itab TYPE STANDARD TABLE OF (tab_name). 
    
    SELECT * 
    INTO TABLE itab
    FROM (tab_name) .
    

    Message was edited by:

    Pawan Kesari

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Aug 17, 2007 at 03:06 PM

    SELECT * FROM (p_table)

    into <internal table>.

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member

      Flawless Code 😊

      PARAMETERS: p_tab(40) TYPE c.
      
      DATA         : lt_data   TYPE REF TO data.
      FIELD-SYMBOLS: <table>  TYPE STANDARD TABLE.
      
      CREATE DATA lt_data TYPE STANDARD TABLE OF (p_tab).
      
      ASSIGN lt_data->* TO <table>.
      
      SELECT * INTO TABLE <table> FROM (p_tab) .

  • Aug 17, 2007 at 03:15 PM

    Hi,

    Use fm RFC_GET_TABLE_ENTRIES or RFC_READ_TABLE.

    aRs

    Add comment
    10|10000 characters needed characters exceeded