ABAP:Message的使用

本文围绕SAP系统中的MESSAGE展开,介绍了其多种变体形式,如MESSAGE xnnn、MESSAGE ID id TYPE mtype NUMBER n等,阐述了不同变体的作用、效果及使用示例。还提及了消息类型在不同上下文中的行为,以及各变体的附加项功能,最后给出了Message使用心得。

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

在SAP系统中,Message是提供给用户程序运行信息的一个重要手段。以下是SAP系统中的帮助。

 

MESSAGE

Variants:

1. MESSAGE xnnn.

2. MESSAGE ID id TYPE mtype NUMBER n.

3. MESSAGE xnnn(mid).

4. MESSAGE msg TYPE mtype.

Effect

Sends a message. Messages are stored in the table T100, are processed using transaction SE91 and can be created by forward navigation.

The ABAP runtime environment handles messages according to the type declared in the MESSAGE statement and the context in which the message was sent. The following message types exist:

A - Abend
: Transaction terminated
E - Error
: Error message
I - Info
: Information
S - Status
: Status message
W - Warning
: Correction possible
X - Exit
: Transaction terminated with short dump

Messages are mainly used to handle user input on screens. The following table shows the behavior of each message type in each context. An explanation of the numbers used is included at the end of the table:

                            A     E     I     S     W     X
--------------------------------------------------------------
PAI Module                   1     2     3     4     5     6
PAI Module for POH            1     7     3     4     7

    6
PAI Module for POV            1     7     3     4     7

    6
--------------------------------------------------------------
AT SELECTION-SCREEN ...     1     8     3     4     9     6
AT SELECTION-SCREEN for  POH  1     7     3     4     7     6
AT SELECTION-SCREEN for  POV  1     7     3     4     7     6
AT SELECTION-SCREEN ON EXIT 1     7     3     4     7     6
--------------------------------------------------------------
AT LINE-SELECTION           1    10     3     4    10     6
AT PFn                      1    10     3     4    10     6
AT USER-COMMAND             1    10     3     4    10     6
--------------------------------------------------------------
INITIALIZATION              1    11     3     4    11     6
START-OF-SELECTION          1    11     3     4    11     6
GET                         1    11     3     4    11     6
END-OF-SELECTION            1    11     3     4    11     6
--------------------------------------------------------------
TOP-OF-PAGE                 1    11     3     4    11     6
END-OF-PAGE                 1    11     3     4    11     6
TOP-OF-PAGE DURING ...      1    10     3     4    10     6
--------------------------------------------------------------
LOAD-OF-PROGRAM             1     1     4     4     4     6
--------------------------------------------------------------
PBO Module                   1     1     4     4     4     6
AT SELECTION-SCREEN OUTPUT  1     1     4     4     4     6
--------------------------------------------------------------
Procedure:                    see

Messages
--------------------------------------------------------------

  1. The message appears in a dialog box and the program terminates. When the user has confirmed the message, control returns to the next- highest area. All the internal sessions are deleted from the stack.

  2. The message appears in the status line. Then PAI terminates and the system returns to the current screen. All the screen fields combined using FIELD or CHAIN are now ready for input. The user must enter new values. The system triggers the PAI event again, with the new values.

  3. The message appears in a dialog box. Once the user has confirmed the message, the program continues immediately after the MESSAGE statement.

  4. The message appears in the status line of the next screen. The program continues immediately after the message statement.

  5. The message appears in the status line. Then the system continues as in 2, except that the user can quit the message using ENTER without having to enter new values. The system continues handling the PAI event from immediately after the message statement.

  6. No message is displayed and a runtime error, MESSAGE_TYPE_X, is triggered. The short dump text contains the message identification.

  7. The program terminates with a runtime error DYNPRO_MSG_IN_HELP. While F1 and F4 are processed, the system cannot send error messages or warnings.

  8. The message appears in the status line. Then the system stops selection screen processing and returns to the selection screen itself. The screen fields specified in the additions to the AT SELECTION-SCREEN statement are now ready for input. The user must enter new values. The system then starts processing the selection screen again with the new values.

  9. The message appears in the status line. Then the system continues as in 8, except the the user can quit the message using ENTER, without having to enter new values. The system continues handling the PAI event from immediately after the message statement.

  10. The message appears in the status line and the processing block terminates. The list level is displayed as before.

  11. The message appears in the status line and the processing block terminates. The system then returns to the program call.

  12. For a demonstration of messages in different contexts, see Example Programs for Messages.


