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

Types statement

Former Member
0 Likes
1,171

Hi ABAP Experts,

I have some doubt on declaring Internal tables, could you tell me which case case1 or case 2 is correct.

Case1.

types: begin of t_kunnr,

name1(30) type c,

name2(30) type c,

kunnr(10) type c,

end of t_kunnr.

TYPES it_kunnr TYPE standard table of t_kunnr.

DATA: wa_kunnr TYPE t_kunnr.

DATA: it_kunnr type t_kunnr.

wa_kunnr-name1 = 'KK'.

wa_kunnr-name2 = 'MM'.

wa_kunnr-kunnr = '4999'.

append wa_kunnr to it_kunnr.

wa_kunnr-name1 = 'KK1'.

wa_kunnr-name2 = 'MM1'.

wa_kunnr-kunnr = '4999'.

append wa_kunnr to it_kunnr.

wa_kunnr-name1 = 'KK1'.

wa_kunnr-name2 = 'MM1'.

wa_kunnr-kunnr = '3999'.

append wa_kunnr to it_kunnr.

loop at it_kunnr into wa_kunnr.

write : / wa_kunnr-name1, wa_kunnr-kunnr.

endloop.

case2

types: begin of t_kunnr,

name1(30) type c,

name2(30) type c,

kunnr(10) type c,

end of t_kunnr.

DATA: wa_kunnr TYPE t_kunnr,

it_kunnr TYPE STANDARD TABLE OF t_kunnr.

wa_kunnr-name1 = 'KK'.

wa_kunnr-name2 = 'MM'.

wa_kunnr-kunnr = '4999'.

append wa_kunnr to it_kunnr.

wa_kunnr-name1 = 'KK1'.

wa_kunnr-name2 = 'MM1'.

wa_kunnr-kunnr = '4999'.

append wa_kunnr to it_kunnr.

wa_kunnr-name1 = 'KK1'.

wa_kunnr-name2 = 'MM1'.

wa_kunnr-kunnr = '3999'.

append wa_kunnr to it_kunnr.

loop at it_kunnr into wa_kunnr.

write : / wa_kunnr-name1, wa_kunnr-kunnr.

endloop.

Thanks in advance.

12 REPLIES 12
Read only

Former Member
0 Likes
1,131

HI,

Your case 2 is correct. If you declare as DATA: it_kunnr type t_kunnr. it will be considered as a work area rather than a internal table . Also clear the internal table header after appending.


types: begin of t_kunnr,
name1(30) type c,
name2(30) type c,
kunnr(10) type c,
end of t_kunnr.

DATA: wa_kunnr TYPE t_kunnr,
it_kunnr TYPE STANDARD TABLE OF t_kunnr.

wa_kunnr-name1 = 'KK'.
wa_kunnr-name2 = 'MM'.
wa_kunnr-kunnr = '4999'.
append wa_kunnr to it_kunnr.
clear it_kunnr.

wa_kunnr-name1 = 'KK1'.
wa_kunnr-name2 = 'MM1'.
wa_kunnr-kunnr = '4999'.
append wa_kunnr to it_kunnr.
clear it_kunnr.

wa_kunnr-name1 = 'KK1'.
wa_kunnr-name2 = 'MM1'.
wa_kunnr-kunnr = '3999'.
append wa_kunnr to it_kunnr.
clear it_kunnr.

loop at it_kunnr into wa_kunnr.
write : / wa_kunnr-name1, wa_kunnr-kunnr.
endloop.

Regards,

Vik

Read only

Former Member
0 Likes
1,131

case 2 is correct.

Read only

Former Member
0 Likes
1,131

case2 is correct

Read only

Former Member
0 Likes
1,131

Hi,

Case 2 is correct.

Because in case 1 , you are overriting table declartion.

Read only

0 Likes
1,131

Hi Thanks for your replies,

Could you explain me how case1 is correct one.

Read only

0 Likes
1,131

Hi Sorry,

Sorry for above reply, Could you explain me how case 2 declaration is correct.

Read only

0 Likes
1,131

Hi All,

Thanks for all your replies.

I got it.

Read only

Former Member
0 Likes
1,131

Hi,

If you need to declare Internal Tables, use DATA Statement.

You are declarinf Types and not Internal Tables.

Uou need to add OCCUR Clause to declare internal table, other wise it works like a work area only.

Example:

data : begin of t_kunnr occurs 0,

kunnr like kna1-kunnr,

name1 like kna1-name1,

end of t_kunnr.

Regds,

Anil

Read only

Former Member
0 Likes
1,131

hi,

case 2 is correct way to declare the internal table.

thanks

Ashu

Read only

Former Member
0 Likes
1,131

Case 2 is suggestable

in case1

TYPES it_kunnr TYPE standard table of t_kunnr.

this means u r declaring the structure again using TYPES.

Thanks,

ajay

Read only

Former Member
0 Likes
1,131

Hi,

As everyone has said case 2 is the right way.

In Case1.

TYPES it_kunnr TYPE standard table of t_kunnr.

DATA: it_kunnr type t_kunnr.

these two statements are wrong. First one rong because 'Types ' statement is used to decalre local program types, which only have a defenition and no related object in Data Base. Data statement is used to create objects in DB of a specific type.

Second statement is wrong because it only creates a DB object of structure type and not table type.

As suggested u can use occurs 0 additions but this is obsolete and not advisable anymore. This is because occurs 0 creates a table with a header line and header line concept is obsolete.

The best practise is to declare the Type using TYPES statement and the decalring table of that type using "type [STANDARD/HASHED/SORTED] table of " addition.

Read only

Former Member
0 Likes
1,131

Hi,

Case 2 is correct, Why becuase in case 1 your creating Type it_kunnr is not treating as an internal table but you are trying to append to it. So internal table always should be Data type only.

Regards,

Ganesh