‎2007 Oct 23 3:32 PM
Hello All,
Could you please explain me what is the differene between using "like" or "type" when defining parameters/variables. I am working with abap for one month now... but still have doubt about this. Are they produce the same result? Or something different happens at the background if we use one or the other? Pls explain with an example.
Appreciate your help.
Thanks in advanced!
‎2007 Oct 23 3:56 PM
This is type definition do not hold any data its just a structure..
types : begin of itab,
f1 type c,
f2 type c,
end of itab.
Now you can use TYPE not LIKE for this because this is just a structure.defining an internal table/ DATA OBJECT
data : it_itab TYPE STANDARD TABLE OF itab. "you are using type to structures
Now another table
data ITAB2 LIKE it_itab. "you can use LIKE to data Objects
you can 't do like this
data itab2 like itab. "this is wrong
*********
type is generally used for declaring variables, parameters for existing data types in abap
for ex: to declare a inter value and character variable of length 10 is as,
data: i1 type i,
c1(10) type c.
like generally refers to existing data objects in abap.
for ex:
data: matnr like mara-matnr,
vbeln like vbap-vbeln.
creating variables matnr, vbeln from existing fields of tables mara, vbap.
when user creates a user defiend structure for work areas, internal tables we generally use type keyword as
types: begin of itab,
.........
.........
........
end of itab.
data: itab1 type itab occurs 0 [with header line]
.............
*********
Go through the link.
http://help.sap.com/saphelp_nw2004s/helpdata/en/fc/eb2ff3358411d1829f0000e829fbfe/content.htm
TYPE - Type is used to tell the system what is the type of data object(variable) you want to create .
LIKE: If there is already a data object declared and you want to declare a similar data object you can just refer to the previous data object using like.
Check this thread.
https://forums.sdn.sap.com/click.jspa?searchID=711746&messageID=2270752
https://forums.sdn.sap.com/click.jspa?searchID=711746&messageID=512214
Like Addition
You use the LIKE addition, similarly to the TYP E addition , in various ABAP statements for defining data types and specifying the types of interface parameters or field symbols. The addition
LIKE <obj>
can be used in the same ABAP statements as the TYPE addition to refer to any data object <obj> that is already visible at that point in the program. The expression <obj> is either the name of the data object or the expression
LINE OF <table-object>
In this case, the LIKE addition describes the line type of a table object that is visible at that point in the program.
You use LIKE to make the new object or type inherit the technical attributes of an existing data object.
TYPE Addition
You use the TYPE addition in various ABAP statements for defining data types and specifying the types of interface parameters or field symbols. The TYPE addition can have various meanings depending on the syntax and context.
Referring to Known Data Types
You can use the addition
TYPE <type>
to refer to any data type <type> that is already known at this point in the program. It can be used in any of the statements listed below. The expression <obj> is either the name of the data object or the expression
LINE OF <table-type>
In this case, the TYPE addition describes the line type of a table type <table-type> that is visible at that point in the program.
‎2007 Oct 23 3:56 PM
This is type definition do not hold any data its just a structure..
types : begin of itab,
f1 type c,
f2 type c,
end of itab.
Now you can use TYPE not LIKE for this because this is just a structure.defining an internal table/ DATA OBJECT
data : it_itab TYPE STANDARD TABLE OF itab. "you are using type to structures
Now another table
data ITAB2 LIKE it_itab. "you can use LIKE to data Objects
you can 't do like this
data itab2 like itab. "this is wrong
*********
type is generally used for declaring variables, parameters for existing data types in abap
for ex: to declare a inter value and character variable of length 10 is as,
data: i1 type i,
c1(10) type c.
like generally refers to existing data objects in abap.
for ex:
data: matnr like mara-matnr,
vbeln like vbap-vbeln.
creating variables matnr, vbeln from existing fields of tables mara, vbap.
when user creates a user defiend structure for work areas, internal tables we generally use type keyword as
types: begin of itab,
.........
.........
........
end of itab.
data: itab1 type itab occurs 0 [with header line]
.............
*********
Go through the link.
http://help.sap.com/saphelp_nw2004s/helpdata/en/fc/eb2ff3358411d1829f0000e829fbfe/content.htm
TYPE - Type is used to tell the system what is the type of data object(variable) you want to create .
LIKE: If there is already a data object declared and you want to declare a similar data object you can just refer to the previous data object using like.
Check this thread.
https://forums.sdn.sap.com/click.jspa?searchID=711746&messageID=2270752
https://forums.sdn.sap.com/click.jspa?searchID=711746&messageID=512214
Like Addition
You use the LIKE addition, similarly to the TYP E addition , in various ABAP statements for defining data types and specifying the types of interface parameters or field symbols. The addition
LIKE <obj>
can be used in the same ABAP statements as the TYPE addition to refer to any data object <obj> that is already visible at that point in the program. The expression <obj> is either the name of the data object or the expression
LINE OF <table-object>
In this case, the LIKE addition describes the line type of a table object that is visible at that point in the program.
You use LIKE to make the new object or type inherit the technical attributes of an existing data object.
TYPE Addition
You use the TYPE addition in various ABAP statements for defining data types and specifying the types of interface parameters or field symbols. The TYPE addition can have various meanings depending on the syntax and context.
Referring to Known Data Types
You can use the addition
TYPE <type>
to refer to any data type <type> that is already known at this point in the program. It can be used in any of the statements listed below. The expression <obj> is either the name of the data object or the expression
LINE OF <table-type>
In this case, the TYPE addition describes the line type of a table type <table-type> that is visible at that point in the program.
‎2007 Oct 23 3:57 PM
Hi,
From the ABAP Keyword Documentation:
Syntax
DATA dobj { {TYPE [LINE OF] type}
| {LIKE [LINE OF] dobj} }
[VALUE val|{IS INITIAL}]
[READ-ONLY].
Effect
In the specification of a data type type or a data object dobj, the data type of the variable dobj is already fully defined before the declaration. The syntax and meaning of the additions TYPE and LIKE has exactly the same meaning as the definition of data types with TYPES, except that for DATA after TYPE a standard table type with a generic table key can be specified. In this case, a bound table type with a standard key is created.
Hope that helps.
Regards,
Amine