SAP macros creation

本文介绍了ABAP中宏的定义方法及注意事项,包括宏的有效范围、如何在程序中插入宏,以及宏与数据库表TRMAC的关系。文章还提供了一个绘制边框的宏示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Syntax Diagram

DEFINE

 

Syntax

DEFINE macro.
  ... &1 ... &9 ...
END-OF-DEFINITION.

Effect

The statement DEFINE defines a macro. Naming conventions apply to the name macro and you cannot use ABAP words. Between the statements DEFINE and END-OF-DEFINITION, you can use any number of complete ABAP statements, with the exception of DEFINE, END-OF-DEFINITION, and program-initiating statements. These statements constitute a section of source code that can be inserted into a program under the name macro. The definition of a macro is not restricted to the limits of processing blocks.

The validity of a macro is defined by its position in the source code. It can be inserted at any position after END-OF-DEFINITION. If another macro is defined with the same name, it overwrites the previous macro from its new position.

Within a macro, you can use up to nine placeholders &1 ... &9 instead of ABAP words and operands. These placeholders must be replaced by fixed words when the macro is inserted.

As well as in the source code of a program, you can also store macros in the database table TRMAC, where they can be used by any program. The system first searches in the current program for a macro, and then in the table TRMAC. Do not define your own macros in TRMAC. One example of a macro in TRMAC is break, which sets a breakpoint in the system field sy-uname, depending on the current user name.
  • In macros, you cannot set any breakpoints. In the ABAP Debugger, the statements of a macro cannot be performed in individual steps.
  • Macros must not include more than a few lines, and should be used sparingly, since it is very difficult to analyze macro errors. Instead of macros, use internal procedures or include programs.


Inserting Macros

macro [p1 p2 ... ].

If a previously defined macro is listed as the first word in an ABAP statement instead of a valid ABAP keyword, then these statements are inserted in the source code at this position. Appropriate ABAP words or operands p1 p2 ... must be specified for all placeholders of the macro. p1 p2 ... replace the placeholders literally, one after the other.

Note

A macro can insert other macros, but not itself.

Example

In this example, a macro write_frame, which draws a frame around a placeholder &1 in a list, is defined and then implemented.

DATA: x TYPE i, y TYPE i, l TYPE i.
DEFINE write_frame.
  x = sy-colno. y = sy-linno.
  WRITE: '|' NO-GAP, &1 NO-GAP, '|' NO-GAP.
  l = sy-colno - x.
  y = y - 1. SKIP TO LINE y. POSITION x.
  ULINE AT x(l).
  y = y + 2. SKIP TO LINE y. POSITION x.
  ULINE AT x(l).
  y = y - 1. x = sy-colno. SKIP TO LINE y. POSITION x.
END-OF-DEFINITION.
SKIP.
write_frame 'In a frame!'.






 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值