ABAP--编写应用服务器执行外部命令的程序示例

本文详细介绍了SAP系统中SXPG_CALL_SYSTEM函数模块的使用方法,包括参数设置、异常处理等内容,并提供了具体示例代码。此外还展示了如何利用OpenDataset在UNIX环境下执行文件压缩与解压操作。

1、相关Package
SBTC

2、相关T-CODE
SM69:
SM49

3、SXPG_CALL_SYSTEM函数说明
 

Use this function module to do the following:
·        Check a user’s authorization to run a command
·        Carry out the command.
To determine the system on which the command should be carried out, the function module takes by default the user’s current host system and the operating system type. 
Since the function module is RFC-capable, you can use RFC (the remote function call interface) to carry run the function module at another SAP application server. The external command is then carried out on the host system on which the destination server is running. See CALL FUNCTION... DESTINATION in the ABAP syntax documentation or User’s Guide for more information.
SXPG_CALL_SYSTEM checks the command that is specified in the call before carrying it out. The function module follows these rules in performing this check:
·        It checks for a command definition that has the same operating system type as in the system field SY-OPSYS. If found, then this command definition is used to run the command.
·        If the first check fails, then the function module looks to see if the command has been defined with an operating system type that belongs to the same syntax group as the SY-OPSYS operating system. A syntax group is an SAP construct that groups together OS’s that share the same command and file name syntax. Example: the various flavors of UNIX belong to a syntax group. 
If found, then this command definition is used to run the command. Example: If SY-OPSYS shows HP-UX as the OS, then a command definition for Sun UNIX would also be acceptable for carrying out the command. 
·        If the second check also fails, then the function modules looks for the command with the operating system type ANYOS. ANYOS indicates that a command definition can be carried out on all supported host operating systems.
If found, then this command definition is used to run the command.
Syntax:
CALL FUNCTION ‘SXPG_CALL_SYSTEM’
     IMPORTING                                                
        COMMANDNAME   = <Name of command to run> “ Default '*'
        ADDITIONAL_PARAMETERS    = <Argument string> “ Default <space>        
     EXPORTING                                                
        STATUS        = <Exit status of command>                  
      TABLES                                                   
        EXEC_PROTOCOL = <Log> “ In structure BTCXPM. Can
                               “ contain STDOUT, STDERR

      EXCEPTIONS                                               
        NO_PERMISSION “ Command rejected by user exit auth.
                      “ check

        COMMAND_NOT_FOUND  “ Command not defined in SAP database
        PARAMETERS_TOO_LONG  “ Complete parameter string exceeds
                             “ 128 characters

        SECURITY_RISK “ Security check failed
        WRONG_CHECK_CALL_INTERFACE “ Problem with function
                                    “ module for additional
                                    “ security check

        PROGRAM_START_ERROR “ Error while starting program                               
        PROGRAM_TERMINATION_ERROR “ Error while requesting final
                                  “ status of program

        X_ERROR “ Reserved
        PARAMETER_EXPECTED “ Required parameter not specified
        TOO_MANY_PARAMETERS “ User arguments not allowed by
                            “ supplied in call
              
        ILLEGAL_COMMAND “ Command not legitimately defined
       
OTHERS
Parameters
IMPORTING Parameters

Parameter name
Use
COMMANDNAME
The name of the definition of the external command, as specified in the maintenance function (transaction SM69).
PARAMETERS
Arguments for the external command as specified by the definition in the SAP system and by the calling program or user.
These arguments are checked for impermissible characters, such as the ; under UNIX. Problems are registered with the SECURITY_RISK exception.

 
EXPORTING Parameters

Parameter name
Use
STATUS
Returns the final status of the execution of the external command:
·      Value ‘O’: The external command was started and ran to end successfully.
·      Value ‘E’: An error occurred; the external command was not run successfully. 

 
Tables Parameters

Parameter name
Use
EXEC_PROTOCOL
Contains the STDOUT and STDERR output of the external command and any output from the target host system.

 
Exceptions

Exception name
Meaning
X_ERROR
Reserved for future use.
NO_PERMISSION
The AUTHORITY-CHECK of the user’s authorization for the authorization object S_LOG_COM failed. The user is not authorized to carry out the command named with the specified arguments on the target system. 
COMMAND_NOT_FOUND
Command name, as identified by COMMANDNAME and OPERATINGSYSTEM, has not been defined in the maintenance function (transaction SM69).
PARAMETERS_TOO_LONG
The combined argument string (ADDITIONAL_PARAMETERS and the DEFINED_PARAMETERS, as returned in ALL_PARAMETERS) exceeds the limit of 128 characters in length.
SECURITY_RISK
Either:
· The command contains impermissible characters. These are characters with potentially dangerous properties, such as ; under UNIX.
· The command definition specifies that an extra-check function module be run. This function module has rejected execution of the command. 
WRONG_CHECK_CALL_
INTERFACE
The command definition specifies that an extra-check function module is to be run. Either this function module is missing, or the interface defined for this function module does not match that of the standard SAP function module SXPG_DUMMY_COMMAND_CHECK. For more information, please see SXPG_DUMMY_COMMAND_CHECK: Interface for Extra-Check Function Modules.
TOO_MANY_PARAMETERS
The command definition specifies that user-specified arguments for the external command are not allowed. However, an additional string of command arguments was specified.
PARAMETER_EXPECTED
The command definition includes the placeholder character ?, which signifies that additional user-defined arguments are required. However, no additional arguments string was supplied.
PROGRAM_START_ERROR
An error occurred while starting the external command. The SAP system field SY-MSGV1 contains additional information on the problem.
PROGRAM_TERMINATION_
ERROR
An error occurred while trying to obtain the return code of the external program. The SAP system field SY-MSGV1 contains additional information on the problem.
ILLEGAL_COMMAND
The external command definition was modified “illegally”. That is, the command was not modified by means of the maintenance function (transaction SM69).
The modified command is registered in the system log in its substituted form. The message is registered under the system log ID “LC”.    
OTHERS
Catch any new exceptions added to this function module. 

