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,
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.
This is from the ABAP keyword help in 6.40, not sure if it works in previous releases or not.
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 )`.
INTO CORRESPONDING FIELDS OF TABLE itab.
LOOP AT itab INTO wa.
WRITE: / wa-fldate, wa-carrname, wa-connid.
Thank you for your response and is very helful.
But, I did not understand the "ABAP keyword help in 6.40".
Besides I also tried to use the program that you gave me, but I get a syntax error. Is it because , I have R/3 release 4.6c?
How can I get this workaround in R/3 release 4.6c.
Thanks a lot,
The keyword help is what comes up when you click the blue 'i' icon in SE38 and select the third option with a keyword.
It is very much possible that this functionality is not present in 4.6C. Then you have to dynamically generate a program or subroutine pool to do the same.