2007 Jan 21 2:57 PM
Dear Friends,
like is used when you want to refer table fields
data v_matnr like mara-matnr
Type is used to define variable with system defined data types
data v_matnr(10) type c.
But my question is .
what will happen or what diff we can find when
data: v_matnr type mara-matnr is used than
data: v_matnr like mara-matnr ?
and what diff we can find when
data table1 like mara occurs 0 with header line.
and
data table1 type mara occurs 0 with header line ?
Thanx in advance
2007 Jan 21 6:01 PM
Him annapuma,
the identical use of TYPE and LIKE comes from the ABAP evolution: A couple of releases ago you could not define any structured TYPEs yourself, only refer to existing DDIC definitions. That time you declared a structured object LIKE an existing DDIC table or structure.
For compatibilty reasons, you can still declare LIKE existing DDIC type. TYPE means just a technical definition, LIKE refers to an existing data object.
I.e. in a subroutine, you may create a data object refering to the technical properties of a parameter passed. Say FORM xyz using P_whatever may have a local reference variable DATA: L_ref TYPE REF TO DATA and a statement CREATE l_ref like p_whatever.
Or you want to loop at a structure table using a field-symbol:
field-symbols: <itab> like line of itab.
loop at itab assigning <itab>.
...
Just to give some examples.
Conclusion: TYPE is for declaration of a data object with properties as described in the abstract TYPE definition (Basic type like C, N, X, ...; DDIC type as table, structure, data element, ...; selef defined TYPE declared with TYPES) whereas LIKE defines the dataobject with same properties as an existing data object.
Regards,
Clemens
Dear Friends,
like is used when you want to refer table fields
data v_matnr like mara-matnr
Type is used to define variable with system defined data types
data v_matnr(10) type c.
But my question is .
what will happen or what diff we can find when
data: v_matnr type mara-matnr is used than
data: v_matnr like mara-matnr ?
and what diff we can find when
data table1 like mara occurs 0 with header line.
and
data table1 type mara occurs 0 with header line ?
Thanx in advance
2007 Jan 21 3:05 PM
For all practical purposes there are the same. The only additional advantage with types is that you can define your own types(including complex ones) in the data dictionary and reuse them accross various programs.
But within a program if two variables are defined one using LIKE and another using TYPE, both referring to the same field, then there is no difference.
If I include a type pool within a program, then I can define my variables only using TYPE to refer to any type defined in that pool. I cannot use LIKE in this scenario. Also, if I want to use native types like C, N, etc, I cannot use LIKE there either. I can use LIKE ABC only if ABC is in the database or if ABC is defined previously in the same program.
I can use TYPE ABC, if ABC is defined in database as a TYPE and included in the program with the statement TYPE-POOLS. I can use it, if it is the native types. I can use it, if it is already defined in the dictionary as a structure/table or structure/table field, or even if it is defined as a data element or a domain. So I can declare a variable V_BUKRS TYPE BUKRS, but I cannot define a variable V_BUKRS LIKE BUKRS.
But if I intend to use V_BUKRS to store company code, I will prefer to declare it as V_BUKRS LIKE T001-BUKRS, only because if tomorrow for some reason, the definition of T001-BUKRS changes to a data element for example, BUKRS_N(say DEC 4) instead of the data element BUKRS(CHAR 4) that it refers to now, I don't have to change my programs because I am referring to the table field and inhereting its properties. Whereas, had I declared my V_BUKRS TYPE BUKRS and the table now changed to BUKRS_N, I will be forced to change my program as there will be a type incompatability.
2007 Jan 21 3:08 PM
2007 Jan 21 3:09 PM
2007 Jan 21 6:01 PM
Him annapuma,
the identical use of TYPE and LIKE comes from the ABAP evolution: A couple of releases ago you could not define any structured TYPEs yourself, only refer to existing DDIC definitions. That time you declared a structured object LIKE an existing DDIC table or structure.
For compatibilty reasons, you can still declare LIKE existing DDIC type. TYPE means just a technical definition, LIKE refers to an existing data object.
I.e. in a subroutine, you may create a data object refering to the technical properties of a parameter passed. Say FORM xyz using P_whatever may have a local reference variable DATA: L_ref TYPE REF TO DATA and a statement CREATE l_ref like p_whatever.
Or you want to loop at a structure table using a field-symbol:
field-symbols: <itab> like line of itab.
loop at itab assigning <itab>.
...
Just to give some examples.
Conclusion: TYPE is for declaration of a data object with properties as described in the abstract TYPE definition (Basic type like C, N, X, ...; DDIC type as table, structure, data element, ...; selef defined TYPE declared with TYPES) whereas LIKE defines the dataobject with same properties as an existing data object.
Regards,
Clemens