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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.rhkb.cn/news/25770.html

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【安卓】BroadcastReceiver 动态声明为 RECEIVER_NOT_EXPORTED 后无法接收任何 Intent 的问题

一、问题起因 自 Android 14 (API 级别 34) 起&#xff0c;使用 context.registerReceiver(receiver, filter, flags) 动态注册广播接收器时&#xff0c;必须显式地声明 RECEIVER_NOT_EXPORTED 或 RECEIVER_EXPORTED 。 如果声明为 RECEIVER_EXPORTED &#xff0c;任何第三方应…

unity pico开发二:创建基本的交互

文章目录 导入UnityXR Interaction ToolKit构建基础内容 导入UnityXR Interaction ToolKit 检查一下packagemanager&#xff0c;unityxr interactionToolkit是否自动导入 我们需要升级到一个不超过3.x的版本&#xff0c;因为pico还不支持3.x的内容 然后右侧samples里导入初始…

[STM32]从零开始的STM32 DEBUG问题讲解及解决办法

一、前言 最近也是重装了一次keil&#xff0c;想着也是重装了&#xff0c;也是去官网下载了一个5.41的最新版&#xff0c;在安装和配置编译器和别的版本keil都没太大的区别&#xff0c;但是在调试时&#xff0c;遇到问题了&#xff0c;在我Debug的System Viewer窗口中没有GPIO&…

学习threejs,使用ShaderMaterial自定义着色器材质

&#x1f468;‍⚕️ 主页&#xff1a; gis分享者 &#x1f468;‍⚕️ 感谢各位大佬 点赞&#x1f44d; 收藏⭐ 留言&#x1f4dd; 加关注✅! &#x1f468;‍⚕️ 收录于专栏&#xff1a;threejs gis工程师 文章目录 一、&#x1f340;前言1.1 ☘️THREE.ShaderMaterial1.1.1…

查看ITHOR全部仿真家庭场景

1. 目标 按序号显示所有120个家庭场景统计单个场景里物体数量 2. 代码 import time from ai2thor.controller import Controller# 统计当前场景中的物体数量 def count_objects_in_scene(controller):objects controller.last_event.metadata["objects"]object_c…

ES6 特性全面解析与应用实践

1、let let 关键字用来声明变量&#xff0c;使用let 声明的变量有几个特点&#xff1a; 1) 不允许重复声明 2) 块儿级作用域 3) 不存在变量提升 4) 不影响作用域链 5) 暂时性死区 6&#xff09;不与顶级对象挂钩 在代码块内&#xff0c;使用let命令声明变量之前&#x…

VSCode轻松调试运行C#控制台程序

1.背景 我一直都是用VS来开发C#项目的&#xff0c;用的比较顺手&#xff0c;也习惯了。看其他技术文章有介绍VS Code更轻量&#xff0c;更方便。所以我专门花时间来使用VS Code&#xff0c;看看它是如何调试代码、如何运行C#控制台。这篇文章是一个记录的过程。 2.操作 2.1 V…

【多模态】Magma多模态AI Agent

1. 前言 微软杨建伟团队&#xff0c;最近在AI Agent方面动作连连&#xff0c;前两天开源了OmniParser V2&#xff0c;2月26日又开源了Magma&#xff0c;OmniParser专注在对GUI的识别解析&#xff0c;而Magma则是基于多模态技术&#xff0c;能够同时应对GUI和物理世界的交互&…

spring Boot入门

目录 Spring Boot 概述 新建Spring Boot项目 方式一&#xff1a;使用Spring Initializr创建SpringBoot项目 方式二&#xff1a;使用Maven方式构建Spring Boot项目 Spring Boot 概述 简介 •Spring Boot是基于Spring框架开发的全新框架&#xff0c;其设计目的是简化Java…

手机隐私数据彻底删除工具:回收或弃用手机前防数据恢复

