5 Replies Latest reply: Mar 3, 2008 11:55 AM by Ranjan MR RSS

How to construct Dynamic Join in ABAP?

Ranjan MR
Currently Being Moderated

Hello ALL,

 

I am typically faced with a situvation where in I need to write a dynamic JOIN SQL statement in ABAP.

 

For example :

 

REPORT ZTEST .

PARAMETERS: TBLNAME(50) DEFAULT 'SPFLI'.

DATA: TOTAL_ROWS TYPE P.

SELECT COUNT(*) FROM (TBLNAME) INTO TOTAL_ROWS.

WRITE: / TBLNAME, TOTAL_ROWS.

 

In the above example, here the fetch from Table is dynamic.

 

Where as if i have to conisder the following SQL statement which is a normal JOIN statement in ABAP,

 

select

  mara~matnr

  marc~werks

  from mara

  join marc on maramatnr = marcmatnr

  into table it_join

  where mara~mtart = 'FERT' and

        marc~werks = '3000'.

 

 

How do i convert this into DYNAMIC JOIN STATEMENT</b> .

 

Looking forward to your responses.

 

Thank you

  • Re: How to construct Dynamic Join in ABAP?
    Vilmos Nagy
    Currently Being Moderated

    Hi,

    This is from the ABAP keyword help in 6.40, not sure if it works in previous releases or not.

     

    Example

    Dynamic specification of the inner joins. The column specification after SELECT is also dynamic.

     

    PARAMETERS: p_cityfr TYPE spfli-cityfrom,

                p_cityto TYPE spfli-cityto.

     

    DATA: BEGIN OF wa,

             fldate TYPE sflight-fldate,

             carrname TYPE scarr-carrname,

             connid   TYPE spfli-connid,

           END OF wa.

     

    DATA itab LIKE SORTED TABLE OF wa

                   WITH UNIQUE KEY fldate carrname connid.

    DATA: column_syntax TYPE string,

          dbtab_syntax TYPE string.

     

    column_syntax = `ccarrname pconnid f~fldate`.

     

    dbtab_syntax = `( ( scarr AS c `

      & ` INNER JOIN spfli AS p ON pcarrid  = ccarrid`

      & ` AND p~cityfrom = p_cityfr`

      & ` AND p~cityto   = p_cityto )`

      & ` INNER JOIN sflight AS f ON fcarrid = pcarrid `

      & ` AND fconnid = pconnid )`.

     

    SELECT (column_syntax)

           FROM (dbtab_syntax)

           INTO CORRESPONDING FIELDS OF TABLE itab.

     

    LOOP AT itab INTO wa.

      WRITE: / wa-fldate, wa-carrname, wa-connid.

    ENDLOOP.

     

    Regards

    Vilmos

  • Re: How to construct Dynamic Join in ABAP?
    Ranjan MR
    Currently Being Moderated

    Thanks .

Actions