intro_abap_01-data objects+ types

38
0 SAP AG 1999 D ata objects in program s Elem entary data objects S tructures C ontents D ata O bjects,Types,and Statem ents 54

Upload: ragi17

Post on 17-Aug-2015

224 views

Category:

Documents


2 download

DESCRIPTION

SAP ABAP data objects and Types

TRANSCRIPT

0SAP AG 1999 Data objects in programs Elementary data objects StructuresContentsData Objects, Types, and Statements540.2SAP AG 1999 Define local program types Define elementary data objects and structures Perform calculations se automatic type con!ersion appropriately"t t#e conclusion of t#is unit, you $ill be able to%Data Objects, Types, and Statements%nit Objecti!es550.&SAP AG 1999O!er!ie$ ' Data Objects, Types, and Statements"("P programOperationsObject)et#odDataDataSubroutineData560.*SAP AG 1999O!er!ie$ ' +ntroduction+ntroduction+ntroduction +ntroductionElementary data objectsElementary data objects Elementary data objectsStructuresStructures Structures570.,SAP AG 1999Definition of Data ObjectsDATA: varname TYPE type.Predefined "("PTypesxser'definedtypesifstringxstringtdT#e type attributesof t#e data objectmust be fullyspecified-lobaltypes int#e "("PDictionarycnp.o$ is t#e datastored/.o$ is itinterpreted/.o$ is t#e type .o$ is t#e typestructured/ structured/(y field (y field Predefined Predefined"("P type "("P type 0engt# 0engt# In general, data objects are defined using the DATA statement, as follows: After the name of the data object, fully specified type is assigned to it using the TYPE addition. The type is linked to the data object statically. It cannot be changed at runtime. There are other syntax variants available for historical reasons!. "ote however, that some of these historical variants are supported in A#A$ %bjects. &or further information, refer to the keyword documentation for the DATA statement. All types are based on predefined A#A$ types, which are discussed in greater detail on the Predefined ABAP Types slide. 'ome of these A#A$ types are fully specified and can be used themselves to typedata objects. %thers need to be modified to include the length, and in some cases the number of decimal places. These are the simplest examples of user(defined types. )ou can create complexstructured types of any level of complexity you want, based on elementary types. )ou can also define types directly in the ABAP Dictionary. )ou can then use these global types in allappropriate *epository objects in the system.580.1SAP AG 1999stringPredefined "("P Typespifncdtxxstring Data type2umeric types*34 .. 41+nteger5loating point numberPac6ed numberString7#e8adecimal5i8ed lengt# Description Static lengt# inbytesSe9uence of digitsSe9uence of c#aractersDateTime.e8idecimal codeSe9uence of c#aracters.e8idecimal code4 .. 1,,&, 4 .. 1,,&, 314 .. 1,,&, are split into% :ules for storage ;alue range "rit#metic usedOperations on bitsC#aracter types.e8adecimal:untime system adjusts lengt#automaticallyC#aracter string operations date calculations> time calculations"ttributes;ariablelengt# The following A#A$ types are fully specified and can be used themselves to typedata objects: d, t,i, f, string and xstring. A special feature of the string and xstring types is that their length is adjusted automatically at runtime. Thus, length is not a component of the type. The following A#A$ types are not fully specified, so you must extend them to include additional information before you use them to define data objects: c, n and x:)ou must extend the type to include the length,which must be from1 to 65535 characters.If you do not declare a length when you define a data object, the system assigns the default length of 1. p:)ou must extend the type to include the length, and number of decimal places if needed, which must be from1 to 16 bytes. If you do not declare a length when you define this data object, the system assigns the default length of 8 bytes that is, +, digits! and 0 decimal places. The type also contains information on how the data is stored, what range of values is allowed, what operations can be performed on the data, and what sort of arithmetic is used if the data object is suitable for use in calculations!. -e will deal with this topic in greater detail later in this unit.590.?SAP AG 1999;isibility of Data Objects"("P programSubroutine0ocal data objects%Only !isible in onemodulari@ation unit-lobal data objects%!isible t#roug#out t#eentire "("P programTemporary runtime objects0egend 0egend)emory madea!ailable fordata objects The extent to which data objects are visible depends on the context. &or data objects defined using the DATA statement, the following rules apply: If the DATA statement occurs between a FORM and ENDFORM statement, it defines a local data object in that subroutine. At all other points in the source code with very few exceptions!, the DATA statement defines a global data object, visible throughout the program. .ocal objects obscure global data objects of the same name. /ata objects defined using the TABE! statement that is, structures! are always global, even if this TABE! statement occurs between FORM and ENDFORM. 600.30.ASAP AG 1999O!er!ie$ ' Definition of Data Objects+ntroduction+ntroduction +ntroductionElementary data objectsElementary data objects Elementary data objectsDefinitionDefinition Definition2umeric data objects2umeric data objects 2umeric data objectsStringsStrings StringsStructuresStructures Structures"utomatic type con!ersion"utomatic type con!ersion "utomatic type con!ersion5i8ed'lengt# data objects5i8ed'lengt# data objects 5i8ed'lengt# data objects610.40SAP AG 1999Definition of Elementary Data Objectsser'defined type%e8plicit type constructionDefinition of a data object$it# reference to a user'defined typeDefinition of a data object$it# reference to a fullyspecified predefined"("P typeDefinition $it# an implicittype constructionTYPE! : my"type#$% TYPE c&percentage"type#$% TYPE p DE'(MA! ).DATA : my"var TYPE my"type& percentage TYPE percentage"type.DATA: c*+nter TYPE i.

DATA: f,ag#-% TYPE c.TYPE!DATAmy"typepercentage"typemy"typepercentage"typeiDATA #-% TYPE cDATA TYPE iTYPETYPE#$% TYPE c#$% TYPE p DE'(MA! )Data object in#erits typeattributes from anot#erdata objectDATA: my"var".e,p (/E my"var. DATA (/E my"var )ou use the TYPE! statement to define your own data types.-hen defining c, n, or x types, you specify the length by entering the number of characters or digits in parentheses after the name. -hen defining p types, you specify the length by entering the number of bytes re0uired in parentheses after the name. )ou specify the number of decimal places using the DE'(MA! addition. The connection between the value range and the length of the type will be discussed in greater detail later in this chapter. )ou define data objects using the DATA statement. )ou assign a type to this data object using the TYPE addition. This can either be a user(defined type or a completely specified predefined A#A$ type. -hen defining a data object, you can also construct a type implicitly:If some information is not available for one of these variants, the system adds this information using type(specific default values: /efault length for objects of type c, n, and x: + character /efault length for objects of type p: 1 bytes2 default value for the number of decimal places: 3. )ou cannot declare a length when defining !TR(N0(type or 1!TR(N0(type data objects. Instead theruntime system adjusts the length of the object as necessary.Thus, the following definition is complete:DATA vstring TYPE string.620.44SAP AG 1999:untime (e#a!ior% +nitial ;alue and Start ;alue"("P runtime system"("P programgd"c.ar- 2 gd"c.ar).ADD - TO c*+nter.'EAR: gd"carrid-& gd"carrid)& c*+nter.Timegd"c.ar-TYPE!: c"type#$% TYPE c.DATA: gd"c.ar- TYPE c"type&gd"c.ar) TYPE c"type 3A4E 565&c*+nter type i.Start of lifetime Start of lifetimeDefinitions%gd"c.ar) c*+nterAB 7777AB AB 7777AB AB 777- 7777'EAR3A4E 5AB5 3A4E 5AB5Type'specific initial !alueStart !alue There is an appropriate initial value assigned to every predefined A#A$ type. To find out what these values are in each case, refer to the keyword documentation for the TYPE! statement, variant +, addition +. An initial value is also assigned to every user(defined type. This value is determined by taking the initial value of the predefined A#A$ type and the length and number of decimal places! declared forthe type. -hen the system enters the validity area, the data objects are instantiated and filled with the initial value. This rule applies: &or all global data objects at the start of the program &or all private and public attributes, when instantiating an object &or all global data objects in a function group, when calling a function module belonging to that function group for the first time &or all local data objects, when calling a subroutine, a method, or a function module If you assign a start value to the data object using the 3A4E addition to the DATA statement, this start value overwrites the initial value. At runtime, you can restore the type-specific initial value to a variable using the 'EAR statement. )ou can 0uery the status of this variable using the (! (N(T(A comparison. &or further information, refer to the keyword documentation for the 'EAR and (F statements.630.42SAP AG 1999:untime (e#a!ior% 5ields of Dynamic 0engt#"("P runtime system"("P programgd"string 2 5AB'DEF06(8/MNOP5.'EAR: gd"string.gd"string 2 5AB'D5.TimeDATA: gd"string TYPE string.Definitions%gd"stringAB'DEF06(8/MNOPAB'D'EAR:untime system adjustslengt# as appropriateStart of !alidity Start of !alidity -hen entering the validity area, there is no need for the system to reserve memory for elementary data objects of variable length, that is for strings !TR(N0 types! or byte se0uences 1!TR(N0 types!. The necessary memory is only allocated once a value has been assigned to the variable. 'EAR string deletes the content and restores the initial status, that is a string or byte se0uence whose length e0uals 4ero. The runtime system adjusts the length of the object as necessary at runtime. )ou can trace the runtime behavior of the string in debugging mode in terms of length!, by displaying the string in the field view and then double clicking the string name to obtain a detailed view. )ou can find out the length of the string at runtime using the !TREN# string % function.640.4&SAP AG 1999O!er!ie$ ' 2umeric data objects+ntroduction+ntroduction +ntroductionElementary data objectsElementary data objects Elementary data objectsDefinitionDefinition Definition2umeric data objects2umeric data objects 2umeric data objectsStringsStrings StringsStructuresStructures Structures"utomatic type con!ersion"utomatic type con!ersion "utomatic type con!ersion5i8ed'lengt# data objects5i8ed'lengt# data objects 5i8ed'lengt# data objects650.4*0.4,0.410.4?SAP AG 1999Summary% :ecommendations for sing 2umericData Types+f you need t#is% +f you need t#is% se t#is predefined "("P type% se t#is predefined "("P type%+ntegers onlyType i, since calculations using integerarit#metic are fastestDecimal numbers forbusiness calculationsType pDecimal numbers forroug# calculationsperformed on !ery smallor !ery large numbersType f660.430.4ASAP AG 1999DescriptionOperations0engt#;alue rangeCalculations5ormattingoptionsPredefined "("P Types for Strings4 ..4 .. 1,,&,1,,&, c#aracters c#aracters4 .. 1,,&, c#aracters3 digitsYYYYMMDD1 digits66MM!!Depends on codepage Depends on codepage(y -regoriancalender(y cloc6String operations String operationsType Type c c Type n Type d Type tTime DateSe9uenceof digits5i8ed'lengt# 5i8ed'lengt#c#ar. string c#ar. stringDatearit#meticTimearit#metic(ased on!alues in usermaster record66:MM:!!Digits!ariable !ariableType Typestring string;ariable' ;ariable'lengt# c#ar. lengt# c#ar.string string The value range of each string depends on the code page ( a table containing all the supported characters. Internally, each character is represented by a code number. -hen the system outputs the character, it uses the code page to convert this number. To find the code page valid in a given system,choose Tools -> CCMS -> Spool -> Spool administration ->Full administration -> Character sets. The initial of each character in a fixed(length string is a space character. "umeric strings are represented internally as character strings. "ote, however, that only digits are permissible as characters ( when character strings are assigned to numeric strings, the system ignoresletters and copies only the digits right(aligned!. 5issing characters are filled with 4eros. The initial value of each character in a numeric string is a 4ero. %nly se0uences of digits are valid for values of type d. These digits form a meaningful date, which complies with the 6regorian calendar. The first four digits represent the year, the next two the month and the last two the day. &or performance reasons however, the object is only checked if it is an inputfield on a screen or selection screen. The initial value of a date is 5777777775. The formatting options are determined by the user settings. &or values of type t, a se0uence of digits is only valid if it can be interpreted as a time on the 78(hourclock. The rules for interpreting the value are similar to those used for dates. The initial value of a time is 57777775.670.200.24SAP AG 1999DATA: date- TYPE d&date) TYPE syst9dat+m.!TART9OF9!EE'T(ON. date- 2 date) 2 sy9dat+m.:R(TE: date- DD;MM;YYYY&lementary fields to flat structures &lat structures to elementary fields In each of these cases, the system fills the target structure byte(by(byte from the left. If the target structure is longer than the source structure, the latter is filled with spaces. If the source structure contains numeric fields, values that cannot be interpreted may result in the target structure, unless the number of string characters in front of the numeric field is identical. 5oreover, target structures containing integer components may cause problems: if the source structure contains an integer field that is, a field four bytes long!, but the number of bytes in front ofthe integer field is not divisible by four, these bytes will be filled with 4eros until the integer address is divisible by four. To avoid this, copy the values component(by(component in these situations. The conversion rules fordata objects then apply. )ou should only use offset addressing on structures if all of the fields in the structure have a string type and all have a fixed length.810.&1SAP AG 1999"ssigning ;alues to +dentically'2amed StructureComponentsTYPE!:name"type#)E%TYPE c&tit,e"type#E%TYPE c&emai,"type#$E% TYPE c.DATA: BE0(N OF s"name& prename TYPE name"type& s+rname TYPE name"type& tit,e TYPE tit,e"type& END OF s"name.DATA: BE0(N OF s"address& prename TYPE name"type& s+rname TYPE name"type& tit,e TYPE tit,e"type& emai, TYPE emai,"type& END OF s"address....MO3E9'ORRE!POND(N0s"name TO s"address.MO3E9'ORRE!POND(N0TOMO3E: s"name9prename TO s"address9prename&s"name9s+rname TO s"address9s+rname&s"name9tit,e TO s"address9tit,e.interpreted internally as an interpreted internally as anassignment using assignment usingMO3E bet$een allidentically'named fields )ou can copy the contents of a source structure to a target structure component by component, using a MO3E9'ORRE!POND(N0 statement. The system then copies each source field to the target field with the same name. The conversion rules for elementary data objects then apply. "ote that the system does not check that identically named components have the same type. This canbe useful, but there are dangers involved.820.&?SAP AG 1999 Define local program types Define elementary data objects and structures Perform calculations se automatic type con!ersion appropriatelyBou are no$ able to%Data Objects, Types, and Statements% nitSummary83