05_SAP - Defining Data in ABAP Part2

download 05_SAP - Defining Data in ABAP Part2

of 30

Transcript of 05_SAP - Defining Data in ABAP Part2

  • 7/30/2019 05_SAP - Defining Data in ABAP Part2

    1/30

    SAP - Defining Data in

    ABAP/4, Part 2

  • 7/30/2019 05_SAP - Defining Data in ABAP Part2

    2/30

    Reference materials

    SAP Teach Yourself ABAP in 21 Days-SAMS.pdf

    SAP BC ABAP Programming.pdf

    http://help.sap.com

    http://help.sap.com/http://help.sap.com/
  • 7/30/2019 05_SAP - Defining Data in ABAP Part2

    3/30

    Contents

    Objectives

    Defining Constants

    Defining Field Strings

    Defining Types

    Structured Types

    Summary

    Q&A

  • 7/30/2019 05_SAP - Defining Data in ABAP Part2

    4/30

    Contents

    Objectives

  • 7/30/2019 05_SAP - Defining Data in ABAP Part2

    5/30

    Objectives

    Use the TABLES statement to define field

    strings

    Understand the difference between fieldstrings defined using DATA and using TABLES

    Understand the TYPES statement and use it todefine your own data types

  • 7/30/2019 05_SAP - Defining Data in ABAP Part2

    6/30

    Contents

    Defining Constants

  • 7/30/2019 05_SAP - Defining Data in ABAP Part2

    7/30

    Constants

    value cannot be changed

    use the CONSTANTS statement

    1 pre-defined constant: SPACE (same as the literal )

    Syntax:

    constants c1[(l)] [type t] [decimals d] value 'xxx'.

    constants c1 like cv value 'xxx'.

    where:

    c1 is the name of the constant.

    cv is the name of a previously defined constant or variable, or is the name of a field that

    belongs to a table or structure in the Data Dictionary.

    (l) is the internal length specification. t is the data type.

    d is the number of decimal places (used only with type p).

    'xxx' is a literal that supplies the value of the constant.

  • 7/30/2019 05_SAP - Defining Data in ABAP Part2

    8/30

    Constants

    constants c1(2) type c value 'AA'.

    constants c2 like c1 value 'BB'.

    constants error_threshold type i value 5. constants amalgamation_date like sy-datum

    value '19970305'.

  • 7/30/2019 05_SAP - Defining Data in ABAP Part2

    9/30

    Contents

    Defining Field Strings

  • 7/30/2019 05_SAP - Defining Data in ABAP Part2

    10/30

    Field Strings

    Field String:

    ~ structure in DDIC

    Defined within ABAP/4 program

    2 statements to define field strings:

    DATA

    TABLES

  • 7/30/2019 05_SAP - Defining Data in ABAP Part2

    11/30

    DATA Statement

    Syntax: data: begin of fs1,

    f1[(l)] [type t] [decimals d] [value 'xxx'],

    f2[(l)] [type t] [decimals d] [value 'xxx'],

    ...end of fs1.

    data begin of fs1.

    data f1[(l)] [type t] [decimals d] [value 'xxx'].

    data f2[(l)] [type t] [decimals d] [value 'xxx'].

    ...[include structure st1.]

    data end of fs1.

    data fs1 like fs2.

  • 7/30/2019 05_SAP - Defining Data in ABAP Part2

    12/30

    DATA Statement

    where: fs1 is the field string name.

    f1 and f2 are the fields (also called components) of thefield string.

    fs2 is the name of a previously defined field string, or is thename of a table or structure in the Data Dictionary.

    (l) is the internal length specification.

    t is the data type.

    d is the number of decimal places (used only with type p).

    'xxx' is a literal that supplies a default value.

    st1 is the name of a structure or table in the DataDictionary.

  • 7/30/2019 05_SAP - Defining Data in ABAP Part2

    13/30

    DATA Statement

    REPORT y_vu_0802.DATA: BEGIN OF totals_1,

    region(7) VALUE 'unknown',

    debits(15) TYPE p,

    count TYPE i,

    END OF totals_1,

    totals_2 LIKE totals_1.

    totals_1-debits = 100.

    totals_1-count = 10.

    totals_2-debits = 200.

    WRITE: / totals_1-region, totals_1-debits, totals_1-count,

    / totals_2-region, totals_2-debits, totals_2-count.

  • 7/30/2019 05_SAP - Defining Data in ABAP Part2

    14/30

    A Field String Can Contain Another Field String

    REPORT y_vu_0803.

    DATA: BEGIN OF names,

    name1 LIKE kna1-name1,

    name2 LIKE kna1-name2,

    END OF names.

    DATA: BEGIN OF cust_info,

    number(10) TYPE n,

    nm LIKE names, "like a field stringEND OF cust_info.

    cust_info-number = 15.

    cust_info-nm-name1 = 'Jack'.

    cust_info-nm-name2 = 'Gordon'.

    WRITE: / cust_info-number,cust_info-nm-name1,

    cust_info-nm-name2.

  • 7/30/2019 05_SAP - Defining Data in ABAP Part2

    15/30

    Defined Exactly Like a DDIC Table or Structure

    REPORT y_vu_0804.

    DATA: my_lfa1LIKE lfa1, "like a table in the DDIC

    my_addr LIKE addr. "like a structure in the DDIC

    my_lfa1-name1 = 'Andrea Miller'.

    my_lfa1-telf1 = '1-243-2746'.

    my_addr-land1 = 'CA'.

    WRITE: / my_lfa1-name1,

    my_lfa1-name2,

    my_addr-land1.

  • 7/30/2019 05_SAP - Defining Data in ABAP Part2

    16/30

    Include DDIC Tables and Structures

    REPORT y_vu_0805.

    DATA BEGIN OF fs1.

    INCLUDE STRUCTURE lfa1.

    DATA: extra_field(3) TYPE c,

    END OF fs1.

    fs1-lifnr = 12.fs1-extra_field = 'xyz'.

    WRITE: / fs1-lifnr,

    fs1-extra_field.

  • 7/30/2019 05_SAP - Defining Data in ABAP Part2

    17/30

    LIKE vs INCLUDE

    REPORT y_vu_0806.

    DATA: BEGIN OF fs1,

    mylfa1 LIKE lfa1,

    extra_field(3) TYPE c,

    END OF fs1.

    fs1-mylfa1-lifnr = 12.

    fs1-extra_field = 'xyz'.

    WRITE: /fs1-mylfa1-lifnr,

    fs1-extra_field.

    REPORT y_vu_0805.

    DATA BEGIN OF fs1.

    INCLUDE STRUCTURE lfa1.

    DATA: extra_field(3) TYPE c,

    END OF fs1.

    fs1-lifnr = 12.

    fs1-extra_field = 'xyz'.

    WRITE: / fs1-lifnr,

    fs1-extra_field.

  • 7/30/2019 05_SAP - Defining Data in ABAP Part2

    18/30

    Using a Field String as a Variable of Type CHAR

    REPORT y_vu_0807.

    DATA: BEGIN OF fs1,

    c1 VALUE 'A',

    c2 VALUE 'B',

    c3 VALUE 'C',

    END OF fs1.

    WRITE: / fs1-c1, fs1-c2, fs1-c3,

    / fs1.

    fs1 = 'XYZ'.

    WRITE: / fs1-c1, fs1-c2, fs1-c3,

    / fs1.

  • 7/30/2019 05_SAP - Defining Data in ABAP Part2

    19/30

    Assignment Involving Two Field Strings

    REPORT y_vu_0808.

    DATA: BEGIN OF fs1,

    c1 VALUE 'A',

    c2 VALUE 'B',c3 VALUE 'C',

    END OF fs1,

    fs2 LIKE fs1.

    fs2 = fs1.

    WRITE: / fs2-c1, fs2-c2, fs2-c3.

  • 7/30/2019 05_SAP - Defining Data in ABAP Part2

    20/30

    TABLES Statement

    Syntax:

    tables fs1.

    where:

    fs1 is the field string name. A table or structure of the same name must exist in the Data

    Dictionary.

    REPORT y_vu_0809.

    TABLES lfa1.

    lfa1-name1 = 'Bugsy'.

    lfa1-land1 = 'US'.

    WRITE: / lfa1-name1, lfa1-land1.

  • 7/30/2019 05_SAP - Defining Data in ABAP Part2

    21/30

    TABLES Statement

    It defines a field string.

    It gives the program access to a database table ofthe same name, if one exists.

    REPORT y_vu_0810.

    TABLES lfa1.

    SELECT * FROM lfa1 INTO lfa1 ORDER BY lifnr.

    WRITE / lfa1-lifnr.

    ENDSELECT.

  • 7/30/2019 05_SAP - Defining Data in ABAP Part2

    22/30

    Contents

    Defining Types

  • 7/30/2019 05_SAP - Defining Data in ABAP Part2

    23/30

    Types

    to define own data types base on existing data types

    Syntax: types t1[(l)] [type t] [decimals d].

    types t1 like v1.

    where: t1 is the type name.

    v1 is the name of a variable previously defined in theprogram, or is the name of a field that belongs to a tableor structure in the Data Dictionary.

    (l) is the internal length specification.

    t is the data type.

    d is the number of decimal places (used only with type p).

  • 7/30/2019 05_SAP - Defining Data in ABAP Part2

    24/30

    TYPES statement

    REPORT y_vu_0811.

    TYPES char2(2) TYPE c.

    DATA: v1 TYPE char2 VALUE 'AB',

    v2 TYPE char2 VALUE 'CD'.

    WRITE: v1, v2.

  • 7/30/2019 05_SAP - Defining Data in ABAP Part2

    25/30

    Make Your Code Clearer and Easier to Read

    REPORT y_vu_0812.

    TYPES: dollars(16) TYPE p DECIMALS 2,

    lira(16) TYPE p DECIMALS 0. "italian lira have no decimals

    DATA: BEGIN OF american_sums,

    petty_cash TYPE dollars,

    pay_outs TYPE dollars,

    lump_sums TYPE dollars,END OF american_sums,

    BEGIN OF italian_sums,

    petty_cash TYPE lira,

    pay_outs TYPE lira,

    lump_sums TYPE lira,

    END OF italian_sums.

    american_sums-pay_outs = '9500.03'.

    "need quotes when literal contains a decimal

    italian_sums-lump_sums = 5141.

    WRITE: / american_sums-pay_outs,

    / italian_sums-lump_sums.

  • 7/30/2019 05_SAP - Defining Data in ABAP Part2

    26/30

    Contents

    Structured Types

  • 7/30/2019 05_SAP - Defining Data in ABAP Part2

    27/30

    Structured Types

    Structured Type: a user-defined type with the definition of a field string

    to Reduce Redundancy and Make Maintenance Easier

    REPORT y_vu_0813.

    TYPES: BEGIN OF address,

    street(25),

    city(20),

    region(7),

    country(15),

    postal_code(9),

    END OF address.

    DATA: customer_addr TYPE address,vendor_addr TYPE address,

    employee_addr TYPE address,

    shipto_addr TYPE address.

    customer_addr-street = '101 Memory Lane'.

    employee_addr-country = 'Transylvania'.

  • 7/30/2019 05_SAP - Defining Data in ABAP Part2

    28/30

    Contents

    Summary

  • 7/30/2019 05_SAP - Defining Data in ABAP Part2

    29/30

    Summary

    Field strings:

    like DDIC structures

    define using DATA or TABLES statement

    DATA: local or global visibility TABLES: global and external visibility

    TYPES statement

    define own data types

    User-defined types reduce redundancy and makemaintenance easier.

  • 7/30/2019 05_SAP - Defining Data in ABAP Part2

    30/30

    Thank you!

    Questions & Answers