Skip to Content
author's profile photo
Former Member

how to write table name in SQL according to the user input

hi

i want to select data from a table , and the table name, field name is input by the user, for example, user input table name T1, field name F1 and i want to construct a SQL like select F1 into a_var from T1 . how does the SQL statement written? thanks for your advice.

Add comment
10|10000 characters needed characters exceeded

  • Follow
  • Get RSS Feed

4 Answers

  • Best Answer
    author's profile photo
    Former Member
    Posted on Aug 21, 2007 at 10:09 AM

    select (F1)

    from (T1)

    into table it.

    reward if useful

    S@meer

    Add comment
    10|10000 characters needed characters exceeded

  • author's profile photo
    Former Member
    Posted on Aug 21, 2007 at 10:08 AM

    hi

    for ur requirement u should go for field sysmbols...

    dynamic internal tables..

    u can chk this....

    TYPE-POOLS : slis.

    PARAMETERS : p_struc LIKE dd02l-tabname.

    DATA : it_fieldcat TYPE lvc_t_fcat,

    t_output TYPE REF TO data .

    FIELD-SYMBOLS: <t_output> TYPE table ,

    <wa_output> TYPE ANY,

    <fval> TYPE ANY.

    CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'

    EXPORTING

    • I_BUFFER_ACTIVE = I_BUFFER_ACTIVE

    i_structure_name = p_struc

    • I_CLIENT_NEVER_DISPLAY = 'X'

    • I_BYPASSING_BUFFER = I_BYPASSING_BUFFER

    • I_INTERNAL_TABNAME = I_INTERNAL_TABNAME

    CHANGING

    ct_fieldcat = it_fieldcat

    • EXCEPTIONS

    • INCONSISTENT_INTERFACE = 1

    • PROGRAM_ERROR = 2

    • OTHERS = 3

    .

    IF sy-subrc <> 0.

    • MESSAGE i000(bctrain) WITH 'MERGE FAILED'.

    *ELSE.

    • MESSAGE i000(bctrain) WITH 'MERGE SUCCESS'.

    ENDIF.

    • CREATE DATA t_newline LIKE LINE OF <t_dyntable>.

    • ASSIGN t_newline->* TO <fs_dyntable>.

    CALL METHOD cl_alv_table_create=>create_dynamic_table

    EXPORTING

    • I_STYLE_TABLE =

    it_fieldcatalog = it_fieldcat

    • I_LENGTH_IN_BYTE =

    IMPORTING

    ep_table = t_output

    • E_STYLE_FNAME =

    EXCEPTIONS

    generate_subpool_dir_full = 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.

    ASSIGN t_output->* TO <t_output>.

    SELECT * up to 10 rows FROM

    (p_struc) INTO

    CORRESPONDING FIELDS OF

    TABLE <t_output>.

    *write : / 'dynamic table created'.

    LOOP AT <t_output> ASSIGNING <wa_output>.

    DO.

    ASSIGN component sy-index of structure <wa_output> to <fval>.

    IF sy-subrc <> 0.

    WRITE : /.

    EXIT.

    ELSE.

    WRITE : <fval>.

    ENDIF.

    ENDDO.

    ENDLOOP.

    Message was edited by:

    Premalatha G

    Add comment
    10|10000 characters needed characters exceeded

  • author's profile photo
    Former Member
    Posted on Aug 21, 2007 at 10:11 AM

    Hi!

    Somehow like this:

    PARAMETERS: p_tabnam(10).

    SELECT * FROM (p_tabnam)...

    For the INTO clause, you have to check the dynamic assigning, with command ASSIGN and field-symbols.

    Regards

    Tamá

    Add comment
    10|10000 characters needed characters exceeded

  • author's profile photo
    Former Member
    Posted on Aug 21, 2007 at 10:13 AM

    Define a data type character with length 72 and give the input from user and then proceed

    for example.

    data : ex(72) type c.

    SUBMIT REPORT ( EX) AND RETURN.

    Add comment
    10|10000 characters needed characters exceeded