abap--关于集(set)的读取(如读取成本中心组下的所有成本中心)

本文介绍SAP系统中如何使用集(Set)来管理层次结构数据,包括各种类型的组如成本中心组、利润中心组等,并提供了相关TCODE、数据表及ABAP示例代码。
部署运行你感兴趣的模型镜像

在sap中很多地方使用了集(set)来存储层次关系的数据,如:
Cost Center Group
Cost Element Group
Order Group
Statistical Key Figure Group
Activity Type Group
Profit Center Group
Business Process Group
Cost Object Group
Account Group
WBS Element Group
Fund Group
Functional Area Group
Grant Group
Business Entities Set
Real Estate Set
Buildings Set
Rental Units Set
Rental Agreements Set
Management Contracts Set
Settlement Units Set
General Contracts Set
... 等等....,具体参见SETCLS表.

相关TCODE
GS01
GS02
GS03
GS04

相关数据表
setcls:Set Classes(SETCLASS(4))
SETCLST: Set Class Descriptions(LANGU(2),SETCLASS(4))
setheader:Set Header and Directory(SETCLASS(4),SUBCLASS(10),SETNAME(24))
setnode:Lower-level sets in sets((SETCLASS(4),SUBCLASS(10),SETNAME(24),LINEID(10))
setleaf:Values in Sets(SETCLASS(4),SUBCLASS(10),SETNAME(24),LINEID(10))

相关函数
'G_SET_GET_ALL_VALUES'
'G_SET_FETCH'

代码样例
该样例代码是提取某个成本中心组下的成本中心到range: s_kostl
*&---------------------------------------------------------------------*
*& Form GET_TREE
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
form get_kostl.
data: l_count type i.
data: begin of i_tree occurs 0,
setname like setnode-setname,
end of i_tree.
data: i_subtree like i_tree occurs 0 with header line,
i_temptree like i_tree occurs 0 with header line.
select setname into i_tree
from setheader
where setclass = '0101' and subclass = '1000'
and setname in s_cengrp.
read table i_setheader with key setname = i_tree-setname.
if sy-subrc = 0.
append i_tree to i_tree.
endif.
endselect.
i_subtree[] = i_tree[].
l_count = 1.
while l_count > 0.
read table i_subtree index 1.
delete i_subtree index 1.
select subsetname as setname
into table i_temptree
from setnode
where setclass = '0101'
and subclass = '1000' and subsetcls = '0101'
and subsetscls = '1000' and setname = i_subtree-setname.
append lines of i_temptree to i_tree.
append lines of i_temptree to i_subtree.
describe table i_subtree lines l_count.
endwhile.
select valsign as sign valoption as option
valfrom as low valto as high
into table s_kostl
from setleaf
for all entries in i_tree
where setclass = '0101'
and subclass = '1000' and setname = i_tree-setname.
endform. " GET_KOSTL
代码二
DATA: t_set_values TYPE TABLE OF rgsb4.

CALL FUNCTION 'G_SET_GET_ALL_VALUES'
EXPORTING
client = sy-mandt
setnr = 'NP_SLOC_SERV'
table = 'MARD'
class = '0000'
fieldname = 'LGORT'
TABLES
set_values = t_set_values
EXCEPTIONS
set_not_found = 1
OTHERS = 2.
代码三

REPORT YTEST_GRP .

* -- Constant for the Set Class
CONSTANTS: c_cls_0106 TYPE rgsbs-class VALUE '0106'.

DATA: l_wa_set_lines_basic TYPE rgsbv,
l_wa_hdr TYPE rgsbs,
l_i_for_lin TYPE STANDARD TABLE OF rgsbf,
l_i_set_lines_basic TYPE STANDARD TABLE OF rgsbv,
l_i_set_lines_data TYPE STANDARD TABLE OF rgsb3,
l_i_set_lines_multi TYPE STANDARD TABLE OF rgsb2,
l_i_set_lines_single TYPE STANDARD TABLE OF rgsb1.

DATA: l_v_setnr TYPE char50.

PARAMETERS: p_PCGRP type PCGRP, " Profit Centre Group
p_kokrs type kokrs. " Controlling Area

CONCATENATE p_kokrs p_pcgrp INTO l_v_setnr.

*-- Get the Set description
CALL FUNCTION 'G_SET_FETCH'
EXPORTING
class = c_cls_0106
setnr = l_v_setnr
IMPORTING
set_header = l_wa_hdr
TABLES
formula_lines = l_i_for_lin
set_lines_basic = l_i_set_lines_basic
set_lines_data = l_i_set_lines_data
set_lines_multi = l_i_set_lines_multi
set_lines_single = l_i_set_lines_single
EXCEPTIONS
no_authority = 1
set_is_broken = 2
set_not_found = 3
OTHERS = 4.

*-- handle your exceptions here

loop at l_i_set_lines_basic into l_wa_set_lines_basic.

write :/ l_wa_set_lines_basic-from,
l_wa_set_lines_basic-to,
l_wa_set_lines_basic-title.
endloop.

参考:https://www.sdn.sap.com/irj/sdn/weblogs?blog=/pub/wlg/5541

您可能感兴趣的与本文相关的镜像

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值