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

diffrence

Former Member
0 Likes
617

what is a diffrence between like and type?

5 REPLIES 5
Read only

Former Member
0 Likes
557

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

Thanks,

reward If Helpful.

Read only

Former Member
0 Likes
557

Hi Javed,

Chk Srinivas reply in this thread

Read only

Former Member
0 Likes
557

Hi,

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

.

chk this u will get better idea.

  • 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.

Refer the links -

Regards

Read only

Former Member
0 Likes
557

Hi,

The TYPE addition should be the only construction that allows a reference to data types, while the LIKE addition should only be used for data objects.

The Repository objects in the ABAP Dictionary are data types, not data objects. Outside the ABAP Objects context, the LIKE reference to database tables and flat structures in the ABAP Dictionary is still allowed for compatibility reasons.

Read only

Former Member
0 Likes
557

Hi,

Difference between 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

Reward if useful

Regards,

Asha