So recently in a program, I am trying to clear couple of columns in an internal table. So instead of doing it the regular old way of looping into reference and clearing those fields, I used the new ABAP to do that job. Don’t reason with me on this, I am addicted to the new ABAP 😛 .
The first issue I faced is the internal table getting cleared completely(emptied) instead of just clearing just one column. See the code below:
test_table1 = VALUE #( FOR <test_line1> IN test_table1 ( VALUE #( BASE <test_line1> field1 = '' ) ) ).
This exact issue is pointed out in the below blog. So the ‘VALUE’ operator clears the left side before the processing of the right side and so it will return empty table instead of clearing it. See that blog in case if you’ve missed it. It is very interesting.
Now for this, I did the same. I used the “LET” expression to store my internal table data in a temporary table and cleared the column. Now test_table1 data will be cleared after assigning the test_table1 data to temp_table and we will do the for loop on temp_table.
test_table1 = VALUE #( LET temp_table = test_table1 IN FOR <test_line1> IN temp_table ( VALUE #( BASE <test_line1> field1 = '' ) ) ).
It worked 🙂 and it was all fun and games until my boy ABAP compiler started to throw errors 😀 .
But in ABAP, LET variables behaves similar to a normal variable declared using DATA except that you cannot access it outside the constructor expressions but they can be reused inside other constructor expression but we should pass the same type to the LET variable and it will become initial.
This is the reason for my errors as I’ve used the same LET variable to clear multiple tables.
There is an another way given by SAP where we can use the field symbol in the LET expression and you might think that it will hold the reference of the assigned variable and will be again be assigned with a different type in an another constructor expression( VALUE #( ) etc., ) but guess what, even the field symbol type is also fixed once used in the LET expression.
Note: It will not work the same way as variable ( LET <fs> = table will not work), I tried it with a different approach and found that it still fixes the type of <fs> and will not be updated in an another LET expression.
ABAP Documentation Link:
If you simply want to clear a column without conditions you can go for modify or corresponding using except. For conditional based you can go with this approach. see the comments below by Alexander and Dmitriy Kurshakov
BTW keep the let expression name simple. See the comments below bySuhas Saha