Variant 1

MESSAGE xnnn.


Extras:

1. ... WITH f1 ... f4

2. ... RAISING exception

3. ... INTO f

4. ... DISPLAY LIKE mtype

Effect

Outputs the message nnn from the message class i with the type x. You must specify the message class i using the MESSAGE-ID addition to the REPORT statement, PROGRAM, or another introductory program statement.

Example

MESSAGE I001.

  • You can specify a different message class in parentheses after the error number, for example MESSAGE I001(SU).
  • When executing the statement, the following system variables are set:

    * SY-MSGID (message class)
    * SY-MSGTY (message type)
    * SY-MSGNO (message number)


Addition 1

... WITH f1 ... f4

Effect

Inserts the contents of a field fi in the message instead of in the placeholder &i. If unnumbered variables (&) are used in a message text, these are replaced consecutively by the fields f1 to f4.
To aid compilation, only numbered variables (&1 to &4) are to be used in future if several fields are involved.
If a "&" is supposed to appear in the message at runtime, you must enter &&.
In the long text of a message, the symbol &Vi& is replaced by the field contents of fi.
After WITH, you can specify 1 to 4 fields.

Note

You can output up to 50 characters per field. If the field contains more characters, these are ignored.

Example

MESSAGE E0004 WITH 'Hugo'.

Note

When executing the statement, the contents of the fields f1 to f4 are assigned to the system fields SY-MSGV1, SY-MSGV2, SY-MSGV3 and SY-MSGV4.

Addition 2

... RAISING exception

Effect

Only possible within a function module or a method (see FUNCTION, METHOD):
Triggers the exception exception.
If the program calling the function module or method handles the exception itself, control returns immediately to that program (see CALL FUNCTION and CALL METHOD). Only then are the current values passed from the procedure to the EXPORTING-, CHANGING- (und RETURNING) parameters of the function module or method, if they are specified as pass-by- reference. However, the calling program can refer to the system field values (see above).

If the calling program does not handle the exception itself, the message is output (see RAISE).
You cannot use this addition in conjunction with the ... INTO cf addition.

Note

If, during a Remote Function Call, an error occurs in the target system, details of the error message are passed back to the calling system in the following system fields: SY-MSGNO, SY-MSGID, SY-MSGTY, SY-MSGV1, SY-MSGV2, SY-MSGV3, and SY-MSGV4. These fields are initialized before every RFC. If a short dump or a type X message occurs, the short text of the dump is transferred to the caller, and the contents of SY-MSGID, SY-MSGTY, SY-MSGNO, and SY-MSGV1 assigned by the system.

In RFC-enabled function modules, no ABAP statements are allowed that would end the RFC connection (for example, either LEAVE or SUBMIT without the AND RETURN addition).

Example
MESSAGE E001 RAISING NOT_FOUND.

Addition 3

... INTO f

Effect

Instead of displaying the message, the system places the message text in the field f. The message type is not evaluated.

You cannot use this addition in conjunction with the ...RAISING exception or the DISPLAY LIKE mtype addition. The system sets the following system variables: SY-MSGID (message class), SY-MSGTY (message type), SY-MSGNO (message number) and SY-MSGV1, SY-MSGV2, SY-MSGV3, SY-MSGV4 (parameters).

Example

DATA msgtext(72).

...

MESSAGE E004 WITH 'Hugo' INTO msgtext.

Addition 4

... DISPLAY LIKE mtype

Effect

The message display uses the icon of the message type mtype but the message is handled according to its actual type.

Note

This addition cannot be used in conjunction with the addition ... INTO.

Example

MESSAGE I004 DISPLAY LIKE 'E'.

Variant 2

MESSAGE ID id TYPE mtype NUMBER n.


Extras:

1. ... WITH f1 ... f4

2. ... RAISING exception

3. ... INTO f

4. ... DISPLAY LIKE mtype

Effect

The message components are set dynamically:

ID
Message class
TYPE
Message type
NUMBER
Message number

The addition MESSAGE-ID of the introductory program statement is not required or is overridden.

Addition 1

... WITH f1 ... f4

Addition 2

... RAISING exception

Addition 3

... INTO f

Addition 4

... DISPLAY LIKE mtype

Effect

