Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Diff Between Type and like

Former Member
0 Likes
1,272

Hi Experts

I m confused between Type and like where to use Type keyword and where to use

Like..

Regards

Hitesh

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,213

Hi,

If you are refering to a <b>Data object</b> use <b>LIKE</b>

And if you are refering to a <b>Data type</b> use <b>TYPE</b>

You can create a variable that inherits exactly the same technical attributes as an existing data type or data object as follows:

<b>DATA <f> [TYPE <type>|LIKE <obj>]...</b>

If you use the <b>TYPE addition</b>, <type> is any data type with fully-specified technical attributes. This can be a:

Non-generic predefined ABAP type (D, F, I, T, STRING, XSTRING)

Any existing local data type in the program.

Any ABAP Dictionary data type

If you use the <b>LIKE addition</b>, <obj> is a data object that has already been declared. This can also be a predefined data object. The variable <f> adopts the same technical attributes as the data object <obj>. You can also use LIKE to refer to a line of an internal table that has already been declared as a data object:

<b>DATA <f> LIKE LINE OF <itab>.</b>

To ensure compatibility with previous releases, <obj> can also be a database table, a view, a structure, or a component of a structure from the ABAP Dictionary.

The data types to which you refer can be elementary types, reference types, or complex types (structures or tables). For elementary field types, the variables are a single field in memory. When you declare a data type with fixed length (D, F, I, T) the system fixes the amount of memory that will be assigned. When you declare an object with a variable length (STRING, XSRTING), the system only assigns enough memory to administer the object. The length of the data object is managed dynamically at runtime. For structures, the variables are a sequence of variables, which may themselves also be included in further complex structures. The individual components take their name <ci> from the type <type> or object <obj>, and can be addressed using <f>-<c i> For tables, the memory contains administration entries that can be filled dynamically at runtime.

Regards,

10 REPLIES 10
Read only

Former Member
0 Likes
1,214

Hi,

If you are refering to a <b>Data object</b> use <b>LIKE</b>

And if you are refering to a <b>Data type</b> use <b>TYPE</b>

You can create a variable that inherits exactly the same technical attributes as an existing data type or data object as follows:

<b>DATA <f> [TYPE <type>|LIKE <obj>]...</b>

If you use the <b>TYPE addition</b>, <type> is any data type with fully-specified technical attributes. This can be a:

Non-generic predefined ABAP type (D, F, I, T, STRING, XSTRING)

Any existing local data type in the program.

Any ABAP Dictionary data type

If you use the <b>LIKE addition</b>, <obj> is a data object that has already been declared. This can also be a predefined data object. The variable <f> adopts the same technical attributes as the data object <obj>. You can also use LIKE to refer to a line of an internal table that has already been declared as a data object:

<b>DATA <f> LIKE LINE OF <itab>.</b>

To ensure compatibility with previous releases, <obj> can also be a database table, a view, a structure, or a component of a structure from the ABAP Dictionary.

The data types to which you refer can be elementary types, reference types, or complex types (structures or tables). For elementary field types, the variables are a single field in memory. When you declare a data type with fixed length (D, F, I, T) the system fixes the amount of memory that will be assigned. When you declare an object with a variable length (STRING, XSRTING), the system only assigns enough memory to administer the object. The length of the data object is managed dynamically at runtime. For structures, the variables are a sequence of variables, which may themselves also be included in further complex structures. The individual components take their name <ci> from the type <type> or object <obj>, and can be addressed using <f>-<c i> For tables, the memory contains administration entries that can be filled dynamically at runtime.

Regards,

Read only

Former Member
0 Likes
1,213

refer

Regards,

Santosh

Read only

Former Member
0 Likes
1,213

HI,

ABAP distinguishes between types and objects. Types are descriptions that do not occupy memory. Objects are instances of types, and do occupy their own memory space. A type describes the technical attributes of all of the objects with that type.

You can use the addition

TYPE <type>

to refer to any data type <type> that is already known at this point in the program.

DATA <f> TYPE <type>.

The data object <f> has a data type corresponding to the type <type>.

DATA <f> LIKE <obj>.

The data object <f> inherits all of the technical attributes of the data object <obj>.

Take an example :

types : begin of ty_tab,

name(30),

pwd(10),

end of ty_tab.

data : itab like ty_tab.

See here we declared the structure of ty_tab, which do not occupy memory. So if we run this, we will get compile time error like this : Field TY_TAB is unknown. It is neither in one of the specified tables nor defined by a DATA statement.

So in this case u need to correct the error with "TYPE" statement...like this.

types : begin of ty_tab,

name(30),

pwd(10),

end of ty_tab.

data : itab type ty_tab.

Refer these

http://help.sap.com/saphelp_nw2004s/helpdata/en/fc/eb2ff3358411d1829f0000e829fbfe/content.htm

http://help.sap.com/saphelp_nw2004s/helpdata/en/fc/eb2ff3358411d1829f0000e829fbfe/content.htm

Rgds,

Prakash

Read only

Former Member
0 Likes
1,213

Hi Hitesh,

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’

Internal Tables

Internal tables are dynamic variable data objects. Like all variables, you declare them using the DATA statement.

You can also declare static internal tables in procedures using the STATICSstatement, and static internal tables in classes using the CLASS-DATAstatement.

This description is restricted to the DATAstatement. However, it applies equally to the STATICS and CLASS-DATA statements.

Referring to Known Table Types

