使用外部程序为Query提供数据

Query 很强大,通过下面的Help说明,可以实现使用ABAP程序来为InfoSet提供数据源,然后再作进一步的处理。

Retrieving Data with Programs

If you want to make evaluations for datasets, but SAP Query automatic data retrieval is not sufficient for the task, create an InfoSet by selecting the field Data Retrieval with Program in the Title and Database screen, then specify a program name and the name of a structure. This structure must reflect the record structure of the dataset you want to evaluate.

 

If you use the component Maintain Queries to create a query for this InfoSet, SAP Query uses the report as a model when generating the query report; the model report itself thus remains unchanged.

 

Before generating the InfoSet for the first time, you must create the model report yourself in the ABAP editor. The model report has to be syntactically correct and have the same definition for fixed point arithmetic as the InfoSet. It is not, however, intended that the model report itself be executed.

 

The structure of this model report and the sequence of its parts are regular. The main structure of a model report is given below.

Report xxxxxxxx .

 

Tables tab .

Definition of Dictionary structure used to set up the InfoSet. This structure must contain the records you want to evaluate.

Parameters .

Definition of parameters, selection criteria, and fields.

Select-Options:…

 

DATA :…

 

DATA: BEGIN OF itab OCCURS xxx.

INCLUDE STRUCTURE tab.

DATA: END of itab.

Definition of an internal table <itab> with structure <tab> which provides the records you want to evaluate.

* <Query_head>

This comment must always appear after your data declarations.

* Code to define the table itab, if such a table is used.

* Beginning of a loop to retrieve each record and place it in the structure tab (SELECT, DO, LOOP, …)

* Code for formatting data (if necessary)

 

* <Query_body>

This comment must always be the last ’statement’ in the loop. The data must be available in structure tab.

* End of data retrieval loop for individual records (ENDSELECT, ENDDO, ENDLOOP;…)

 

 

 

Note : If the model report components are in the wrong sequence, SAP Query may generate meaningless reports. The character strings of the two comment lines *<QUERY_HEAD> and *<QUERY_BODY> are fixed to start immediately after the character “<”, but the system does not distinguish between upper and lower case.

 

Continue:

Generating the InfoSet is possible only if the model report exists, conforms to the above conventions, is free of syntax errors and has the correct fixed point arithmetic setting.

 

When you generate query reports which use a model report, the generated reports inherit the attributes of the model report. In the end, this allows you to retrieve data by using a logical database. However, you should only make use of this option in special cases, since if you are using a logical database, it is better to set up the InfoSet this way also.

 

Below is a model report which uses the SELECT statement:

*————————————————————————–

* data retrieval program for functional area FLDP

*————————————————————————–

REPORT AQ00FLDP.

 

TABLES SAPLANE.

 

SELECT-OPTIONS TYPE FOR SAPLANE-PLANETYPE.

 

*<Query_head>

select * from saplane where planetype in type.

*<Query_body>

endselect.

 

Below is a model report which uses the LOOP statement:

*————————————————————————–

* data retrieval program for functional area FLDX

*————————————————————————–

REPORT AQ00FLDX.

 

TABLES: SAPLANE, INDX.

 

DATA: PLANEDATA LIKE SAPLANE OCCURS 100 WITH HEADER LINE.

 

SELECT-OPTIONS TYPE FOR PLANEDATA-PLANETYPE.

 

*<Query_head>

IMPORT PLANEDATA FROM DATABASE INDX(PL) ID ‘PLANEDAT’.

LOOP AT PLANEDATA WHERE PLANETYPE IN TYPE.

MOVE-CORRESPONDING PLANEDATA TO SAPLANE.

*<Query_body>

ENDLOOP.

 

The data is retrieved and placed in an internal table which is filled by an import from the INDX. In the LOOP structure, each line must be read from PLANEDATA into SAPLANE because the InfoSet was created via the structure SAPLANE and the query expects the data in a field string called SAPLANE.

 

You can see all the options available for you to use with InfoSets using logical databases in the screen for InfoSet maintenance, for example connecting additional tables and the definition of additional fields, and so on. Make a special note of the special features called Sequential Datasets at the end of the section.

 

InfoSets which retrieve data using a program, offer you a wide range of options. In a model report, there are no restrictions on how you should organize the retrieval of your data. Therefore you can use very complex algorithms. SELECT statements that read on a cross-client basis can also be used (option CLIENT SPECIFIED).

在Linux系统上通过MySQL命令行工具查询数据库的数据,通常需要以下几个步骤: 1. **安装MySQL**:首先确保您的Linux系统已经安装了MySQL,如果没有,可以使用包管理器(如apt、yum或dnf)进行安装: ``` sudo apt-get install mysql-server ``` 2. **启动服务并登录**:安装完成后,启动MySQL服务并打开命令行客户端(例如,在Ubuntu上): ```sh sudo service mysql start mysql -u [username] -p ``` 输入密码后,进入MySQL shell。 3. **选择数据库**: ``` USE your_database_name; ``` 将`your_database_name`替换为您想要查询的实际数据库名称。 4. **运行SQL查询**: 您可以在这输入SQL查询语句,例如获取某个表的所有记录: ```sql SELECT * FROM your_table_name; ``` 将`your_table_name`替换为您实际的表名。 5. **查看结果**: 查询完成后,会显示查询结果。 如果你想在外部脚本中执行这样的操作,你可以创建一个shell脚本或Python等程序,编写包含上述步骤的代码,并使用命令行工具执行。例如,Python中可以使用`subprocess`模块来执行SQL命令: ```python import subprocess def query_db(): cmd = f'mysql -u [username] -p -e "SELECT * FROM your_table_name;"' process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) output, error = process.communicate() return output.decode('utf-8') # 调用函数 result = query_db() ``` 记得将占位符替换成实际的用户名、密码和表名。执行完这个脚本,就可以在终端看到查询结果了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值