Skip to Content
-1

Question about using Assign Statement with Table Expression

Just a quick question for all the ABAP Experts out there.
For the below code should I use SY-SUBRC = 0 for the ASSIGN Keyword or CATCH CX_SY_ITAB_LINE_NOT_FOUND for the Table Expression? Not sure how this works... Thanks for any insight!

" CHECK IF EMPLOYEE FOUND IN BPC MASTER DATA DIM TABLE 
ASSIGN <lt_employee>[ (lc_id) = <lv_employee> ] TO FIELD-SYMBOL(<ls_employee>).
Add a comment
10|10000 characters needed characters exceeded

Related questions

4 Answers

  • Best Answer
    Posted on Oct 23, 2019 at 07:05 PM

    As said in the official ABAP documentation of ASSIGN table_exp TO <fs>:

    In this variant, the statement ASSIGN sets the return code sy-subrc.

    • If the specified row is found, sy-subrc is set to 0.
    • If the row is not found, sy-subrc is set to 4, except when the end of the table is reached in binary searches in sorted tables. In this case, sy-subrc is set to 8.

    If the specified row is not found, an exception is not raised (unlike in other uses of table expressions).

    So, no need of using TRY...CATCH (unless you change the XXX of ASSIGN itab[ XXX ] such a way that it can raise an exception.

    DATA(itab) = VALUE string_table( ).
    
    ASSIGN itab[ 1 ] TO FIELD-SYMBOL(<fs>). " It doesn't need TRY/CATCH
    ASSERT sy-subrc <> 0 AND <fs> IS NOT ASSIGNED.
    
    TRY.
        ASSIGN itab[ 1 / 0 ] TO <fs>. " It needs TRY/CATCH
      CATCH cx_root INTO DATA(lx).
    ENDTRY.
    ASSERT lx IS INSTANCE OF cx_sy_zerodivide.
    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Oct 23, 2019 at 02:14 PM

    Hello Kenneth Murray,

    Using the expression in TRY and CATCH Block is a better way of coding.

    You can also Use SY-SUBRC, the real problem arises when you try to use the Field Symbol.

    In case if the Field Symbol assignment is not successful then the code will dump when the Field symbol is used further.

    With Try Catch block you handle your Exception better, in case you go with SY-SUBRC just ensure when SY-SUBRC is not 0 don't use the field symbol further.

    There is also another keyword as below in case if you are looking for options:

    CHECK <fs> IS ASSIGNED.

    Regards!

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Oct 23, 2019 at 03:58 PM

    Hi Kenneth,

    As in many cases, the answer is 'it depends'.

    In principle, Satish is right when he says the 'TRY - CATCH' is cleaner. But it also comes with a lot of baggage... You have to manage the exceptions, propagate, react, all that jazz. If your statement is within a larger environment that has an exception management strategy, that would be the approach I would take.

    If, on the other hand, you just want a quick success check to, say, go left or right, depending on the success or failure of the ASSIGN - go with the SY_SUBRC. Quick, simple and without access baggage.

    Hope that helps,
    Mike

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Oct 24, 2019 at 09:29 AM

    I just want to add 1 more point here, if you using index inside table expression ( table[ number ] ) then its OK to wrap that code with TRY...CATCH, but if you are going to use component name (as your sample) instead and component name is wrong, then its become an uncatchable exception. So I think the best way to do it with component name is check the existence of component first:

    DATA(ref) = NEW flighttab( ).
    ASSIGN ref->* TO FIELD-SYMBOL(<f_flight>).
    SELECT * FROM sflight INTO TABLE @<f_flight>.
    "TRY..CATCH OK
    TRY .
        ASSIGN <f_flight>[ 1 / 0 ] TO FIELD-SYMBOL(<f_line1>).
      CATCH cx_root.
        BREAK-POINT.
    ENDTRY.
    "Dump
    TRY .
        ASSIGN <f_flight>[ ('CARRRID') = 'AA' ] TO FIELD-SYMBOL(<f_line2>).
      CATCH cx_root.
        BREAK-POINT.
    ENDTRY.
    
    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.