‎2018 Jul 17 8:04 AM
Hi,
I was just trying out a simple code using abap inline declaration. However, I am stuck at some particular lines of code . Below is the code snippet:
TYPES: BEGIN OF TY_FINAL,
VBELN TYPE VBELN_VA,
VKORG TYPE VKORG,
KUNNR TYPE KUNNR,
POSNR TYPE POSNR_VA,
MATNR TYPE MATNR,
END OF TY_FINAL.
DATA: L_REF TYPE REF TO DATA,
VBELN1 TYPE VBELN_VA.
SELECT-OPTIONS: S1 FOR VBELN1.
START-OF-SELECTION.
CREATE DATA L_REF TYPE TY_FINAL.
SELECT VBELN,
VKORG,
KUNNR
FROM VBAK
INTO TABLE @data(IT_VBAK)
WHERE VBELN IN @S1.
IF IT_VBAK IS NOT INITIAL.
SELECT VBELN,
POSNR,
MATNR
FROM VBAP
INTO TABLE @data(IT_VBAP)
FOR ALL ENTRIES IN @IT_VBAK
WHERE VBELN = @IT_VBAK-VBELN.
ENDIF.
ASSIGN IT_FINAL TO field-SYMBOL(<FS_FINAL>).
ASSIGN L_REF->* TO FIELD-SYMBOL(<FS_WA>) .
LOOP AT IT_VBAK ASSIGNING FIELD-SYMBOL(<FS_VBAK>).
<FS_WA>-VBELN = <FS_VBAK>-VBELN.
<FS_WA>-Vkorg = <FS_VBAK>-Vkorg.
<FS_WA>-kunnr = <FS_VBAK>-kunnr.
read table it_vbap assigning <fs_vbap> with key vbeln = <fs_vbak>-vbeln.
<FS_WA>-posnr = <FS_VBAp>-posnr.
<FS_WA>-matnr = <FS_VBap>-matnr.
APPEND <FS_WA> TO <FS_FINAL>.
ENDLOOP.
UNASSIGN <FS_WA> .
Here are my queries:
1) Is there a different way to declare the select options when working with inline declaration or do I have to use a variable using DATA statement like the conventional way (I had to declare a vbeln1 type vbeln_va) ?
2) How do I assign the internal table to a field symbol?
ASSIGN IT_FINAL TO field-SYMBOL(<FS_FINAL>).
3) How to create the reference variable (L_REF in this case) using inline declaration ?
Regards,
Matt
‎2018 Jul 17 10:53 AM
Read the documentation?
"Is there a different way to declare the select options when working with inline declaration or do I have to use a variable using DATA statement like the conventional way"
No, the only thing you can do (but that doesn't change the SELECT-OPTIONS statement):
DATA(var) = VALUE scarr-carrid( ).
SELECT-OPTIONS selopt FOR var.
"How do I assign the internal table to a field symbol?"
Exactly as shown, but of course, the internal table must exist, the position behind ASSIGN is not a declaration position:
DATA itab TYPE TABLE OF string.
ASSIGN itab TO FIELD-SYMBOL(<fs>).
"How to create the reference variable (L_REF in this case) using inline declaration":
Trivial ...
DATA var TYPE i.
GET REFERENCE OF var INTO DATA(dref1).
DATA(dref2) = REF #( var ).
‎2018 Jul 17 10:53 AM
Read the documentation?
"Is there a different way to declare the select options when working with inline declaration or do I have to use a variable using DATA statement like the conventional way"
No, the only thing you can do (but that doesn't change the SELECT-OPTIONS statement):
DATA(var) = VALUE scarr-carrid( ).
SELECT-OPTIONS selopt FOR var.
"How do I assign the internal table to a field symbol?"
Exactly as shown, but of course, the internal table must exist, the position behind ASSIGN is not a declaration position:
DATA itab TYPE TABLE OF string.
ASSIGN itab TO FIELD-SYMBOL(<fs>).
"How to create the reference variable (L_REF in this case) using inline declaration":
Trivial ...
DATA var TYPE i.
GET REFERENCE OF var INTO DATA(dref1).
DATA(dref2) = REF #( var ).
‎2018 Jul 17 12:04 PM
So even though I am using inline declaration in my program, in some cases I HAVE to use the DATA statement (like in the case of the internal table and the reference variable) ? I thought that the purpose of inline declaration was to eliminate the use the DATA statement completely for declaration purposes.
‎2018 Jul 17 12:08 PM
I thought that the purpose of inline declaration was to eliminate the use the DATA statement completely for declaration purposes.
Not really.I find inline declaration to improve readability, especially with helper variables.
‎2018 Jul 17 12:11 PM
"I thought that the purpose of inline declaration was to eliminate the use the DATA statement completely for declaration purposes."
Who said so? No you use it where appropriate. Of course, you can in fact get rid of DATA, but why?
TYPES itab TYPE STANDARD TABLE OF string WITH EMPTY KEY.
DATA(itab) = VALUE itab( ).
ASSIGN itab TO FIELD-SYMBOL(<fs>).
‎2018 Jul 17 10:57 AM
Your explanation is wrong regarding TABLES. With TABLES you declare a structure and not a table.
Simply, don't use TABLES ...
‎2018 Jul 17 12:09 PM
Thanks for using a code block - do you think next time you might nicely format the code as well, with indents, to make it easier to read?