4、示例
REPORT  ZABAPFTP.

data : t_btcxpm      like btcxpm occurs 0,
         p_addparam like sxpgcolist-parameters,
         rep_date       like sy-datum,
         t_date           like SXPGCOLIST-PARAMETERS.

rep_date = sy-datum - 1.
t_date = rep_date.

*p_addparam = '/sap_production/usr/sap/trans/data/zabapftp.sh'.

refresh t_btcxpm. clear t_btcxpm.

call function 'SXPG_CALL_SYSTEM'
     EXPORTING
          commandname                = 'ZABAPFTP'
          additional_parameters      = t_date
     TABLES
          exec_protocol              = t_btcxpm
     EXCEPTIONS
          no_permission              = 1
          command_not_found          = 2
          parameters_too_long        = 3
          security_risk              = 4
          wrong_check_call_interface = 5
          program_start_error        = 6
          program_termination_error  = 7
          x_error                    = 8
          parameter_expected         = 9
          too_many_parameters        = 10
          illegal_command            = 11
          others                     = 12.

if sy-subrc ne 0.
   write:/ 'Error in ZABAPFTP ', sy-subrc.
endif.

5、使用Open Dataset的FILTER执行系统命令
下面为UNIX环境下的文件压缩和解压
DATA DSN(20) VALUE '/usr/test.Z'.
OPEN DATASET DSN FOR OUTPUT FILTER 'compress'.
OPEN DATASET DSN FOR INPUT FILTER 'uncompress'.

 

在SAP ABAP中,内外部转换函数主要用于处理单位和金额的标准化转换。这些函数确保数据在系统内部存储时符合统一格式,并且在展示给用户时能够按照业务需求进行显示。以下是几个常见的实际应用场景和使用方法。 ### 单位转换 #### 内部单位转外部单位 在某些情况下,系统需要将内部存储的单位(如标准计量单位)转换为用户可读的外部单位。这种转换通常用于报表展示或界面显示。例如,系统可能以“EA”(Each)作为内部单位存储数量,但在界面上需要显示为“件”。 **示例代码:** ```abap CALL FUNCTION 'CONVERSION_EXIT_CUNIT_OUTPUT' EXPORTING input = gs_data-meins IMPORTING output = gs_data-meins. ``` 该函数将`gs_data-meins`中的内部单位转换为外部单位并赋值给同一个字段[^3]。 #### 外部单位转内部单位 当用户输入外部单位时,需要将其转换为系统内部使用的标准单位以便存储和计算。例如,用户输入“件”,系统需要将其转换为“EA”。 **示例代码:** ```abap CALL FUNCTION 'CONVERSION_EXIT_CUNIT_INPUT' EXPORTING input = user_input_unit IMPORTING output = internal_unit. ``` ### 金额转换 #### 外部金额转内部金额 外部金额通常是用户输入的数值,而内部金额需要根据特定的转换因子进行调整。例如,在货币转换过程中,外部金额需要除以一个转换因子才能存储为内部金额。 **示例代码:** ```abap CALL FUNCTION 'BAPI_CURRENCY_CONV_TO_INTERNAL' EXPORTING currency = 'JPY' amount_external = jpy_e max_number_of_digits = 23 IMPORTING amount_internal = jpy_i return = ret. ``` 此函数将外部金额`jpy_e`转换为内部存储金额`jpy_i`,并返回消息`ret`[^2]。 #### 内部金额转外部金额 当从数据库中读取内部金额时,可能需要将其转换为用户可见的外部金额。例如,在报表中显示金额时,需要乘以转换因子。 **示例代码:** ```abap CALL FUNCTION 'BAPI_CURRENCY_CONV_FROM_INTERNAL' EXPORTING currency = 'USD' amount_internal = usd_i IMPORTING amount_external = usd_e return = ret. ``` ### 实际应用案例 1. **物料管理模块** 在物料管理中,不同仓库可能使用不同的计量单位。例如,一个仓库使用“千克”,另一个仓库使用“吨”。通过单位转换函数,可以确保所有数据在系统内部统一为标准单位(如“千克”),并在展示时根据用户需求转换为相应的外部单位。 2. **财务模块** 在财务模块中,金额通常需要根据汇率进行转换。例如,将美元金额转换为欧元金额。通过金额转换函数,可以确保内部存储的金额与外部显示的金额保持一致,并且可以根据需要进行转换。 3. **销售订单处理** 在销售订单处理中,客户可能会以不同的货币下单。系统需要将这些外部货币金额转换为内部存储金额,以便进行后续的财务核算和报表生成。 4. **跨公司交易** 在多公司架构下,不同公司可能使用不同的货币和单位。通过内外部转换函数,可以确保交易数据在不同公司之间传递时保持一致性。 5. **数据迁移与集成** 在数据迁移或与其他系统集成时,外部系统的数据格式可能与SAP系统的内部存储格式不一致。通过使用转换函数,可以确保数据在导入SAP系统之前被正确转换为内部格式。 ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值