查 SAP 表数据的接口
1.使用范例:
字段 | 注释 |
---|---|
QUERY_TABLE | 查询的表名 |
FIELDNAME | 查询的字段 |
ROWCOUNT | 查询的行数 |
ROWCOUNT | 查询的行数 |
OPTIONS | 查询条件 |
FIELDS | 查询字段的释义和字符长度 |
DATA | 查询的数据 |
TOTALROWS | 符合条件数据的行数 |
FIELDS 结果:
外围系统接口调用此接口,需要通过 FIELDS 的长度去划分 DATA 的字段内容。
2.传参内容:
3.详细源码:
FUNCTION zfm_query_table.
*"----------------------------------------------------------------------
*"*"本地接口:
*" IMPORTING
*" VALUE(QUERY_TABLE) TYPE DD02L-TABNAME
*" VALUE(FIELDNAME) TYPE BAPI_MSG
*" VALUE(DELIMITER) TYPE SONV-FLAG DEFAULT SPACE
*" VALUE(NO_DATA) TYPE SONV-FLAG DEFAULT SPACE
*" VALUE(ROWSKIPS) TYPE SOID-ACCNT DEFAULT 0
*" VALUE(ROWCOUNT) TYPE SOID-ACCNT DEFAULT 0
*" VALUE(OPTIONS) TYPE CHAR300 OPTIONAL
*" EXPORTING
*" VALUE(TOTALROWS) TYPE SOID-ACCNT
*" TABLES
*" FIELDS STRUCTURE RFC_DB_FLD
*" DATA STRUCTURE CHAR8000
*" EXCEPTIONS
*" TABLE_NOT_AVAILABLE
*" TABLE_WITHOUT_DATA
*" OPTION_NOT_VALID
*" FIELD_NOT_VALID
*" NOT_AUTHORIZED
*" DATA_BUFFER_EXCEEDED
*"----------------------------------------------------------------------
CALL FUNCTION 'VIEW_AUTHORITY_CHECK'
EXPORTING
view_action = 'S'
view_name = query_table
EXCEPTIONS
no_authority = 2
no_clientindependent_authority = 2
no_linedependent_authority = 2
OTHERS = 1.
IF sy-subrc = 2.
RAISE not_authorized.
ELSEIF sy-subrc = 1.
RAISE table_not_available.
ENDIF.
* ----------------------------------------------------------------------
* find out about the structure of QUERY_TABLE
* ----------------------------------------------------------------------
DATA BEGIN OF table_structure OCCURS 10.
INCLUDE STRUCTURE dfies.
DATA END OF table_structure.
"DATA TABLE_HEADER LIKE X030L.
DATA table_type TYPE dd02v-tabclass.
CALL FUNCTION 'DDIF_FIELDINFO_GET'
EXPORTING
tabname = query_table
* FIELDNAME = ' '
* LANGU = SY-LANGU
* LFIELDNAME = ' '
* ALL_TYPES = ' '
* GROUP_NAMES = ' '
IMPORTING
* X030L_WA =
ddobjtype = table_type
* DFIES_WA =
* LINES_DESCR =
TABLES
dfies_tab = table_structure
* FIXED_VALUES =
EXCEPTIONS
not_found = 1
internal_error = 2
OTHERS = 3.
IF sy-subrc <> 0.
RAISE table_not_available.
ENDIF.
IF table_type = 'INTTAB'.
RAISE table_without_data.
ENDIF.
* ----------------------------------------------------------------------
* isolate first field of DATA as output field
* (i.e. allow for changes to structure DATA!)
* ----------------------------------------------------------------------
DATA line_length TYPE i.
FIELD-SYMBOLS <d>.
ASSIGN COMPONENT 0 OF STRUCTURE data TO <d>.
* If this line leads to a syntax error
* please just delete the 'in character mode'
DESCRIBE FIELD <d> LENGTH line_length IN CHARACTER MODE.
* ----------------------------------------------------------------------
* if FIELDS are not specified, read all available fields
* ----------------------------------------------------------------------
DATA number_of_fields TYPE i.
DESCRIBE TABLE fields LINES number_of_fields.
IF number_of_fields = 0.
LOOP AT table_structure.
MOVE table_structure-fieldname TO fields-fieldname.
APPEND fields.
ENDLOOP.
ENDIF.
* ----------------------------------------------------------------------
* for each field which has to be read, copy structure information
* into tables FIELDS_INT (internal use) and FIELDS (output)
* ----------------------------------------------------------------------
DATA: BEGIN OF fields_int OCCURS 10,
fieldname LIKE table_structure-fieldname,
type LIKE table_structure-inttype,
decimals LIKE table_structure-decimals,
length_src LIKE table_structure-intlen,
length_dst LIKE table_structure-leng,
offset_src LIKE table_structure-offset,
offset_dst LIKE table_structure-offset,
END OF fields_int,
line_cursor TYPE i.
*--------insert by Steve at23.05.2017 22:09:58 Begin-------*
DATA: BEGIN OF it_fldnm OCCURS 0,
fieldname(500) TYPE c,
END OF it_fldnm.
SPLIT fieldname AT ',' INTO TABLE it_fldnm.
*--------insert by Steve at23.05.2017 22:09:58 End----------*
line_cursor = 0.
* for each field which has to be read ...
LOOP AT fields.
*--------insert by Steve at23.05.2017 22:10:17 Begin-------*
READ TABLE it_fldnm WITH KEY fieldname = fields-fieldname.
IF sy-subrc NE 0.
DELETE fields.
CONTINUE.
ENDIF.
*--------insert by Steve at23.05.2017 22:10:17 End----------*
READ TABLE table_structure WITH KEY fieldname = fields-fieldname.
IF sy-subrc NE 0.
RAISE field_not_valid.
ENDIF.
* compute the place for field contents in DATA rows:
* if not first field in row, allow space for delimiter
IF line_cursor <> 0.
IF no_data EQ space AND delimiter NE space.
MOVE delimiter TO data+line_cursor.
ENDIF.
line_cursor = line_cursor + strlen( delimiter ).
ENDIF.
* ... copy structure information into tables FIELDS_INT
* (which is used internally during SELECT) ...
fields_int-fieldname = table_structure-fieldname.
fields_int-length_src = table_structure-intlen.
* FIELDS_INT-LENGTH_DST = TABLE_STRUCTURE-LENG.
* modified by Theobald, 2007-11-20
fields_int-length_dst = table_structure-outputlen.
fields_int-offset_src = table_structure-offset.
fields_int-offset_dst = line_cursor.
fields_int-type = table_structure-inttype.
fields_int-decimals = table_structure-decimals.
* compute the place for contents of next field in DATA rows
* LINE_CURSOR = LINE_CURSOR + TABLE_STRUCTURE-LENG.
* modified by Theobald, 2007-11-20
line_cursor = line_cursor + table_structure-outputlen.
IF line_cursor > line_length AND no_data EQ space.
RAISE data_buffer_exceeded.
ENDIF.
APPEND fields_int.
* ... and into table FIELDS (which is output to the caller)
fields-fieldtext = table_structure-fieldtext.
fields-type = table_structure-inttype.
fields-length = fields_int-length_dst.
fields-offset = fields_int-offset_dst.
MODIFY fields.
ENDLOOP.
* end of loop at FIELDS
* ----------------------------------------------------------------------
* read data from the database and copy relevant portions into DATA
* ----------------------------------------------------------------------
* output data only if NO_DATA equals space (otherwise the structure
* information in FIELDS is the only result of the module)
IF no_data EQ space.
DATA: BEGIN OF work, buffer(30000), f TYPE f, END OF work.
DATA:lv_rowcount TYPE i.
FIELD-SYMBOLS: <wa> TYPE any, <comp> TYPE any.
ASSIGN work TO <wa> CASTING TYPE (query_table).
IF rowcount > 0.
rowcount = rowcount + rowskips.
lv_rowcount = rowcount + 1.
ENDIF.
SELECT * FROM (query_table) INTO <wa> WHERE (options).
IF sy-dbcnt GT rowskips.
IF rowcount > 0 AND sy-dbcnt GE lv_rowcount.
CONTINUE.
ENDIF.
* copy all relevant fields into DATA (output) table
LOOP AT fields_int.
IF fields_int-type = 'P'.
ASSIGN COMPONENT fields_int-fieldname
OF STRUCTURE <wa> TO <comp>
TYPE fields_int-type
DECIMALS fields_int-decimals.
ELSE.
ASSIGN COMPONENT fields_int-fieldname
OF STRUCTURE <wa> TO <comp>
TYPE fields_int-type.
ENDIF.
MOVE <comp> TO
<d>+fields_int-offset_dst(fields_int-length_dst).
ENDLOOP.
* end of loop at FIELDS_INT
APPEND data.
ENDIF.
ENDSELECT.
totalrows = sy-dbcnt.
ENDIF.
ENDFUNCTION.