Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

SAP ABAP 7.5 SP 003 - Doubt with local type in program

0 Kudos

Experts,

I am learning SAP ABAP 7.5 programming based on ABAP to the future SAP Press book.

*Data declaration
types: begin of ty_data,
name type string,
age type i,
end of ty_data.
data: lit_data type table of ty_data.
field-symbols:<lfs_data> type ty_data.
*Fill records in internal table
append initial to lit_data assigning <lfs_data>.
<lfs_data>-name = 'Apple'.
<lfs_data>-age = 10.
append initial to lit_data assigning <lfs_data>.
<lfs_data>-name = 'Orange'.
<lfs_data>-age = 20.



I am aware that we can skip data declarations in ABAP 7.5 using data(record_count) = lines(lit_data). Can you please let me know how i can skip declaring local type ty_data & take advantage of data() statement in above scenario.

BR,

Aspire

5 REPLIES 5

Sandra_Rossi
Active Contributor
0 Kudos

You can't, because anyway, a data object must be declared (inline or explicitly) based on an existing type. So, if you want to create a new data type, then the only possibility is to use TYPES.

0 Kudos

Hi Sandra,

Thanks a ton for your valuable reply. I am so happy to see your reply. Extremely helpful to me.

I have one more question along the same lines of data declaration.

*Fetch data from MARD based on Selection screen parameters P_MATNR & P_RESWK
SELECT matnr, werks, labst FROM mard
            INTO TABLE @DATA(lit_mard)
              WHERE matnr = p_matnr AND 
                    werks = p_reswk.
if lit_mard is not initial.
field-symbols: <lfs_mard> like line of lit_mard.
loop at lit_mard assigning <lfs_mard>
...... some logic
endloop.
endif.

Sandra - My question is about data declaration for field symbol <LFS_MARD>. As seen above, I am explicitly declaring it provided lit_mard has records using LIKE LINE OF. For LIT_MARD, I am taking advantage of implicit data declaration feature(@DATA(LIT_MARD)).

Is my approach to declare field symbol correct? Is it possible to declare it implicitly as well?

Challenges that i see with this approach:

1) I would end up declaring field symbols throughout the program wherever its required just before its usage. I am aware of creating a class & using attribute of class as data type which i read in SAP Press book. Assuming I am not creating a class, can we have a better approach to declare field symbols?

2) Assume a scenario where i have to write select query inside loop(definitely not best practice. But let us assume for a moment).

I would not be able to declare field symbol before LIT_MARD gets declared using select query implicitly. In this case, data declaration for field symbol <LFS_MARD> has to be done forcefully inside loop. Is this correct?

3) Let us take same scenario from point 2 (Select inside loop). Say I am declaring LIT_MARD using @DATA() implicitly. In this case, every time select is called inside loop, LIT_MARD will be declared. Is this correct? Are we looking at switching to explicit data declaration and write LIT_MARD type table of TY_MARD outside loop as a solution? Is there any other better approach?

BR,

Aspire

0 Kudos

Hi Sandra,

From See Page 13 - ABAP 7.4 Features, I see below,

Above image explains, how we can skip LIKE LINE OF usage & replace it with inline declaration.

This leaves with me 1 question,

1) If we do inline declaration for field symbol using above approach, won't we do data declaration for every single record inside loop? Say we have 100 records in loop. Won't inline declaration for field symbol, declare it 100 times?

BR,

Aspire

0 Kudos

No. The declarations are never executed, so it won't loop.

0 Kudos

Hi Sandra,

Thanks a lot for your valuable reply. Sorry, I didn't get you completely.

Declarations are never executed - Does this mean, field symbol will be declared inline for the very first iteration of loop & the same won't happen for the remaining 99 iterations(Assuming we are looping for 100 records in this loop).

BR,

Aspire