打开SAP 客户端工具
ABAP 中 创建包(SE80)
创建函数组
展开ABAP 工作台,双击ABAP Dictionary 字典:
选择第三个data type,输入数据结构名称ZSQL_CLAUSE_ELEMENTS,点击创建:
选中Structure结构,点击确定:
输入简称,增加一个数据元素TEXT,类型为SO_TEXT
同样的方法,我们创建Structure结构ZTABLEROWS,增加一条数据元素,名字任意如ZTABLEROWS,数据元素 CHAR2000
保存这两个structure,然后点击上方的结构树按钮,打开Structure所在包目录
展开Structure目录,就可以看到我们定义的那两个结构了,分别右击>Activate激活
FUNCTION HHDI_EXTRACT_TABLE_DATA.
*"----------------------------------------------------------------------
*"*"Local Interface:
*" TABLES
*" FIELDS STRUCTURE ZSQL_CLAUSE_ELEMENTS
*" FROMCLAUSE STRUCTURE ZSQL_CLAUSE_ELEMENTS
*" WHERECLAUSE STRUCTURE ZSQL_CLAUSE_ELEMENTS
*" DATA STRUCTURE ZTABLEROWS
*"----------------------------------------------------------------------
*"----------------------------------------------------------------------
*" Copy selected fields from QUERY_TABLE to DATA_STRUCTURE
*"----------------------------------------------------------------------
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.
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.
ENDFUNCTION.
函数
l 新建abap function
注意函数的名称一定为:HHDI_EXTRACT_TABLE_DATA
先把HHDI_EXTRACT_TABLE_DATA的源码贴进去
点击左边的table表格tab,新增4个表,FIELDS /FROMCLAUSE /WHERECLAUSE /DATA,类型都为LIKE,后面的结构类型按照文档源码,前三个输入ZSQL_CLAUSE_ELEMENTS,最后一个输入ZTABLEROW
like报参数过时的错误,回车再回车即可。
再次check
没有错误则表示function 可以了,注意还需要设置函数可以远程访问
最后再激活一下。
在HHDI中SAP数据连接配置:
数据抽取任务的配置:
SELECT语句参考:
select KNA1~NAME2,KNVV~KUNNR,KNA1~LAND1 from KNA1 INNER JOIN KNVV ON KNA1~KUNNR = KNVV~KUNNR where KNA1~NAME2 <>'' AND KNA1~LAND1 = 'US'
注意OPENSQL不支持SELECT *
簇表数据量太大会导致SAP报缓存溢出的错误,可用WHERE语句控制其数据范围,数据量过大时,可以再HHDI的作业流程中采用循环方式取数。