user919789
user919789

Reputation: 171

Save internal table to directory as CSV file?

I'm trying to save a SAP internal table to SAP Directory as .CSV file. I've learned that I should convert the contents to character type but i get an illegal casting error. This is my code:

 FORM f_opendata TABLES p_tab TYPE STANDARD TABLE USING
                   p_work TYPE any.


FIELD-SYMBOLS: <lfs_wa>.


 CONCATENATE c_headerfile c_initials sy-datum c_ext INTO v_dtasetfile.

OPEN DATASET v_dtasetfile FOR OUTPUT IN TEXT MODE ENCODING UTF-8.

IF sy-subrc EQ 0.

LOOP AT p_tab INTO p_work.
  ASSIGN p_work TO <lfs_wa> CASTING TYPE c.
  TRANSFER <lfs_wa> TO v_dtasetfile.
ENDLOOP.
CLOSE DATASET v_dtasetfile.
MESSAGE i899(f2) WITH 'SUCCESS' v_dtasetfile.

ENDIF.
ENDFORM.   

This is the error:

An exception occurred that is explained in detail below. The exception, which is assigned to class CX_SY_ASSIGN_CAST_ILLEGAL_CAST, was not caught in procedure F_OPENDATA, nor was it propagated by a RAISING clause. Since the caller of the procedure could not have anticipated that the exception would occur, the current program is terminated. The reason for the exception is:

The error occurred at a statement of the form ASSIGN f TO CASTING.
ASSIGN f TO CASTING TYPE t.
or
ASSIGN f TO CASTING LIKE f1.
or
at table statements with the addition
ASSIGNING CASTING.

The following error causes are possible:
1. The type of field f or the target type determined by , t or f1 contains data references, object references, strings or internal tables as components.
These components must match exactly as far as their position (offset) and their type are concerned.
2. The specified type t or the type of f1 are not suited for the type of the field symbol .
3. The row type of the related table is not suited for the target type specified by according to the rules described in 1).

Upvotes: 1

Views: 29640

Answers (2)

user919789
user919789

Reputation: 171

I used a function to covert to text format before I transfered data to the dataset:

CALL FUNCTION 'SAP_CONVERT_TO_TEX_FORMAT'
EXPORTING
  I_FIELD_SEPERATOR          = c_colon
TABLES
  I_TAB_SAP_DATA             = ig_final
CHANGING
 I_TAB_CONVERTED_DATA       = ig_text
EXCEPTIONS
 CONVERSION_FAILED          = 1
 OTHERS                     = 2
      .
IF SY-SUBRC <> 0.
  MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
     WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.



OPEN DATASET v_g_dtasetfile FOR OUTPUT IN TEXT MODE ENCODING UTF-8.

IF sy-subrc EQ 0.

LOOP AT ig_text ASSIGNING <fs_l_wa>.
  TRANSFER <fs_l_wa> TO v_g_dtasetfile.
ENDLOOP.
CLOSE DATASET v_g_dtasetfile.

MESSAGE i899(f2) WITH text-020 v_g_dtasetfile text-021.

ENDIF.

Upvotes: 1

vwegert
vwegert

Reputation: 18483

Converting isn't the same thing as casting. If your table contains fields that are not CLIKE, the system will refuse to cast the entire line. You have two options at this point:

  • ensure that your output table only contains character-like fields or
  • walk through each field of each line (you can use RTTI to do this) and convert the contents as required

Upvotes: 1

Related Questions