当前位置: 首页 > news >正文

ABAP语言的动态程序

通过几个例子,由浅入深讲解 ABAP 动态编程。ABAP 动态编程主要通过 RTTS (Runtime Type Services) 来实现,包括 RTTI 和 RTTC:

  • 运行时类型标识(RTTI) – 提供在运行时获取数据对象的类型定义的方法。
  • 运行时类型创建(RTTC) – 提供在运行时使用任何类型定义创建数据对象的方法。

动态程序的基本要素

第一个例子主要演示构建程序的最基本要素。
第一步:创建 structure descriptor 和 table descriptor,主要用于后面动态构建内表的结构:

data struct_descr type ref to cl_abap_datadescr.
data table_descr type ref to cl_abap_tabledescr.struct_descr ?= cl_abap_structdescr=>describe_by_name( 'SFLIGHTS' ).
table_descr ?= cl_abap_tabledescr=>create( p_line_type = struct_descr ).

第二步:基于 table descriptor 创建泛型的 data reference object:

data table_dref type ref to data.
create data table_dref type handle table_descr.

第三步:使用 field symbol,将 table reference object 指向 field symbol。

field-symbols <table> type any table.
assign table_dref->* to <table>.

这样,就动态创建了一个内表 <table>。第一个完整的例子:

report  z_dynamic_table_01.data gr_alv type ref to cl_salv_table.data struct_descr type ref to cl_abap_datadescr.
data table_descr type ref to cl_abap_tabledescr.struct_descr ?= cl_abap_structdescr=>describe_by_name( 'SFLIGHTS' ).
table_descr ?= cl_abap_tabledescr=>create( p_line_type = struct_descr ).data table_dref type ref to data.
create data table_dref type handle table_descr.field-symbols <table> type any table.
assign table_dref->* to <table>.select * from sflights up to 10 rows into corresponding fields of table <table>.try.call method cl_salv_table=>factoryimportingr_salv_table = gr_alvchangingt_table      = <table>.catch cx_salv_msg .
endtry.gr_alv->display( ).

动态程序的灵活性

如果只是从 SFLIGHTS 表获取数据,当然没必要使用动态程序,因为它让程序更加复杂。我们对上面的程序进行修改,支持在选择屏幕中输入任意表名,都能够在 ALV 中显示。

report  z_dynamic_table_02.data gr_alv type ref to cl_salv_table.data struct_descr type ref to cl_abap_datadescr.
data table_descr type ref to cl_abap_tabledescr.parameters tab_name type dd02l-tabname.start-of-selection.struct_descr ?= cl_abap_structdescr=>describe_by_name( tab_name ).table_descr ?= cl_abap_tabledescr=>create( p_line_type = struct_descr ).data table_dref type ref to data.create data table_dref type handle table_descr.field-symbols <table> type any table.assign table_dref->* to <table>.select * from (tab_name) up to 10 rows into corresponding fields of table <table>.try.call method cl_salv_table=>factoryimportingr_salv_table = gr_alvchangingt_table      = <table>.catch cx_salv_msg .endtry.gr_alv->display( ).

构建动态程序所需的字段

第一个例子主要是演示动态程序的基本要素,一般情况下,我们不需要获取一个数据表的所有字段,这样就需要创建需要的字段。下面的示例演示了创建字段的方法。示例仍然从 sflights 中获取数据,然后在 ALV 中显示。只需要 CARRID, CONNID, CARRNAME 和 FLDATE 四个字段。

report  z_dynamic_table_03.data gr_alv type ref to cl_salv_table.data gs_component  type cl_abap_structdescr=>component.
data gt_component  type cl_abap_structdescr=>component_table.data struct_descr  type ref to cl_abap_datadescr.
data table_descr   type ref to cl_abap_tabledescr.data carrid type sflights-carrid.
data connid type sflights-connid.
data carrname type sflights-carrname.
data fldate type sflights-fldate.try.gs_component-name = 'CARRID'.gs_component-type ?= cl_abap_datadescr=>describe_by_data( carrid ).append gs_component to gt_component.gs_component-name = 'CONNID'.gs_component-type ?= cl_abap_datadescr=>describe_by_data( connid ).append gs_component to gt_component.gs_component-name = 'CARRNAME'.gs_component-type ?= cl_abap_datadescr=>describe_by_data( carrname ).append gs_component to gt_component.gs_component-name = 'FLDATE'.gs_component-type ?= cl_abap_datadescr=>describe_by_data( fldate ).append gs_component to gt_component.struct_descr ?= cl_abap_structdescr=>create( p_components = gt_component ).table_descr ?= cl_abap_tabledescr=>create( p_line_type = struct_descr ).data table_dref type ref to data.create data table_dref type handle table_descr.field-symbols <table> type any table.assign table_dref->* to <table>." Select data from database and fill dynamically created tableselect * from sflights up to 10 rows into corresponding fields of table <table>.try.call method cl_salv_table=>factoryimportingr_salv_table = gr_alvchangingt_table      = <table>.catch cx_salv_msg .endtry.gr_alv->display( ).catch cx_root.
endtry.

