06_abap - assignments, conversions, and calculations

Upload: varaprasadp

Post on 14-Apr-2018

227 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/30/2019 06_ABAP - Assignments, Conversions, And Calculations

    1/28

    ABAP - Assignments,Conversions, and

    Calculations

  • 7/30/2019 06_ABAP - Assignments, Conversions, And Calculations

    2/28

    Contents

    Objectives

    Working with System Variables

    Assignment Statements

    Summary

  • 7/30/2019 06_ABAP - Assignments, Conversions, And Calculations

    3/28

    Contents

    Objectives

  • 7/30/2019 06_ABAP - Assignments, Conversions, And Calculations

    4/28

    Objectives

    Use common system variables

    Use the CLEAR statement and understand its effect onvariables and field strings

    Perform assignments using the MOVE and MOVE-CORRESPONDING statements

    Predict and perform data conversions using assignment

    statements

    Code mathematical expressions

  • 7/30/2019 06_ABAP - Assignments, Conversions, And Calculations

    5/28

    Contents

    Working with System Variables

  • 7/30/2019 06_ABAP - Assignments, Conversions, And Calculations

    6/28

    System Variables

    DDIC structure syst(in program: systor sy)

  • 7/30/2019 06_ABAP - Assignments, Conversions, And Calculations

    7/28

    Commonly Used System Variables

  • 7/30/2019 06_ABAP - Assignments, Conversions, And Calculations

    8/28

    System Variables

    REPORT y_vu_0901.

    TABLES lfa1.

    PARAMETERS p_land1 LIKE lfa1-land1 OBLIGATORY

    DEFAULT 'US'.

    WRITE: / 'Current date:', sy-datum,

    / 'Current time:', sy-uzeit,

    / 'Current user:', sy-uname,/ 'Vendors having country code', p_land1,

    /.

    SELECT * FROM lfa1

    WHERE land1 = p_land1

    ORDER BY lifnr.

    WRITE: / sy-dbcnt, lfa1-lifnr.

    ENDSELECT.

    WRITE: / sy-dbcnt, 'records found'.

    IF sy-subrc 0.

    WRITE: / 'No vendors exist for country', p_land1.

    ENDIF.

  • 7/30/2019 06_ABAP - Assignments, Conversions, And Calculations

    9/28

    System Variables

    REPORT y_vu_0901.

    TABLES t005t.

    PARAMETERS p_land1 LIKE lfa1-land1 OBLIGATORY DEFAULT 'US'.

    SELECT SINGLE * FROM t005t

    WHERE spras = sy-langu "current logon language

    AND land1 = p_land1.

    IF sy-subrc = 0.WRITE: 'Description:', t005t-landx.

    ELSE.

    WRITE: 'No description exists for', p_land1.

    ENDIF.

  • 7/30/2019 06_ABAP - Assignments, Conversions, And Calculations

    10/28

    Contents

    Assignment Statements

  • 7/30/2019 06_ABAP - Assignments, Conversions, And Calculations

    11/28

    Assignment Statements

    assignment statement: assigns value to variableor field string

    3 common assignment statements: CLEAR set value to default initial values

    MOVEmove a value from one field to another

    MOVE-CORRESPONDINGmove with matchingnames

  • 7/30/2019 06_ABAP - Assignments, Conversions, And Calculations

    12/28

    CLEAR statement

    Syntax: clear v1 [with v2 | with 'A' | with NULL]

    where: v1 and v2 are variable or field string names.

    'A' is a literal of any length.

    NOTES: If v1 type c filled with blanks.

    If other data type filled with zeros.

    If v1 is a field string cleared each components.

    with v2 the 1st byte from v2 is used to fill the entire length of v1.

    with 'A the 1stbyte from literal 'A is used to fill with NULL the entire length of v1 is filled with hexadecimal zeros.

  • 7/30/2019 06_ABAP - Assignments, Conversions, And Calculations

    13/28

    MOVE statement

    Syntax: move v1 to v2.

    v2 = v1.

    v2 = v1 = vm = vn

    move v1[+N(L)] to v2[+N(L)]. v2[+N(L)] = v1[+N(L)].

    where: v1 is the sending variable or field string.

    v2 is the receiving variable or field string. N is an offset from the beginning of the variable or field

    string.

    L is the number of bytes to move.

  • 7/30/2019 06_ABAP - Assignments, Conversions, And Calculations

    14/28

    MOVE statement

  • 7/30/2019 06_ABAP - Assignments, Conversions, And Calculations

    15/28

    Data Conversions

    Length Adjustment

  • 7/30/2019 06_ABAP - Assignments, Conversions, And Calculations

    16/28

    Subfields

    Subfield: the portion of a field

    Offset

    Length

    Syntax: v1[+o][(L)] = v2[+o][(L)].

    v1 and v2 are variable or field string names.

    o is a zero-based offset from the beginning of the

    field.

    L is a length in bytes.

  • 7/30/2019 06_ABAP - Assignments, Conversions, And Calculations

    17/28

    Subfields

    REPORT y_vu_0907.

    DATA: f1(7), f2(7).

    f1 = 'BOY'. "same as: move 'BOY' to f1.

    f2 = 'BIG'. "same as: move 'BIG' to f2.

    f1+0(1) = 'T'. "f1 now contains 'TOY '.

    WRITE / f1.

    f1(1) = 'J'. "same as: f1+0(1) = 'J'.

    WRITE / f1. "f1 now contains 'JOY '.

    f1(1) = f2. "same as: f1(1) = f2(1).

    WRITE / f1. "f1 now contains 'BOY '.

    f1+4 = f1. "same as: f1+4(3) = f1(3).

    WRITE / f1. "f1 now contains 'BOY BOY'.f1(3) = f2(3). "same as: f1+0(3) = f2+0(3).

    WRITE / f1. "f1 now contains 'BIG BOY'.

  • 7/30/2019 06_ABAP - Assignments, Conversions, And Calculations

    18/28

    Using MOVE with Field Strings

    Field string is treated as type C

  • 7/30/2019 06_ABAP - Assignments, Conversions, And Calculations

    19/28

    MOVE-CORRESPONDING statement

    MOVE-CORRESPONDING statement:

    from field string to field string

    MOVE statements for components with matchingnames

    Syntax: move-corresponding v1 to v2.

    v1 is the sending variable or field string.

    v2 is the receiving variable or field string.

  • 7/30/2019 06_ABAP - Assignments, Conversions, And Calculations

    20/28

    MOVE-CORRESPONDING statement

    data: begin of s1,

    c1 type p decimals 2 value '1234.56',

    c2(3) value 'ABC',

    c3(4) value '1234',

    end of s1,

    begin of s2,

    c1(8),x2(3) value 'XYZ',

    c3 type i,

    end of s2.

    move-corresponding s1 to s2.move s1-c1 to s2-c1.

    move s1-c3 to s2-c3.

  • 7/30/2019 06_ABAP - Assignments, Conversions, And Calculations

    21/28

    Performing Calculations

    COMPUTE

    ADD or ADD-CORRESPONDING

    SUBTRACT or SUBTRACT-CORRESPONDING

    MULTIPLY or MULTIPLY-CORRESPONDING

    DIVIDE or DIVIDE-CORRESPONDING

  • 7/30/2019 06_ABAP - Assignments, Conversions, And Calculations

    22/28

    COMPUTE Statement

    Syntax:

    compute v3 = v1 op v2 [op vn ...].

    v3 = v2 op v2 [op vn ...].

    where:

    v3 is the receiving variable for the result of the computation.

    v1, v2, and vn are the operands.

    op is a mathematical operator.

  • 7/30/2019 06_ABAP - Assignments, Conversions, And Calculations

    23/28

    COMPUTE Statement

  • 7/30/2019 06_ABAP - Assignments, Conversions, And Calculations

    24/28

    ADD or ADD-CORRESPONDING

    Syntax:

    add v1 to v2.

    add-corresponding s1 to s2.

    where:

    v2 is the variable being added to.

    v1 is the variable added to v2.

    s2 is the field string being added to. s1 is the field string added to s2.

    The same for SUBTRACT, MULTIPLY, and DIVIDE

  • 7/30/2019 06_ABAP - Assignments, Conversions, And Calculations

    25/28

    ADD, SUBTRACT, MULTIPLY, DIVIDEREPORT y_vu_0913.

    DATA: f1 TYPE i VALUE 2,

    f2 TYPE i VALUE 3,

    BEGIN OF s1,

    c1 TYPE i VALUE 10,

    c2 TYPE i VALUE 20,

    c3 TYPE i VALUE 30,

    END OF s1,

    BEGIN OF s2,c1 TYPE i VALUE 100,

    x2 TYPE i VALUE 200,

    c3 TYPE i VALUE 300,

    END OF s2.

    ADD f1 TO f2. WRITE / f2. "f1 is unchanged

    SUBTRACT f1 FROM f2. WRITE / f2.

    MULTIPLY f2 BY f1. WRITE / f2.

    DIVIDE f2 BY f1. WRITE / f2.

    ADD-CORRESPONDING s1 TO s2. WRITE: / s2-c1, s2-x2, s2-c3.

    SUBTRACT-CORRESPONDING s1 FROM s2. WRITE: / s2-c1, s2-x2, s2-c3.

    MULTIPLY-CORRESPONDING s2 BY s1. WRITE: / s2-c1, s2-x2, s2-c3.

    DIVIDE-CORRESPONDING s2 BY s1. WRITE: / s2-c1, s2-x2, s2-c3.

  • 7/30/2019 06_ABAP - Assignments, Conversions, And Calculations

    26/28

    Contents

    Summary

  • 7/30/2019 06_ABAP - Assignments, Conversions, And Calculations

    27/28

    Summary

    Structure syst system variables

    Assignment: CLEAR, MOVE, MOVE-CORRESPONDINGstatement

    Conversions are automatically performed when necessary

    A field string name without a component name type C

    Subfield: to access the individual bytes of a field's value

    COMPUTE, ADD, SUBTRACT, MULTIPLY, DIVIDE andCORRESPONDING

  • 7/30/2019 06_ABAP - Assignments, Conversions, And Calculations

    28/28

    Summary