Like all other data objects, you can declare internal tables using the LIKE or TYPE addition of the DATA statement.

DATA itab TYPE type|LIKE obj [WITH HEADER LINE].

Here, the LIKE addition refers to an existing table object in the same program. The TYPE addition can refer to an internal type in the program declared using the TYPES statement, or a table type in the ABAP Dictionary.

You must ensure that you only refer to tables that are fully typed. Referring to generic table types (ANY TABLE, INDEX TABLE) or not specifying the key fully is not allowed (for exceptions, refer to Special Features of Standard Tables).

The WITH HEADER LINE addition is obsolete; you should no longer use it. Also see the keyword documentation.

The optional addition WITH HEADER LINE declares an extra data object with the same name and line type as the internal table. This data object is known as the header line of the internal table. You use it as a work area when working with the internal table (see Using the Header Line as a Work Area). When you use internal tables with header lines, you must remember that the header line and the body of the table have the same name. If you have an internal table with header line and you want to address the body of the table, you must indicate this by placing brackets after the table name (itab[]). Otherwise, ABAP interprets the name as the name of the header line and not of the body of the table. You can avoid this potential confusion by using internal tables without header lines. In particular, internal tables nested in structures or other internal tables must not have a header line, since this can lead to ambiguous expressions.

TYPES vector TYPE SORTED TABLE OF i WITH UNIQUE KEY table_line.

DATA: itab TYPE vector,

jtab LIKE itab WITH HEADER LINE.

  • MOVE itab TO jtab. <- Syntax error!

MOVE itab TO jtab[].

The table object itab is created with reference to the table type vector. The table object jtab has the same data type as itab. jtab also has a header line. In the first MOVE statement, jtab addresses the header line. Since this has the data type I, and the table type of itab cannot be converted into an elementary type, the MOVE statement causes a syntax error. The second MOVE statement is correct, since both operands are table objects.

Declaring New Internal Tables

You can use the DATA statement to construct new internal tables as well as using the LIKE or TYPEaddition to refer to existing types or objects. The table type that you construct does not exist in its own right; instead, it is only an attribute of the table object. You can refer to it using the LIKE addition, but not using TYPE. The syntax for constructing a table object in the DATA statement is similar to that for defining a table type in the TYPESstatement.

DATA itab TYPE|LIKE tabkind OF linetype WITH key

[INITIAL SIZE n]

[WITH HEADER LINE].

As illustrated when you define a table type yourself, the type constructor

tabkind OF linetype WITH key

defines the table type tabkind, the line type linetype, and the key key of the internal table itab. Since the technical attributes of data objects are always fully specified, the table must be fully specified in the DATAstatement. You cannot create generic table types (ANY TABLE, INDEX TABLE), only fully-typed tables (STANDARD TABLE and TABLE, SORTED TABLE, HASHED TABLE). You must also specify the key and whether it is to be unique (for exceptions, refer to Special Features of Standard Tables).

As in the TYPES statement, you can, if you wish, allocate an initial amount of memory to the internal table using the INITIAL SIZEaddition. You can create an internal table with a header line using the WITH HEADER LINE addition. The header line is created under the same conditions as apply when you refer to an existing table type.

DATA itab TYPE HASHED TABLE OF spfli

WITH UNIQUE KEY carrid connid.

The table object itab has the type hashed table, a line type corresponding to the flat structure SPFLI from the ABAP Dictionary, and a unique key with the key fields CARRID and CONNID. The internal table itab can be regarded as an internal template for the database table SPFLI. It is therefore particularly suitable for working with data from this database table as long as you only access it using the key.

Hope this resolves your query.

Reward all the helpful answers.

Regards

Read only

Former Member
0 Likes
1,213

Hi

When you use like you have to specify the structure with the field name

ex

data: begin of itab occurs 0,

vbeln like vbak-vbeln,

posnr like vbap-posnr,

end of itab.

else you give type in any other declaration.

Read only

Former Member
0 Likes
1,213

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.

Read only

Former Member
0 Likes
1,213

Hi,

If you want to refer the the existing data dictionary element use like, in this case the internal subroutines which r assigned to existing data element will be applicable for new one.

Type can be used for completely new data type strusture.

Thanks

Sandeep

Reward if helpful

Read only

Former Member
0 Likes
1,213

hi,

TYPE -

its refers datatype.

if it is your own field name u can use type.

eg : DATA VAR TYPE I.

LIKE -

its refers data objects.

to refer another field that is already existing use like.

eg : DATA VBELN LIKE VBRP-VBELN.

U can also declare DATA VBELN(10) TYPE C.

BOTH WILL SAME....

Senthil kumar.

Read only

Former Member
0 Likes
1,213

Hi hitesh,

1.

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.

2. try this code (just copy paste)

report abc.

types : char50(50) type c.

*----


type.

data : d1 type c, "--- native

d2 type n, "--- native

d25 type char50 , "----


User defined data type

d3 type bukrs, "---- data element / domain

d4 type persno, "---- data element / domain

d5 type t001, "---- table

d99 type c

.

data :

*l1 like c "----


Not Allowed

*l2 like n "----


Not Allowed

*l25 like char50 , "----


User defined data type

*l3 like bukrs "----


Not Allowed

*l4 like persno, "----


Not Allowed

l5 like t001 , "---- table

l99 like pa0001

.

I hope it helps.

regards,

amit M.

Read only

Former Member
0 Likes
1,213

Regards,

shafi