As in variant 1.

Example

MESSAGE ID 'SU' TYPE 'E' NUMBER '004' WITH 'Hugo'.

Outputs the message with the number 004 and MESSAGE-ID SU (see above) as an E (Error) message and replaces the first variable (&) with 'Hugo'.

Example

MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
        WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

Constructs the message dynamically from the contents of the system fields SY-MSGID, SY-MSGTY, SY-MSGNR , SY-MSGV1 ,SY-MSGV2,SY-MSGV3,and SY-MSGV4. These may, for example, be set by an exception after CALL FUNCTION or CALL TRANSACTION ... USING.

Variant 3

MESSAGE xnnn(mid).


Extras:

1. ... WITH f1 ... f4

2. ... RAISING exception

3. ... INTO f

4. ... DISPLAY LIKE mtype

Effect

As in variant 1. The message class is determined by specifying mid. The addition MESSAGE-ID of the introductory program statement is not required or is overridden.

Addition 1

... WITH f1 ... f4

Addition 2

... RAISING exception

Addition 3

... INTO f

Addition 4

... DISPLAY LIKE mtype

Effect

As in variant 1.

Example

MESSAGE X004(SU) WITH 'Hugo'.

Variant 4

MESSAGE msg TYPE mtype.


Extras:

1. ... RAISING exception
2. ... DISPLAY LIKE mtype

Effect

With this variant, the message can be passed directly in the form of a character-like field. The message type is specified using the required TYPE addition.

Addition 1

... RAISING exception

Addition 2

... DISPLAY LIKE mtype

Effect

As in variant 1.

Example

MESSAGE 'File not found.' TYPE 'E'.

The text 'File not found.' is output as the error message.

Exceptions

Non-Catchable Exceptions

  • Cause: Message type unknown
    Runtime Error: MESSAGE_TYPE_UNKNOWN
  • Cause: Triggers termination with a short dump
    Runtime Error: MESSAGE_TYPE_X


Additional help

Messages


使用心得:
Message 显示给用户的字符串长度有限制,将很长的显示信息分配给Message的多个参数来显示出来,如果采用字符串连接后给第一个参数显示可能出现丢失显示信息。




 

