Quantcast
Channel: SCN : All Content - ABAP Development
Viewing all articles
Browse latest Browse all 8332

Best way to create dynamic sql

$
0
0

Hello all,

 

İ need to create dynamic queries, these queries will be based on the z table entries.

For example: User will be able to choose which fields to be selected and returned which i will record them in z tables.

where i called them conditions and returns.

 

so i need to build dynamic sql queries.

 

i thought of this way!!

 

 

 

  METHOD DYNAMIC_SQL_QUERY.

    DATA: TABLENAME  TYPE TABNAME,

          FNAME      TYPE FIELDNAME,

          FIELDNAMES TYPE  TABLE OF FIELDNAME WITH DEFAULT KEY,

          LT_MARA TYPE STANDARD TABLE OF MARA,

          LO_DREF TYPE REF TO DATA

          .

 

 

    FIELD-SYMBOLS : <FS_TABLE> TYPE STANDARD TABLE,

                    <FS_FIELDVAL>  TYPE ANY,

                    <LFS_FIELDNAME> LIKE LINE OF FIELDNAMES.

 

 

    DEFINE CREATE_TABLE.

      TABLENAME = &1.

      CREATE DATA &2 TYPE STANDARD TABLE OF Tablename.

    END-OF-DEFINITION.

 

 

 

    CREATE_TABLE 'MARA' lo_dref.

    assign lo_dref->* to <fs_table>.

 

 

    FNAME = 'MATNR'.

    APPEND INITIAL LINE TO FIELDNAMES ASSIGNING <LFS_FIELDNAME>.

    <LFS_FIELDNAME> = FNAME.

 

 

    FNAME = 'MTART'.

    APPEND INITIAL LINE TO FIELDNAMES ASSIGNING <LFS_FIELDNAME>.

    <LFS_FIELDNAME> = FNAME.

 

  **Dynamic query here!!

    SELECT (FIELDNAMES)

      FROM (TABLENAME)

      UP TO 20 ROWS

      INTO CORRESPONDING FIELDS OF TABLE <FS_TABLE>.

   

  ENDMETHOD.                    "dynamic_sql_query

 

 

Do you think this is the right approach!!!


Viewing all articles
Browse latest Browse all 8332

Trending Articles