软件介绍 有这样一款由吾爱网友chenwangjun 原创开发的数据处理软件&#xff0c;名为 AndroidDiskClear。它的核心功能十分强大&#xff0c;能够将你手机里已经删除的各类文件&#xff0c;像图片、普通文件、文字信息等彻底清除干净&#xff0c;有效杜绝数据恢复类软件的二次恢…

docker使用代理的简单配置

1准备代理服务器 准备代理服务器&#xff0c;例如192.168.120.168:52209 配置docker.service文件 查看service文件的位置 systemctl status docker 编辑service文件 vim /usr/lib/systemd/system/docker.service 添加代理配置 ... [Service] Environment"HTTP_PROXY…

Coze与Dify:企业级大模型应用开发认知陷阱与破局之道

前言 当前大模型应用开发似乎陷入了一种“范式陷阱”&#xff1a;当人们谈论AI Agent或智能体时&#xff0c;脑海里浮现的往往是Coze、Dify这类以对话交互为核心的低代码平台。这些工具确实降低了体验大模型的门槛&#xff0c;但也让行业陷入一种危险的认知偏差——将大模型等…

GitHub 语析 - 基于大模型的知识库与知识图谱问答平台

语析 - 基于大模型的知识库与知识图谱问答平台 GitHub 地址&#xff1a;https://github.com/xerrors/Yuxi-Know &#x1f4dd; 项目概述 语析是一个强大的问答平台&#xff0c;结合了大模型 RAG 知识库与知识图谱技术&#xff0c;基于 Llamaindex VueJS FastAPI Neo4j 构…

由浅入深系列——Distinctive Image Featuresfrom Scale-Invariant Keypoints(SIFT)

第一章&#xff1a;为什么我们需要"图像指纹"&#xff1f;——SIFT的诞生 想象一下&#xff0c;你带着一张埃菲尔铁塔的明信片来到巴黎。站在铁塔脚下&#xff0c;你举起明信片想拍张对比照——但无论怎么调整角度&#xff0c;手机APP就是识别不出两张图片的对应关系…

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核心配…

Redis缓存一致性难题:如何让数据库和缓存不“打架”?

标题&#xff1a;Redis缓存一致性难题&#xff1a;如何让数据库和缓存不“打架”&#xff1f;&#xff08;附程序员脱发指南&#xff09; 导言&#xff1a;当数据库和缓存成了“异地恋” 想象一下&#xff1a;你刚在美团下单了一份麻辣小龙虾&#xff0c;付款后刷新页面&#…

eMMC安全简介

1. 引言 术语“信息安全”涵盖多种不同的设计特性。一般而言&#xff0c; 信息安全是指通过实践防止信息遭受未经授权的访问、使用、披露、中断、篡改、检查、记录或销毁。 信息安全的三大核心目标为 机密性&#xff08;Confidentiality&#xff09;、完整性&#xff08;Integr…

如何用python画一棵分形树

这个代码会生成一个彩色的分形树图案&#xff0c;可以通过调整draw_tree函数中的参数来改变树的形状和大小 import turtle import random# 递归函数绘制分形树 def draw_tree(branch_len, t):if branch_len > 5:t.color(random.choice(colors))t.pensize(branch_len / 10)t…

linux中安装部署Jenkins,成功构建springboot项目详细教程

参考别人配置Jenkins的git地址为https&#xff0c;无法连上github拉取项目&#xff0c;所以本章节介绍通过配置SSH地址来连github拉取项目 目录&#xff1a; 1、springboot项目 1.1 创建名为springcloudproject的springboot项目工程 1.2 已将工程上传到github中&#xff0c;g…

sqli-lab

没了解sql注入的知识&#xff0c;本篇更像是复现 数据库层面 │ ├── 数据库1&#xff08;Database 1&#xff09; │ │ ├── 表1&#xff08;Table 1&#xff09; │ │ │ ├── 主键字段&#xff08;Primary Key&#xff09;&#xff1a;唯一标识表中的每…