2020 Jul 08 9:19 AM
Someone give me a RFC for SQL query.
TYPE-POOLS: abap.
DATA:
columnName TYPE SO_TEXT,
fieldDataDescrRef TYPE REF TO abap_componentdescr,
numberFields TYPE i,
fieldDescr TYPE abap_componentdescr,
fieldname TYPE string,
fieldDescrTab TYPE abap_component_tab,
rowStructDescr TYPE REF TO cl_abap_structdescr,
rowReference TYPE REF TO data,
returnRowString TYPE string,
dataFieldString TYPE string,
dataline LIKE data,
fromClauseRow TYPE ZSQL_CLAUSE_ELEMENTS,
fromClauseString TYPE string,
whereClauseRow TYPE ZSQL_CLAUSE_ELEMENTS,
whereClauseString TYPE string,
fieldsRow TYPE ZSQL_CLAUSE_ELEMENTS,
FNAME(600) VALUE 'myfile'.
FIELD-SYMBOLS:
<datarow> TYPE ANY,
<datafield> TYPE ANY.
* CREATE DataStructure with field names
* Datatypes are read from fieldnames of FIELDS input table
DESCRIBE TABLE FIELDS LINES numberFields.
LOOP AT FIELDS INTO fieldsRow.
fieldname = SY-TABIX.
* names need to be unique and must start with a char
CONCATENATE 'string' fieldname INTO fieldname.
CONDENSE fieldname.
fieldDescr-name = fieldname.
* for dictionary lookup we need to change columnnames from Open SQL
* to dictionary notation
columnName = fieldsRow-TEXT.
REPLACE FIRST OCCURRENCE OF SUBSTRING '~' IN columnName WITH '-' RESPECTING CASE.
fieldDescr-type ?= cl_abap_typedescr=>describe_by_name( columnName ).
APPEND fieldDescr TO fieldDescrTab.
ENDLOOP.
rowStructDescr = cl_abap_structdescr=>create( fieldDescrTab ).
* now we create the actual data structure in memory
create data rowReference type HANDLE rowStructDescr.
* finally we assign it to the Field-symbol used by the select statement
ASSIGN rowReference->* TO <datarow>.
* End Create DataStructure
* to simplify calls we concatenate from and whereclause into strings
* this way caller doesn't need to check word wrappings
fromClauseString = ''.
LOOP AT FROMCLAUSE INTO fromClauseRow.
CONCATENATE fromClauseString fromClauseRow-TEXT INTO fromClauseString.
ENDLOOP.
whereClauseString = ''.
LOOP AT WHERECLAUSE INTO whereClauseRow.
CONCATENATE whereClauseString whereClauseRow-TEXT INTO whereClauseString.
ENDLOOP.
* Now start actual select operation
SELECT (FIELDS) FROM (fromClauseString) INTO <datarow> WHERE (whereClauseString).
* we read all fields of the current row, cast it to string and
* concatenate it into a dataline with division chars.
CLEAR: returnRowString.
DO numberFields TIMES.
ASSIGN component sy-index of structure <datarow> to <datafield>.
dataFieldString = <datafield>.
CONCATENATE returnRowString '^' datafieldstring INTO returnRowString.
ENDDO.
dataline = returnRowString.
* finally dataline is added to the return table.
INSERT dataline INTO TABLE data.
ENDSELECT.
open DATASET FNAME FOR APPENDING IN TEXT MODE ENCODING DEFAULT.
TRANSFER whereClauseString TO FNAME.
IF SY-SUBRC <> 0.
WRITE: 'SY-SUBRC:', SY-SUBRC,
/ 'System Message:'.
ENDIF.
CLOSE DATASET FNAME.
and error occurred in the line below at the FIELDS loop sometimes.
fieldDescr-type ?= cl_abap_typedescr=>describe_by_name( columnName ).
FIELDS is an internal table with line type char(72). I tried with two test case, case 1 FIELDS as 'cosp_bak~objnr' and it's OK. When I test with 'cskt~bukrs' this line has error. I debug to this method and below line cause the error.
case_kind_all admin_tab_line-kind admin_tab_line-xtype.
when others.
* all others are erros
raise type_not_found.
endcase.
I check it and the data admin_tab_line is from this line.
* get administration information
system-call describe administration
mode 'N' of p_name into admin_tab_line-xtype crc admin_tab_line-kind.
Can some one tell me what's this? and I checked when their is nor error the admin_tab_line-xtype is 'E' and when the value is 'X' There is error. Thx.
2020 Jul 08 9:51 AM
CSKT-BUKRS simply doesn't exist in SAP Dictionary.
You must handle the exception:
CALL METHOD cl_abap_typedescr=>describe_by_name(
EXPORTING
p_name = columnName
RECEIVING
p_descr_ref = DATA(p_descr_ref)
EXCEPTIONS
type_not_found = 1 ).
IF sy-subrc <> 0.
" Handle the invalid column name
ELSE.
fieldDescr-type ?= p_descr_ref.
...
ENDIF.
2020 Jul 08 9:51 AM
CSKT-BUKRS simply doesn't exist in SAP Dictionary.
You must handle the exception:
CALL METHOD cl_abap_typedescr=>describe_by_name(
EXPORTING
p_name = columnName
RECEIVING
p_descr_ref = DATA(p_descr_ref)
EXCEPTIONS
type_not_found = 1 ).
IF sy-subrc <> 0.
" Handle the invalid column name
ELSE.
fieldDescr-type ?= p_descr_ref.
...
ENDIF.
2020 Jul 08 10:17 AM
REPLACE FIRST OCCURRENCE OF SUBSTRING '~' IN columnName WITH '-' RESPECTING CASE.
This line replace the tilde with dash. But even with upper case 'CSKT-BUKRS', It still not working. This table and fields do exist.
2020 Jul 08 10:23 AM
You're right. In fact I made my answer partly on your written explanation where you talk about 'cosp_bak~objnr' and 'cskt~bukrs'.
I have edited the first part of my answer:
CSKT-BUKRS simply doesn't exist in SAP Dictionary.