10-17-2008 4:31 AM
Hi,
Given the following internal table.
TYPES: BEGIN OF ty_sflight,
sflight LIKE sflight,
perocc(4) TYPE p DECIMALS 2,
END OF ty_sflight.
DATA: gt_sflight TYPE TABLE OF ty_sflight.
First of all, what's the difference between using TYPE and LIKE, eg in sflight LIKE sflight. Both seem to work the same when I tried debugging.
Secondly, how do I populate the internal table? I tried the following but got an error:
SELECT sflight~carrid sflight~connid
INTO (gt_sflight-sflight-carrid, gt_sflight-sflight-connid) FROM sflight UP TO 50 ROWS.
ENDSELECT.
10-17-2008 5:44 AM
Hi,
The approach you have follwed will not give result , When you declared a structure type using types statement it is only a data type not an variable or actual object. When you are refering some thing with like in data command the refering object must be an variable or object not data type.
Coming to your sample prog the following code will you correct result, note gt_sflight is a table type required in structure type.
TYPES: BEGIN OF ty_sflight,
CARRID type S_CARR_ID,
CONNID type S_CONN_ID,
END OF ty_sflight.
DATA: gt_sflight type TABLE OF ty_sflight,
wt_sflight type ty_sflight.
SELECT CARRID CONNID
INTO (wt_sflight-CARRID,wt_sflight-Connid ) FROM sflight UP TO 50 ROWS.
append wt_sflight to gt_sflight.
clear wt_sflight.
ENDSELECT.
loop at gt_sflight into wt_sflight.
write:/ wt_sflight-carrid,
wt_sflight-connid.
endloop.
10-17-2008 5:44 AM
Hi,
The approach you have follwed will not give result , When you declared a structure type using types statement it is only a data type not an variable or actual object. When you are refering some thing with like in data command the refering object must be an variable or object not data type.
Coming to your sample prog the following code will you correct result, note gt_sflight is a table type required in structure type.
TYPES: BEGIN OF ty_sflight,
CARRID type S_CARR_ID,
CONNID type S_CONN_ID,
END OF ty_sflight.
DATA: gt_sflight type TABLE OF ty_sflight,
wt_sflight type ty_sflight.
SELECT CARRID CONNID
INTO (wt_sflight-CARRID,wt_sflight-Connid ) FROM sflight UP TO 50 ROWS.
append wt_sflight to gt_sflight.
clear wt_sflight.
ENDSELECT.
loop at gt_sflight into wt_sflight.
write:/ wt_sflight-carrid,
wt_sflight-connid.
endloop.
10-17-2008 5:55 AM
hi
here are the differences
1.)TYPE will only get the technical specification related to the data element (like data type and data length)
LIKE decalaration will get all the detailsa including semantic and technical detals related to the field or data element.
2.)It is good Programming practivce to use TYPE instead of LIKE (where ever possible). It is memory efficient.
3.)
The fundamental differance between the type and like is
One is used as data type ( TYPE) the other one as
data object ( DATA )
Data type will not hold memory where as the data object will occupy the memory.
Best practise is to define the data type and use the data object when exactly required
4.)Type - Reference to a Data type / SE11 Field-Fieldname.
Like - Obselete statement its like saying i want just like this , so inherit all the property of that variable.
5.)Please refer to the following link
http://help.sap.com/search/highlightContent.jsp
6.) In the following two declarations, there is a difference.
1)
types:
var1 type mara-matnr. ( type always refer to the SE11)
I mean the Mara should be created in SE11 tcode.
data :
var2 type var1.
*************************************
2)
types :
var3 like mara-matnr. ( here it is not necessary that it should be an object created in SE11 )
data :
var4 type var3.
for eg:
data: g_matnr type mara-matnr.
types: var3 like g_matnr. - no error
types: var2 type g_matnr. - error will come.
as g_matnr is not declared in Se11.
POpulating internal tables :-
1.)Append data line by line
Syntax
APPEND [<wa> TO / INITIAL LINE TO] <itab>.
- Appends new line to internal table <itab>.
- <wa> is source area which is appended.
- INITIAL LINE TO adds a line initialized with the correct value for its type to the table.
Note: sy-tabix contains the index of the appended line.
2.)Using COLLECT statement
Syntax
COLLECT [<wa> INTO] <itab>.
Note:
Use COLLECT statement for tables having unique standard keys.
Incase of tables with header line omit INTO option.
If an entry with same standard key exists new line is not appended but adds the numeric fields to existing contents.
Else collect acts in a similar way as append.
Sy-tabix contains the index of the processed line.
3.)Using INSERT statement
Syntax
INSERT [<wa> INTO / INITIAL LINE INTO] <itab> [index <idx>].
Note:
New entry has index <idx> and following line has index <idx>+1.
If less than <idx>-1 entries then cannot insert line and sy-subrc = 4.
4.)To append part or all of an internal table
Syntax
APPEND LINES OF <itab1> [FROM <n1>] [TO <n2>] TO <itab2>.
Note:
Without the FROM and TO options, this statement appends the entire table <itab1> to <itab2>.
5.)To copy entire contents of one table into another in one execution
Syntax
MOVE <itab1> To <itab2>.
OR
<itab1> = <itab2>.
hope u understand....
Thanxs and regards.
sachin sharma
njoy day!!!!
Edited by: sachin sharma on Oct 17, 2008 7:00 AM
10-17-2008 6:05 AM
Hi Jonathan,
Welcome to SDN. This is a very simple question. Please search in SDN with the search patterns as 'TYPE' or 'LIKE' you will have number of threads. Or else you can go through a wonderful [help|http://help.sap.com/saphelp_nw70/helpdata/en/fc/eb2ff3358411d1829f0000e829fbfe/frameset.htm] which is provided by SAP on this.
Regards,
Swapna.