<think>我们正在讨论ABAP中的MESSAGE语句。根据用户的问题,他/她想了解如何在ABAP中编写消息(MESSAGE语句的用法)。由于ABAP是一种用于SAP系统开发的编程语言,MESSAGE语句用于在程序中向用户显示消息。 在ABAP中,MESSAGE语句有几种不同的形式,主要用于在程序执行期间向用户报告信息、警告、错误等。消息可以是预定义在T100消息库中的,也可以是硬编码在程序中的(虽然不推荐,因为不利于多语言支持)。 ### 1. 显示T100消息库中的消息 通常,我们会在SE91事务代码中创建消息类(Message Class),并在消息类中定义消息编号和消息文本。然后在ABAP程序中使用MESSAGE语句按照以下格式显示: ``` MESSAGE ID <message_id> TYPE <message_type> NUMBER <message_number> WITH <v1> <v2> <v3> <v4>. ``` - `<message_id>`:消息类(例如,'ZMY_MESSAGE_CLASS') - `<message_type>`:消息类型,可以是以下之一: - 'E':错误(Error),会中断当前事务并显示消息 - 'W':警告(Warning),用户可以选择继续或取消 - 'I':信息(Information),弹出信息框,用户确认后继续 - 'S':成功(Success),在状态栏显示 - 'A':终止(Abend),会终止程序并显示消息 - 'X':退出(Exit),会导致程序以运行时错误终止 - `<message_number>`:消息编号(三位数字,例如123) - `WITH <v1> ... <v4>`:用于替换消息文本中的占位符(&1, &2, &3, &4) 例如,假设在消息类ZMY_MSG中定义了消息编号001,消息文本为“Material &1 not found”。那么可以这样使用: ``` DATA: lv_matnr TYPE matnr VALUE '123456'. MESSAGE ID 'ZMY_MSG' TYPE 'E' NUMBER '001' WITH lv_matnr. ``` ### 2. 使用短消息格式 ABAP也支持短格式的MESSAGE语句,直接指定消息类型和消息文本(但这种方式不推荐用于正式程序,因为文本是硬编码的,且不支持多语言): ``` MESSAGE 'This is a message' TYPE 'I'. ``` ### 3. 使用消息号直接发送 还可以使用消息号直接发送(但需要指定消息类): ``` MESSAGE e001(zmy_msg) WITH lv_matnr. ``` ### 4. 在异常中抛出消息 在类方法中,我们也可以使用MESSAGE ... RAISING语句将消息作为异常抛出: ``` MESSAGE e001(zmy_msg) WITH lv_matnr RAISING material_not_found. ``` ### 注意事项 - 在ABAP程序中,通常建议使用T100消息库中的消息,这样便于维护和多语言支持。 - 消息类型决定了消息的显示方式以及程序的行为(例如,类型'E'会中断当前事务,而类型'I'则不会)。 ### 示例代码 以下是一个简单的示例,展示如何在ABAP程序中使用MESSAGE语句: ```abap REPORT z_message_demo. DATA: lv_matnr TYPE matnr VALUE '123456'. START-OF-SELECTION. " 假设消息类ZMY_MSG中消息001的文本为 'Material &1 not found' MESSAGE ID 'ZMY_MSG' TYPE 'E' NUMBER '001' WITH lv_matnr. ``` 当执行上述程序时,如果消息类ZMY_MSG中的消息001存在,则会显示一个错误消息:“Material 123456 not found”。 ### 引用说明 在ABAP中,消息是用户交互的重要组成部分,正确使用消息可以提高程序的用户友好性。关于消息的更多详细用法,可以参考SAP官方ABAP文档[^1]。 §§相关问题§§ 1. 如何在ABAP中创建消息类(Message Class)? 2. ABAP消息类型(E, W, I, S, A, X)分别有什么作用? 3. 如何在ABAP异常处理中使用MESSAGE语句? 4. 如何为ABAP消息实现多语言支持?</think>### ABAP MESSAGE语句用法详解 在ABAP中,`MESSAGE`语句用于向用户显示信息、警告或错误消息。以下是主要用法和规则: #### 1. 基本语法格式 ```abap MESSAGE <message_type> [<message_id>(<message_class>)] [WITH <v1> <v2> <v3> <v4>] [RAISING <exception>]. ``` - `<message_type>`:消息类型(`E`错误, `W`警告, `I`信息, `S`成功, `A`终止) - `<message_id>`:消息编号(3位数字) - `<message_class>`:消息类(事务码`SE91`创建) - `WITH`:替换消息文本中的占位符`&1`,`&2`等 #### 2. 常用消息类型 | 类型 | 行为 | 示例 | |------|------|------| | `E` | 错误,中断当前操作 | `MESSAGE E001(ZMY_CLASS)` | | `W` | 警告,用户需确认 | `MESSAGE W002(ZMY_CLASS)` | | `I` | 信息,弹出对话框 | `MESSAGE I003(ZMY_CLASS)` | | `S` | 成功,状态栏显示 | `MESSAGE S004(ZMY_CLASS)` | | `A` | 终止,终止程序 | `MESSAGE A005(ZMY_CLASS)` | #### 3. 实际应用示例 ```abap " 显示带变量的错误消息 DATA lv_matnr TYPE matnr VALUE '100-100'. MESSAGE E001(ZMM) WITH lv_matnr. " 消息文本: Material &1 not found " 在方法中抛出异常 METHODS process_item RAISING cx_failure. METHOD process_item. IF item_invalid. MESSAGE E123(ZORDER) RAISING cx_failure. ENDIF. ENDMETHOD. " 直接显示文本(不推荐,应使用消息类) MESSAGE 'Invalid input' TYPE 'E'. ``` #### 4. 最佳实践 1. **使用消息类**:始终在`SE91`中创建消息类,避免硬编码文本 ```abap " 正确用法 MESSAGE ID 'ZMY_CLASS' TYPE 'E' NUMBER '001'. ``` 2. **占位符限制**:最多支持4个变量(`&1`-`&4`) 3. **程序上下文**: - 在报表中使用`AT SELECTION-SCREEN`验证输入 - 在对话程序中使用`PBO`/`PAI`模块 4. **异常处理**:结合`RAISING`将消息转换为异常 #### 5. 调试技巧 - 使用事务码`ST22`查看消息导致的`dump` - 在调试器输入`/h`后执行,可跳过消息弹出 > 重要提示:避免在循环中使用`MESSAGE I`,可能导致多次弹出窗口中断操作[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值