09-08-2010 10:42 AM
Dear ABAPers,
Did SAP create char structure for each table in data dictionary. For example char structure for table MARA, they have fields:
MATNR CHAR 18
ERSDA CHAR 8
ERNAM CHAR 12
LAEDA CHAR 8
AENAM CHAR 12
VPSTA CHAR 15
PSTAT_D CHAR 15
LVOMA CHAR 1
MTART CHAR 4
MBRSH CHAR 1
MATKL CHAR 9
BISMT CHAR 18
MEINS CHAR 3
BSTME CHAR 3
...
I need to generate internal table like table MARA but have all fields in char type. The purpose is to pass this internal table to unix file using syntax 'open dataset' 'transfer' 'close dataset'.
Thanks
Regards
Hadi
09-08-2010 10:56 AM
Hi ,
try this way..
see the semple WIKI http://wiki.sdn.sap.com/wiki/display/ABAP/DynamicInternaltable
Create a Dynamic Internal table with Hard code the Field type as Char with mara fields.
ref_table_des ?= cl_abap_typedescr=>describe_by_name( MARA).
idetails[] = ref_table_des->components[].
LOOP AT idetails INTO xdetails.
CLEAR xfc.
xfc-fieldname = xdetails-name .
xfc-datatype = 'CHAR'. "pass this always
xfc-inttype = xdetails-type_kind.
xfc-intlen = xdetails-length / 2.
SHIFT xfc-intlen LEFT DELETING LEADING '0' .
xfc-decimals = xdetails-decimals.
APPEND xfc TO jfc.
ENDLOOP.
* Create dynamic internal table and assign to FS
CALL METHOD cl_alv_table_create=>create_dynamic_table
EXPORTING
it_fieldcatalog = jfc
IMPORTING
ep_table = dy_table.
ASSIGN dy_table->* TO <dyn_table>.
* Create dynamic work area and assign to FS
CREATE DATA dy_line LIKE LINE OF <dyn_table>.
ASSIGN dy_line->* TO <dyn_wa>.
Prabhudas
09-08-2010 11:57 AM
Thanks a lot Prabhu,
The solution is awesome,,,,
Regards
Hadi
09-13-2010 4:46 AM
Hi Prabhu and all
I tried the code and find that each data type has internal length which is taken in attribute ref_table_des->components[] and it's not the same length as we can see in SE16.
For example data with type 'c' will has internal length 2 * length in SE16. That is the reason why you divide internal length by 2 in example below, to make it back into lenth as stated in SE16
LOOP AT idetails INTO xdetails.
CLEAR xfc.
xfc-fieldname = xdetails-name .
xfc-datatype = 'CHAR'. "pass this always
xfc-inttype = xdetails-type_kind.
xfc-intlen = xdetails-length / 2.
SHIFT xfc-intlen LEFT DELETING LEADING '0' .
xfc-decimals = xdetails-decimals.
APPEND xfc TO jfc.
ENDLOOP.
I also noted that data type 'p' will have internal length = (n + 1) / 2. n = length in SE16.
Also data type 'f' will have internal length = n / 2.
Then I modified the code as follow:
LOOP AT idetails INTO xdetails.
CLEAR xfc.
xfc-fieldname = xdetails-name .
xfc-datatype = 'CHAR'. "pass this always
xfc-inttype = xdetails-type_kind.
if wa_zdetails-type_kind = 'P'.
*wa_zlfc-intlen = ( wa_zdetails-length * 2 ) - 1.*
elseif wa_zdetails-type_kind = 'F'.
*wa_zlfc-intlen = wa_zdetails-length * 2.*
else.
wa_zlfc-intlen = wa_zdetails-length / 2.
endif.
SHIFT xfc-intlen LEFT DELETING LEADING '0' .
xfc-decimals = xdetails-decimals.
APPEND xfc TO jfc.
ENDLOOP.
I am afraid that I will miss some other data type that I need to e handle. Kindly need your advice about it.
Thanks
Regards
Hadi