Application Development 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: 

type

Former Member
0 Kudos

what is difference between type and like?

4 REPLIES 4

Former Member
0 Kudos

type is refrence to directly data element....

like refers to the fields of the table means if the field defintion is chages then the variable refered with like is changed...

reward points if useful....

Former Member
0 Kudos

TYPE AND LIKE.....a very simple example....

data: ch type string,

ch1 like ch.

i.e. like can be used refer the generic data objects (in the above example ch,ch1 are called data objects)

type is used for refer the predefined data types (in the above example string in the predefined data type) and abap dictionary objects.

Kanagaraja_L
Active Contributor
0 Kudos

TYPE – When u want to create a data object of an existing data type E.g.

DATA gv_var1 TYPE i. “Create a data object of type integer.

LIKE - When u have a data object and u want to create a similar data object. E.g.

DATA gv_var2 LIKE gv_var1. “gv_var2 is an integer like gv_var1.

“LIKE” IS NOT RECOMMENDED FOR USE.

TYPES – When u want to create a data type and not an object.

TYPES ty_int TYPE i. “ ty_int is an integer type

Then u can use that as follows instead of INTEGER TYPE ‘I’.

DATA gv_var1 TYPE ty_int. “Create a data object of type integer.

By using ‘TYPES’ we create a local data type in ABAP program; by using DATA we create a data object.

Suppose you want to have a variable of type integer – u can do this in 2 ways –

DATA gt_int TYPE i.

Or

TYPES ty_int TYPE i. “ Creating a data type of type integer

DATA gt_int TYPE ty_int.

The above example was just to make you understand what the TYPES statement do, it does not make sense to use TYPES for the above case though,

Because you have an existing data type ‘I’ for integer.

Use of TYPES

Whenever you need any internal table in your program which should have same fields as in a database table, we can declare the internal table

Using LIKE addition.

DATA gt_itab LIKE <dbtab> WITH HEADER LINE.

But like addition is not recommended as a coding standard, and so is WITH HEADER LINE addition.

So what you should do is.

Define a data type using TYPES.

TYPES: BEGIN OF ty_name,

  • Fields of database table

  • Use this if u need only selected fields of database table in internal table

  • which is the requirement in most cases

name1 TYPE ad_name1,

name2 TYPE ad_name2,

str_suppl1 TYPE ad_strspp1,

str_suppl2 TYPE ad_strspp2,

street TYPE ad_street,

city1 TYPE ad_city1,

post_code1 TYPE ad_pstcd1,

fax_number TYPE ad_fxnmbr1,

name_first TYPE ad_namefir,

name_last TYPE ad_namelas,

END OF ty_name.

OR

TYPES: BEGIN OF ty_name.

  • Use this if you need all fields of a database table/structure

  • in your internal table

INCLUDE STRUCTURE <dbstruc>.

TYPES: END OF ty_name.

2. Declare an internal table object using the TYPE declared in step 1.

DATA: gt_itab TYPE TABLE OF ty_name.

3. Instead of header line in table, use a work area like a structure or a field symbol.

DATA: gs_wa TYPE ty_name. “ Structure

Or

FIELD-SYMBOLS : <fs_name> TYPE ty_name. “ Field Symbol

Kanagaraja L

Former Member
0 Kudos

Hi Srinath,

See this link.

Plzz Reward if useful,

Mahi.