动态程序实现行转列

最后给出一个稍微综合一点的示例,对于给定的数据:

实现行转列,并在 ALV 中显示:


完整代码:

report  z_dynamic_table_rtts.type-pools: slis .types:begin of gfirst_typ,vend(6) type c,month(5) type c,amt type i,end of gfirst_typ .* RTTS declarations
data struct_descr type ref to cl_abap_datadescr.
data table_descr type ref to cl_abap_tabledescr.
data gs_component type cl_abap_structdescr=>component.
data gt_component type cl_abap_structdescr=>component_table.* Dynamice table declarations
data table_dref type ref to data.
data line_dref type ref to data.* Field symbol decalrations
field-symbols: <gfs_line>, <gfs_line1>.
field-symbols: <gfs_table> type standard table, <fld>.data it_zdemo type standard table of gfirst_typ.
data wa_zdemo like line of it_zdemo.* SALV declarionts.
data lo_cols type ref to cl_salv_columns.
data lo_salv_table type ref to cl_salv_table.
data lo_column type ref to cl_salv_column.
data col_name(30) type c.
data col_desc(20) type c.start-of-selection.* Populate the initial input table.
* Usually this input table contents will be popluated at run time,
* which raises the requirement of dynamic table.
* The talbe contens are filled here for illustration purpose.perform fill_table using:'V100' 'JAN' '100','V100' 'FEB' '250','V200' 'FEB' '200','V300' 'FEB' '150','V200' 'MAR' '250','V300' 'MAR' '300','V100' 'APR' '200','V100' 'MAY' '100','V200' 'MAY' '50','V300' 'MAY' '125','V400' 'MAY' '475'.* Write the datawrite : / 'Initial internal table'.write : /(6) 'Vendor', (12) 'Month', (3) 'Amt'.loop at it_zdemo into wa_zdemo.write : / wa_zdemo-vend, wa_zdemo-month, wa_zdemo-amt.clear wa_zdemo.endloop.* Create structure of dyanmic internal table.gs_component-name = 'VEND'.gs_component-type ?= cl_abap_datadescr=>describe_by_data( wa_zdemo-vend ).append gs_component to gt_component .* Loop throught the internal table creating a column for every distinct monthloop at it_zdemo into wa_zdemo.
* Search the component table if the month column already exists.read table gt_component into gs_component with key name = wa_zdemo-month.if sy-subrc ne 0.
* The name of the column would be the month
* and the data type would be the same as the amount filed of intenal talegs_component-name = wa_zdemo-month.gs_component-type ?= cl_abap_datadescr=>describe_by_data( wa_zdemo-amt ).append gs_component to gt_component.endif.clear: gs_component, wa_zdemo.endloop.struct_descr ?= cl_abap_structdescr=>create( p_components = gt_component ).table_descr ?= cl_abap_tabledescr=>create( p_line_type = struct_descr ).create data table_dref type handle table_descr.create data line_dref type handle struct_descr.assign table_dref->* to <gfs_table>.assign line_dref->* to <gfs_line>.* Fill vendor fieldloop at it_zdemo into wa_zdemo.read table <gfs_table> into <gfs_line> with key ('VEND') = wa_zdemo-vend.if sy-subrc ne 0.assign component 'VEND' of structure <gfs_line> to <fld>.<fld> = wa_zdemo-vend.append <fld> to <gfs_table>.endif.clear wa_zdemo.endloop.* Aggregate data.loop at it_zdemo into wa_zdemo.read table <gfs_table> assigning <gfs_line> with key ('VEND') = wa_zdemo-vend.loop at gt_component into gs_component.if gs_component-name = wa_zdemo-month.if <fld> is assigned.unassign <fld>.endif.assign component gs_component-name of structure <gfs_line> to <fld>.<fld> = <fld> + wa_zdemo-amt.endif.endloop.clear wa_zdemo.endloop.* ALV showtry.cl_salv_table=>factory(importingr_salv_table = lo_salv_tablechangingt_table = <gfs_table>).catch cx_salv_msg.endtry.* Get columns objectlo_cols = lo_salv_table->get_columns( ).* Individual column namesloop at gt_component into gs_component.try.col_name = gs_component-name.lo_column = lo_cols->get_column( col_name ).if col_name = 'VEND'.col_desc = 'Vendor'.else.concatenate col_name  '''13' into col_desc.endif.lo_column->set_medium_text( col_desc ).lo_column->set_output_length( 10 ).catch  cx_salv_not_found.endtry.endloop.* Display tablelo_salv_table->display( ).*&---------------------------------------------------------------------*
*&      Form  fill_table
*&---------------------------------------------------------------------*
form fill_table using p_fld1 type gfirst_typ-vendp_fld2 type gfirst_typ-monthp_fld3 type gfirst_typ-amt.clear wa_zdemo.wa_zdemo-vend = p_fld1.wa_zdemo-month = p_fld2.wa_zdemo-amt = p_fld3.append wa_zdemo to it_zdemo.endform.                    "fill_table

相关文章:

ABAP语言的动态程序

通过几个例子&#xff0c;由浅入深讲解 ABAP 动态编程。ABAP 动态编程主要通过 RTTS (Runtime Type Services) 来实现&#xff0c;包括 RTTI 和 RTTC: 运行时类型标识&#xff08;RTTI&#xff09; – 提供在运行时获取数据对象的类型定义的方法。运行时类型创建&#xff08;R…...

开源电商项目、物联网项目、销售系统项目和社区团购项目

以下是推荐的开源电商项目、物联网项目、销售系统项目和社区团购项目&#xff0c;均使用Java开发&#xff0c;且无需付费&#xff0c;GitHub地址如下&#xff1a; ### 开源电商项目 1. **mall** GitHub地址&#xff1a;[https://github.com/macrozheng/mall](https://git…...

Docker教程(喂饭级!)

如果你有跨平台开发的需求&#xff0c;或者对每次在新机器上部署项目感到头疼&#xff0c;那么 Docker 是你的理想选择&#xff01;Docker 通过容器化技术将应用程序与其运行环境隔离&#xff0c;实现快速部署和跨平台支持&#xff0c;极大地简化了开发和部署流程。本文详细介绍…...

HTML:自闭合标签简单介绍

1. 什么是自结束标签&#xff1f; 定义&#xff1a;自结束标签&#xff08;Self-closing Tag&#xff09;是指 不需要单独结束标签 的 HTML 标签&#xff0c;它们通过自身的语法结构闭合。语法形式&#xff1a; 在 HTML5 中&#xff1a;直接写作 <tag>&#xff0c;例如 …...

【和鲸社区获奖作品】内容平台数据分析报告

1.项目背景与目标 在社交和内容分享领域&#xff0c;某APP凭借笔记、视频等丰富的内容形式&#xff0c;逐渐吸引了大量用户。作为一个旨在提升用户互动和平台流量的分享平台&#xff0c;推荐算法成为其核心功能&#xff0c;通过精准推送内容&#xff0c;努力实现更高的点击率和…...

GitCode 助力 python-office:开启 Python 自动化办公新生态

项目仓库&#xff1a;https://gitcode.com/CoderWanFeng1/python-office 源于需求洞察&#xff0c;打造 Python 办公神器 项目作者程序员晚枫在运营拥有 14w 粉丝的 B 站账号 “Python 自动化办公社区” 时&#xff0c;敏锐察觉到非程序员群体对 Python 学习的强烈需求。在数字…...

超参数、网格搜索

一、超参数 超参数是在模型训练之前设置的&#xff0c;它们决定了训练过程的设置和模型的结构&#xff0c;因此被称为“超参数”。以KNN为例&#xff1a; 二、网格搜索 交叉验证&#xff08;Cross-Validation&#xff09;是在机器学习建立模型和验证模型参数时常用的方法&…...

or-tools编译命令自用备注

cmake .. -G "Visual Studio 17 2022" -A Win32 //vs2022 cmake .. -G "Visual Studio 15 2017" -A Win32 //vs2017 -DBUILD_DEPSON //联网下载 -DCMAKE_INSTALL_PREFIXinstall //带安装命令 -DCMAKE_CXX_FLAGS"/u…...

vulnhub靶场【kioptrix-4】靶机

前言 靶机&#xff1a;kioptrix-4&#xff0c;IP地址为192.168.1.75&#xff0c;后期IP地址为192.168.10.8 攻击&#xff1a;kali&#xff0c;IP地址为192.168.1.16&#xff0c;后期IP地址为192.168.10.6 都采用VMware虚拟机&#xff0c;网卡为桥接模式 这里的靶机&#xf…...

readline模块详解!!【Node.js】

‌“书到用时方恨少&#xff0c;事非经过不知难。”‌ —— 陆游 目录 ‌readline 是什么&#xff1f;‌基本用法&#xff1a;‌创建 Interface 类&#xff1a;核心流程‌&#xff1a; ‌Interface 类的关键事件&#xff1a;line&#xff1a;close&#xff1a;pause&#xff1a…...

软件测试的七大误区

随着软件测试对提高软件质量重要性的不断提高&#xff0c;软件测试也不断受到重视。但是&#xff0c;国内软件测试过程的不规范&#xff0c;重视开发和轻视测试的现象依旧存在。因此&#xff0c;对于软件测试的重要性、测试方法和测试过程等方面都存在很多不恰当的认识&#xf…...

【欢迎来到Git世界】Github入门

241227 241227 241227 Hello World 参考&#xff1a;Hello World - GitHub 文档. 1.创建存储库 r e p o s i t o r y repository repository&#xff08;含README.md&#xff09; 仓库名需与用户名一致。 选择公共。 选择使用Readme初始化此仓库。 2.何时用分支&#xf…...

解决 Ubuntu 24.04 虚拟机内无法ping 通 Hostname 的问题

问题背景 在 VMware 或 VirtualBox 中安装 Ubuntu 24.04 虚拟机时&#xff0c;遇到无法通过主机名&#xff08;Hostname&#xff09;进行网络通信的问题。例如&#xff0c;将虚拟机的主机名设置为 001&#xff0c;执行 ping 001 时返回 ping 0.0.0.1 并超时。此问题通常由 主机…...

给小白的oracle优化工具,了解一下

有时懒得分析或语句太长&#xff0c;可以尝试用oracle的dbms_sqldiag包进行sql优化&#xff0c; --How To Use DBMS_SQLDIAG To Diagnose Query Performance Issues (Doc ID 1386802.1) --诊断SQL 性能 SET ECHO ON SET LINESIZE 132 SET PAGESIZE 999 SET LONG 999999 SET SER…...

CT技术变迁史——CT是如何诞生的?

第一代CT(平移-旋转) X线球管为固定阳极,发射X线为直线笔形束,一个探测器,采用直线和旋转扫描相结合,即直线扫描后,旋转1次,再行直线扫描,旋转180完成一层面扫描,扫描时间3~6分钟。矩阵象素256256或320320。仅用于颅脑检查。 第二代CT (平移-旋转) 与第一代无质…...

【PHP脚本语言详解】为什么直接访问PHP文件会显示空白?从错误示例到正确执行!

前言 作为一名开发者&#xff0c;你是否曾经遇到过这样的问题&#xff1a;写了一个PHP脚本&#xff0c;放到服务器根目录后&#xff0c;直接通过file:///路径访问却显示空白页面&#xff1f;而换成http://localhost却能正常显示&#xff1f;这篇文章将带你深入理解PHP脚本语言…...

软件工程---需求工程

软件需求工程师发现、获取、组织、分析、编写和管理需求的系统方法&#xff0c;以使客户和项目组之间达成共识。 需求工程共包含五个步骤&#xff1a; 需求获取&#xff1a;对业务问题分析&#xff0c;与项目干系人沟通&#xff0c;以理解系统的目标、期望和约束&#xff0c;…...

spring注解开发(Spring整合MyBatis——Mapper代理开发模式、(Spring、MyBatis、Jdbc)配置类)(6)

目录 一、纯MyBatis独立开发程序。 &#xff08;1&#xff09;数据库与数据表。 &#xff08;2&#xff09;实体类。 &#xff08;3&#xff09;dao层接口。&#xff08;Mapper代理模式、无SQL映射文件——注解配置映射关系&#xff09; &#xff08;4&#xff09;MyBatis核心配…...

散户情绪周期模型(情绪影响操作)

目录 一、个股上涨阶段情绪演化二、个股下跌阶段情绪演化三、底部震荡阶段情绪演化四、情绪观察与操作工具箱1. 情绪自测量表&#xff08;每日收盘后记录&#xff09;2. 情绪-指标对照表 五、高阶情绪管理技巧1.认知重构训练2.生理指标监控&#xff08;需配合智能手表&#xff…...

计算机毕业设计SpringBoot+Vue.js网上商城系统(源码+文档+PPT+讲解)

温馨提示&#xff1a;文末有 CSDN 平台官方提供的学长联系方式的名片&#xff01; 温馨提示&#xff1a;文末有 CSDN 平台官方提供的学长联系方式的名片&#xff01; 温馨提示&#xff1a;文末有 CSDN 平台官方提供的学长联系方式的名片&#xff01; 作者简介&#xff1a;Java领…...

C++初阶-list的底层

目录 1.std::list实现的所有代码 2.list的简单介绍 2.1实现list的类 2.2_list_iterator的实现 2.2.1_list_iterator实现的原因和好处 2.2.2_list_iterator实现 2.3_list_node的实现 2.3.1. 避免递归的模板依赖 2.3.2. 内存布局一致性 2.3.3. 类型安全的替代方案 2.3.…...

以下是对华为 HarmonyOS NETX 5属性动画(ArkTS)文档的结构化整理,通过层级标题、表格和代码块提升可读性:

一、属性动画概述NETX 作用&#xff1a;实现组件通用属性的渐变过渡效果&#xff0c;提升用户体验。支持属性&#xff1a;width、height、backgroundColor、opacity、scale、rotate、translate等。注意事项&#xff1a; 布局类属性&#xff08;如宽高&#xff09;变化时&#…...

Python:操作 Excel 折叠

💖亲爱的技术爱好者们,热烈欢迎来到 Kant2048 的博客!我是 Thomas Kant,很开心能在CSDN上与你们相遇~💖 本博客的精华专栏: 【自动化测试】 【测试经验】 【人工智能】 【Python】 Python 操作 Excel 系列 读取单元格数据按行写入设置行高和列宽自动调整行高和列宽水平…...

【HarmonyOS 5.0】DevEco Testing:鸿蒙应用质量保障的终极武器

——全方位测试解决方案与代码实战 一、工具定位与核心能力 DevEco Testing是HarmonyOS官方推出的​​一体化测试平台​​&#xff0c;覆盖应用全生命周期测试需求&#xff0c;主要提供五大核心能力&#xff1a; ​​测试类型​​​​检测目标​​​​关键指标​​功能体验基…...

大数据零基础学习day1之环境准备和大数据初步理解

学习大数据会使用到多台Linux服务器。 一、环境准备 1、VMware 基于VMware构建Linux虚拟机 是大数据从业者或者IT从业者的必备技能之一也是成本低廉的方案 所以VMware虚拟机方案是必须要学习的。 &#xff08;1&#xff09;设置网关 打开VMware虚拟机&#xff0c;点击编辑…...

从零实现STL哈希容器:unordered_map/unordered_set封装详解

本篇文章是对C学习的STL哈希容器自主实现部分的学习分享 希望也能为你带来些帮助~ 那咱们废话不多说&#xff0c;直接开始吧&#xff01; 一、源码结构分析 1. SGISTL30实现剖析 // hash_set核心结构 template <class Value, class HashFcn, ...> class hash_set {ty…...

Android Bitmap治理全解析:从加载优化到泄漏防控的全生命周期管理

引言 Bitmap&#xff08;位图&#xff09;是Android应用内存占用的“头号杀手”。一张1080P&#xff08;1920x1080&#xff09;的图片以ARGB_8888格式加载时&#xff0c;内存占用高达8MB&#xff08;192010804字节&#xff09;。据统计&#xff0c;超过60%的应用OOM崩溃与Bitm…...

CMake控制VS2022项目文件分组

我们可以通过 CMake 控制源文件的组织结构,使它们在 VS 解决方案资源管理器中以“组”(Filter)的形式进行分类展示。 🎯 目标 通过 CMake 脚本将 .cpp、.h 等源文件分组显示在 Visual Studio 2022 的解决方案资源管理器中。 ✅ 支持的方法汇总(共4种) 方法描述是否推荐…...

HashMap中的put方法执行流程(流程图)

1 put操作整体流程 HashMap 的 put 操作是其最核心的功能之一。在 JDK 1.8 及以后版本中&#xff0c;其主要逻辑封装在 putVal 这个内部方法中。整个过程大致如下&#xff1a; 初始判断与哈希计算&#xff1a; 首先&#xff0c;putVal 方法会检查当前的 table&#xff08;也就…...

蓝桥杯 冶炼金属

原题目链接 &#x1f527; 冶炼金属转换率推测题解 &#x1f4dc; 原题描述 小蓝有一个神奇的炉子用于将普通金属 O O O 冶炼成为一种特殊金属 X X X。这个炉子有一个属性叫转换率 V V V&#xff0c;是一个正整数&#xff0c;表示每 V V V 个普通金属 O O O 可以冶炼出 …...