‎2007 Apr 11 6:35 AM
hi,
what is the Difference between TYPE and LIKE declarations.
thanks.
‎2007 Apr 11 6:41 AM
Hi,
&----
*& Report ZTYPES *
*& *
&----
REPORT ZTYPES .
Table declaration (old method)
DATA: BEGIN OF tab_ekpo OCCURS 0, "itab with header line
ebeln TYPE ekpo-ebeln,
ebelp TYPE ekpo-ebelp,
END OF tab_ekpo.
*Table declaration (new method) "USE THIS WAY!!!
TYPES: BEGIN OF t_ekpo,
ebeln TYPE ekpo-ebeln,
ebelp TYPE ekpo-ebelp,
END OF t_ekpo.
DATA: it_ekpo TYPE STANDARD TABLE OF t_ekpo INITIAL SIZE 0, "itab
wa_ekpo TYPE t_ekpo. "work area (header line)
Build internal table and work area from existing internal table
DATA: it_datatab LIKE tab_ekpo OCCURS 0, "old method
wa_datatab LIKE LINE OF tab_ekpo.
Build internal table and work area from existing internal table,
adding additional fields
TYPES: BEGIN OF t_repdata.
INCLUDE STRUCTURE tab_ekpo. "could include EKKO table itself!!
TYPES: bukrs TYPE ekpo-werks,
bstyp TYPE ekpo-bukrs.
TYPES: END OF t_repdata.
DATA: it_repdata TYPE STANDARD TABLE OF t_repdata INITIAL SIZE 0, "itab
wa_repdata TYPE t_repdata. "work area (header line)
Refer this link
http://www.sapdevelopment.co.uk/tips/tips_itab.htm
<b>type -- refer to data type (may be user defined using types)
like -- refer to database fields.</b>
regards
ravish
<b>*plz reward if helpful</b>
‎2007 Apr 11 6:36 AM
Hi Brahma reddy,
Check this info.
The Statements TYPES and DATA
Each ABAP program define its own data types using the statement.
TYPES dtype [TYPE type|LIKE dobj] ...
and declare its own variables or instance attributes of classes using the statement
DATA var [{TYPE type}|{LIKE dobj}] ...
Within the program or a class, you can also define local data types and variables within procedures. Local variables in procedures obscure identically-named variables in the main program or class.
When creating data types and data objects, there are a number of naming convention that also apply for other local program definitions, such as procedures. These are described in detail in the keyword documentation.
The Additions TYPE and LIKE
The additions TYPE type and LIKE dobj are used in various ABAP statements. The additions can have various meanings, depending on the syntax and context.
Definition of local types in a program
Declaration of data objects
Dynamic creation of data objects
Specification of the type of formal parameters in subroutines
Specification of the type of formal parameters in methods
Specification of the type of field symbols
Constructing New Data Types
The TYPE addition allows you to construct new data types in the TYPES, DATA; CONSTANTS; and STATICSstatements. In the TYPES statement, these are local data types in the program. In the other statements, they are attributes of new data objects, meaning that the newly defined data types are not free-standing. Rather, they are linked to database objects.This means that you can refer to them using the LIKEaddition, but not using TYPE.
To construct new data types, the addition TYPE can be used with the following type constructors:
Construction of reference types
REF TO type|dobj
Construction of structured data types
BEGIN OF struc_type.
...
END OF struc_type.
Construction of table types
tabkind OF linetype [WITH key]
These data types only exist during the runtime of the ABAP program.
Referring to Known Data Types or Data Objects
Using the additions TYPE or LIKE in the TYPESstatement, local data types in a program can be referred to known data types or data objects. This is mainly the case with user-defined elementary data types. If you declare variables using the additions TYPE type or LIKE dobj with statement DATA, the data type of var is already fully defined before the declaration is made.
The known types or data that are referred to must be visible at the point where the data type or variable is declared.
A known data type can be any of the following:
A predefined ABAP type to which you refer using the TYPE addition
An existing local data type in the program to which you refer using the TYPE addition
The data type of a local data object in the program to which you refer using the LIKE addition
A data type in the ABAP Dictionary to which you refer using the TYPE addition. To ensure compatibility with earlier releases, it is still possible to use the LIKE addition to refer to database tables and flat structures in the ABAP Dictionary. However, you should use the TYPE addition in new programs.
The LIKE addition takes its technical attributes from a visible data object. As a rule, you can use LIKE to refer to any object that has been declared using DATA or a similar statement, and is visible in the current context. The data object only has to have been declared. It is irrelevant whether the data object already exists in memory when you make the LIKE reference.
In principle, the local data objects in the same program are visible. As with local data types, there is a difference between local data objects in procedures and global data objects. Data objects defined in a procedure obscure other objects with the same name that are declared in the global declarations of the program.
You can also refer to the data objects of other visible ABAP programs. These might be, for example, the visible attributes of global classes in class pools. If a global class cl_lobal has a public instance attribute or static attribute attr, you can refer to it as follows in any ABAP program:
DATA dref TYPE REF TO cl_global.
DATA: f1 LIKE cl_global=>attr,
f2 LIKE dref->attr.
You can access the technical properties of an instance attribute using the class name and a reference variable without first having to create an object. The properties of the attributes of a class are not instance-specific and belong to the static properties of the class.
TYPES: BEGIN OF struct,
number_1 TYPE i,
number_2 TYPE p DECIMALS 2,
END OF struct.
DATA: wa_struct TYPE struct,
number LIKE wa_struct-number_2,
date LIKE sy-datum,
time TYPE t,
text TYPE string,
company TYPE s_carr_id.
This example declares variables with reference to the internal type STRUCT in the program, a component of an existing data object wa_struct, the predefined data object SY-DATUM, the predefined ABAP type t and STRING, and the data element S_CARR_ID from the ABAP Dictionary.
Referring to Generic Data Types
If you refer to one of the generic predefined ABAP types of fixed length (c, n, p, x) in the TYPES or DATA statement, you must specify the undefined technical attributes.
TYPES|DATA var[(length)] TYPE type [DECIMALS dec]...
TYPES|DATA var TYPE type [LENGTH len] [DECIMALS dec]...
DATA: text1,
text2 LENGTH 2,
text3 TYPE c LENGTH 3,
pack TYPE p DECIMALS 2 VALUE '1.225'.
This example creates three character variables with field lengths of one, two, and three bytes respectively, and a packed number variable with field length 8 bytes and two decimal places. If the attribute Fixed point arithmetic is set, the value of pack is 1.23.
This example shows how to declare elementary data objects with reference to predefined ABAP types.
PROGRAM demo_elementary_data_objects.
DATA text1 TYPE c LENGTH 20.
DATA text2 TYPE string.
DATA number TYPE i.
text1 = 'The number'.
number = 100.
text2 = 'is an integer.'.
WRITE: text1, number, text2.
This program produces the following output on the screen:
The number 100 is an integer.
In this example, the data objects text1, text2 and number are declared with the DATA statement. The technical attributes are determined by referring to the predefined ABAP types c, string, and I. Values from unnamed literals are assigned to the data objects. The contents of the named data objects are displayed on the list.
Specifying a Start Value
When you declare an elementary fixed-length variable, the DATAstatement automatically fills it with the type-specific initial value as listed in the table in the Predefined ABAP Types section.
However, you can also specify a starting value of a fixed-length elementary variable (also within a structure declaration) using the VALUE addition in the DATAstatement:
DATA var ... VALUE val|{IS INITIAL}.
Specifying start values:
DATA: counter TYPE p VALUE 1,
date TYPE d VALUE '19980601',
flag TYPE n VALUE IS INITIAL.
After this data declaration, the character string flag contains its type specific
Initial value 0
Hope this resolves your query.
Reward all the helpful answers.
Regards
‎2007 Apr 11 6:37 AM
Hi,
Diff bn TYPE N LIKE.
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
Regards,
Priyanka.
‎2007 Apr 11 6:38 AM
hi,
TYPE is used while refering to the data types and types declared using types statement, where as LIKE is used to refer to the data objects.
http://help.sap.com/saphelp_nw2004s/helpdata/en/fc/eb2ff3358411d1829f0000e829fbfe/frameset.htm
For TYPE
http://help.sap.com/saphelp_47x200/helpdata/en/d3/2e974d35c511d1829f0000e829fbfe/frameset.htm
For LIKE
http://help.sap.com/saphelp_47x200/helpdata/en/d3/2e974d35c511d1829f0000e829fbfe/frameset.htm
http://help.sap.com/saphelp_nw2004s/helpdata/en/fc/eb367a358411d1829f0000e829fbfe/content.htm
LIKE means the datatype of the variable is similar to the referenced variable.
TYPE means it is a predefined data type.
Eg:
DATA int TYPE i.
Here int is of integer data type.
DATA var LIKE int.
var IS a variable having same data type of int. which in turn is integer.
You can find these helpful when you reference database table variables... You need not know what is the datatype defined.
Also it adds to FLEXIBILITY.
Whenever you make changes to your database tables and fields,
that change is REFLECTED back to your program that is,
You need not change all your program code when you change your table fields...
‎2007 Apr 11 6:39 AM
hi,
chk out the example
data: v_var1 type c.
data v_var2 like v_var1.
type is used for referring standard datatypes.
like is used for referring user defined types.
regards,
Navneeth.K
‎2007 Apr 11 6:41 AM
Hi,
&----
*& Report ZTYPES *
*& *
&----
REPORT ZTYPES .
Table declaration (old method)
DATA: BEGIN OF tab_ekpo OCCURS 0, "itab with header line
ebeln TYPE ekpo-ebeln,
ebelp TYPE ekpo-ebelp,
END OF tab_ekpo.
*Table declaration (new method) "USE THIS WAY!!!
TYPES: BEGIN OF t_ekpo,
ebeln TYPE ekpo-ebeln,
ebelp TYPE ekpo-ebelp,
END OF t_ekpo.
DATA: it_ekpo TYPE STANDARD TABLE OF t_ekpo INITIAL SIZE 0, "itab
wa_ekpo TYPE t_ekpo. "work area (header line)
Build internal table and work area from existing internal table
DATA: it_datatab LIKE tab_ekpo OCCURS 0, "old method
wa_datatab LIKE LINE OF tab_ekpo.
Build internal table and work area from existing internal table,
adding additional fields
TYPES: BEGIN OF t_repdata.
INCLUDE STRUCTURE tab_ekpo. "could include EKKO table itself!!
TYPES: bukrs TYPE ekpo-werks,
bstyp TYPE ekpo-bukrs.
TYPES: END OF t_repdata.
DATA: it_repdata TYPE STANDARD TABLE OF t_repdata INITIAL SIZE 0, "itab
wa_repdata TYPE t_repdata. "work area (header line)
Refer this link
http://www.sapdevelopment.co.uk/tips/tips_itab.htm
<b>type -- refer to data type (may be user defined using types)
like -- refer to database fields.</b>
regards
ravish
<b>*plz reward if helpful</b>
‎2007 Apr 11 6:41 AM
hi,
http://help.sap.com/saphelp_nw2004s/helpdata/en/fc/eb2ff3358411d1829f0000e829fbfe/frameset.htm
For TYPE
http://help.sap.com/saphelp_47x200/helpdata/en/d3/2e974d35c511d1829f0000e829fbfe/frameset.htm
For LIKE
http://help.sap.com/saphelp_47x200/helpdata/en/d3/2e974d35c511d1829f0000e829fbfe/frameset.htm
http://help.sap.com/saphelp_nw2004s/helpdata/en/fc/eb367a358411d1829f0000e829fbfe/content.htm
LIKE means the datatype of the variable is similar to the referenced variable.
TYPE means it is a predefined data type.
Eg:
DATA int TYPE i.
Here int is of integer data type.
DATA var LIKE int.
var IS a variable having same data type of int. which in turn is integer.
You can find these helpful when you reference database table variables... You need not know what is the datatype defined.
Also it adds to FLEXIBILITY.
Whenever you make changes to your database tables and fields,
that change is REFLECTED back to your program that is,
You need not change all your program code when you change your table fields...
Rgds
Anversha
‎2007 Apr 11 6:47 AM
Hi,
1. TYPE doesnot occupy any memory tht means they r just like structure we use in our prog where as LIKE has its own memory and also if we define using like it takes all the properties of tht data object but same is not the case with type.
2. TYPE - Type is used to tell the system what is the type of data object(variable) you want to create .
ie something like char , float, int that we have in C.
with ABAP you can have additional user defined types which you can create using the TYPES statement and using the already existent types .(something like structures).
This can be done in either SE38 ABAP editor or in SE11(ABAP dictionary).
DATA: this sattement is used to declare you varible as in c to tell the system that you will be using a data object which is of the type you have mentioned.
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.
Please reward points if helpful.
Regards
Vana
‎2007 Apr 11 6:51 AM
Hi,
Go through this link
http://help.sap.com/saphelp_nw2004s/helpdata/en/fc/eb2ff3358411d1829f0000e829fbfe/content.htm
http://help.sap.com/saphelp_nw2004s/helpdata/en/fc/eb2ff3358411d1829f0000e829fbfe/content.htm
Refer this thread it will help u to differentiate betwn the 2.
Rgds,
Prakash
‎2007 Apr 11 6:57 AM
Hi brahma,
1. When you take reference of objects that occupies memory ( data elements,variables decalared with DATA) then use LIKE
2. and when you are taking ref. to a elementry data types(C,I,D,T...) or to a variables decalared with TYPES statement then we use TYPE
regards,
amit m.